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 5*5169Slclee * Common Development and Distribution License (the "License"). 6*5169Slclee * 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 */ 21*5169Slclee 220Sstevel@tonic-gate /* 23*5169Slclee * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 320Sstevel@tonic-gate /* All Rights Reserved */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 350Sstevel@tonic-gate 360Sstevel@tonic-gate /* 370Sstevel@tonic-gate * PROGRAM: fdisk(1M) 380Sstevel@tonic-gate * This program reads the partition table on the specified device and 390Sstevel@tonic-gate * also reads the drive parameters. The user can perform various 400Sstevel@tonic-gate * operations from a supplied menu or from the command line. Diagnostic 410Sstevel@tonic-gate * options are also available. 420Sstevel@tonic-gate */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate #include <stdio.h> 450Sstevel@tonic-gate #include <stdlib.h> 460Sstevel@tonic-gate #include <string.h> 470Sstevel@tonic-gate #include <unistd.h> 480Sstevel@tonic-gate #include <errno.h> 490Sstevel@tonic-gate #include <fcntl.h> 500Sstevel@tonic-gate #include <ctype.h> 510Sstevel@tonic-gate #include <sys/stat.h> 520Sstevel@tonic-gate #include <sys/types.h> 530Sstevel@tonic-gate #include <sys/param.h> 540Sstevel@tonic-gate #include <sys/systeminfo.h> 550Sstevel@tonic-gate #include <sys/efi_partition.h> 560Sstevel@tonic-gate #include <sys/byteorder.h> 570Sstevel@tonic-gate #include <sys/systeminfo.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 600Sstevel@tonic-gate #include <sys/dkio.h> 610Sstevel@tonic-gate #include <sys/vtoc.h> 620Sstevel@tonic-gate 630Sstevel@tonic-gate #define CLR_SCR "[1;1H[0J" 640Sstevel@tonic-gate #define CLR_LIN "[0K" 650Sstevel@tonic-gate #define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \ 660Sstevel@tonic-gate "[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H" 670Sstevel@tonic-gate #define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K" 680Sstevel@tonic-gate #define W_LINE "[12;1H[0K[11;1H[0K" 690Sstevel@tonic-gate #define E_LINE "[24;1H[0K[23;1H[0K" 700Sstevel@tonic-gate #define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \ 710Sstevel@tonic-gate "[0K[18;1H[0K[19;1H[0K[13;1H" 720Sstevel@tonic-gate #define T_LINE "[1;1H[0K" 730Sstevel@tonic-gate 740Sstevel@tonic-gate #define DEFAULT_PATH "/dev/rdsk/" 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* XXX - should be in fdisk.h, used by sd as well */ 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * the MAX values are the maximum usable values for BIOS chs values 800Sstevel@tonic-gate * The MAX_CYL value of 1022 is the maximum usable value 810Sstevel@tonic-gate * the value of 1023 is a fence value, 820Sstevel@tonic-gate * indicating no CHS geometry exists for the corresponding LBA value. 830Sstevel@tonic-gate * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1) 840Sstevel@tonic-gate * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT) 850Sstevel@tonic-gate */ 860Sstevel@tonic-gate #define MAX_SECT (63) 870Sstevel@tonic-gate #define MAX_CYL (1022) 880Sstevel@tonic-gate #define MAX_HEAD (254) 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* for clear_vtoc() */ 910Sstevel@tonic-gate #define OLD 0 920Sstevel@tonic-gate #define NEW 1 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* readvtoc/writevtoc return codes */ 950Sstevel@tonic-gate #define VTOC_OK 0 /* Good VTOC */ 960Sstevel@tonic-gate #define VTOC_INVAL 1 /* invalid VTOC */ 970Sstevel@tonic-gate #define VTOC_NOTSUP 2 /* operation not supported - EFI label */ 980Sstevel@tonic-gate #define VTOC_RWERR 3 /* couldn't read or write VTOC */ 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Support for fdisk(1M) on the SPARC platform 1020Sstevel@tonic-gate * In order to convert little endian values to big endian for SPARC, 1030Sstevel@tonic-gate * byte/short and long values must be swapped. 1040Sstevel@tonic-gate * These swapping macros will be used to access information in the 1050Sstevel@tonic-gate * mboot and ipart structures. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate #ifdef sparc 1090Sstevel@tonic-gate #define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF)) 1100Sstevel@tonic-gate #define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \ 1110Sstevel@tonic-gate (les((unsigned)((val)&0xffff0000)>>16))) 1120Sstevel@tonic-gate #else 1130Sstevel@tonic-gate #define les(val) (val) 1140Sstevel@tonic-gate #define lel(val) (val) 1150Sstevel@tonic-gate #endif 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1180Sstevel@tonic-gate #define VTOC_OFFSET 512 1190Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8) 1200Sstevel@tonic-gate #define VTOC_OFFSET 0 1210Sstevel@tonic-gate #else 1220Sstevel@tonic-gate #error No VTOC format defined. 1230Sstevel@tonic-gate #endif 1240Sstevel@tonic-gate 125251Slclee static char Usage[] = "Usage: fdisk\n" 1260Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1270Sstevel@tonic-gate "[ -b masterboot ]\n" 1280Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1290Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n" 1300Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n" 1310Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice"; 1320Sstevel@tonic-gate 133251Slclee static char Usage1[] = " Partition options:\n" 1340Sstevel@tonic-gate " -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1350Sstevel@tonic-gate " Create a partition with specific attributes:\n" 1360Sstevel@tonic-gate " id = system id number (fdisk.h) for the partition type\n" 1370Sstevel@tonic-gate " act = active partition flag (0 is off and 128 is on)\n" 1380Sstevel@tonic-gate " bhead = beginning head for start of partition\n" 1390Sstevel@tonic-gate " bsect = beginning sector for start of partition\n" 1400Sstevel@tonic-gate " bcyl = beginning cylinder for start of partition\n" 1410Sstevel@tonic-gate " ehead = ending head for end of partition\n" 1420Sstevel@tonic-gate " esect = ending sector for end of partition\n" 1430Sstevel@tonic-gate " ecyl = ending cylinder for end of partition\n" 1440Sstevel@tonic-gate " rsect = sector number from start of disk for\n" 1450Sstevel@tonic-gate " start of partition\n" 1460Sstevel@tonic-gate " numsect = partition size in sectors\n" 1470Sstevel@tonic-gate " -b master_boot\n" 1480Sstevel@tonic-gate " Use master_boot as the master boot file.\n" 1490Sstevel@tonic-gate " -B Create one Solaris partition that uses the entire disk.\n" 1500Sstevel@tonic-gate " -E Create one EFI partition that uses the entire disk.\n" 1510Sstevel@tonic-gate " -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1520Sstevel@tonic-gate " Delete a partition. See attribute definitions for -A.\n" 1530Sstevel@tonic-gate " -F fdisk_file\n" 1540Sstevel@tonic-gate " Use fdisk_file to initialize on-line fdisk table.\n" 1550Sstevel@tonic-gate " -I Forego device checks. Generate a file image of what would go\n" 1560Sstevel@tonic-gate " on a disk using the geometry specified with the -S option.\n" 1570Sstevel@tonic-gate " -n Do not run in interactive mode.\n" 1580Sstevel@tonic-gate " -R Open the disk device as read-only.\n" 1590Sstevel@tonic-gate " -t Check and adjust VTOC to be consistent with fdisk table.\n" 1600Sstevel@tonic-gate " VTOC slices exceeding the partition size will be truncated.\n" 1610Sstevel@tonic-gate " -T Check and adjust VTOC to be consistent with fdisk table.\n" 1620Sstevel@tonic-gate " VTOC slices exceeding the partition size will be removed.\n" 1630Sstevel@tonic-gate " -W fdisk_file\n" 1640Sstevel@tonic-gate " Write on-disk table to fdisk_file.\n" 1650Sstevel@tonic-gate " -W - Write on-disk table to standard output.\n" 1660Sstevel@tonic-gate " -v Display virtual geometry. Must be used with the -W option.\n" 1670Sstevel@tonic-gate " Diagnostic options:\n" 1680Sstevel@tonic-gate " -d Activate debug information about progress.\n" 1690Sstevel@tonic-gate " -g Write label geometry to standard output:\n" 1700Sstevel@tonic-gate " PCYL number of physical cylinders\n" 1710Sstevel@tonic-gate " NCYL number of usable cylinders\n" 1720Sstevel@tonic-gate " ACYL number of alternate cylinders\n" 1730Sstevel@tonic-gate " BCYL cylinder offset\n" 1740Sstevel@tonic-gate " NHEADS number of heads\n" 1750Sstevel@tonic-gate " NSECTORS number of sectors per track\n" 1760Sstevel@tonic-gate " SECTSIZ size of a sector in bytes\n" 1770Sstevel@tonic-gate " -G Write physical geometry to standard output (see -g).\n" 1780Sstevel@tonic-gate " -h Issue this verbose help message.\n" 1790Sstevel@tonic-gate " -o offset\n" 1800Sstevel@tonic-gate " Block offset from start of disk (default 0). Ignored if\n" 1810Sstevel@tonic-gate " -P # specified.\n" 1820Sstevel@tonic-gate " -P fill_patt\n" 1830Sstevel@tonic-gate " Fill disk with pattern fill_patt. fill_patt can be decimal or\n" 1840Sstevel@tonic-gate " hexadecimal and is used as number for constant long word\n" 1850Sstevel@tonic-gate " pattern. If fill_patt is \"#\" then pattern of block #\n" 1860Sstevel@tonic-gate " for each block. Pattern is put in each block as long words\n" 1870Sstevel@tonic-gate " and fills each block (see -o and -s).\n" 1880Sstevel@tonic-gate " -r Read from a disk to stdout (see -o and -s).\n" 1890Sstevel@tonic-gate " -s size Number of blocks on which to perform operation (see -o).\n" 1900Sstevel@tonic-gate " -S geom_file\n" 1910Sstevel@tonic-gate " Use geom_file to set the label geometry (see -g).\n" 1920Sstevel@tonic-gate " -w Write to a disk from stdin (see -o and -s)."; 1930Sstevel@tonic-gate 194251Slclee static char Ostr[] = "Other OS"; 195251Slclee static char Dstr[] = "DOS12"; 196251Slclee static char D16str[] = "DOS16"; 197251Slclee static char DDstr[] = "DOS-DATA"; 198251Slclee static char EDstr[] = "EXT-DOS"; 199251Slclee static char DBstr[] = "DOS-BIG"; 200251Slclee static char PCstr[] = "PCIX"; 201251Slclee static char Ustr[] = "UNIX System"; 202251Slclee static char SUstr[] = "Solaris"; 203251Slclee static char SU2str[] = "Solaris2"; 204251Slclee static char X86str[] = "x86 Boot"; 205251Slclee static char DIAGstr[] = "Diagnostic"; 206251Slclee static char IFSstr[] = "IFS: NTFS"; 207251Slclee static char AIXstr[] = "AIX Boot"; 208251Slclee static char AIXDstr[] = "AIX Data"; 209251Slclee static char OS2str[] = "OS/2 Boot"; 210251Slclee static char WINstr[] = "Win95 FAT32"; 211251Slclee static char EWINstr[] = "Ext Win95"; 212251Slclee static char FAT95str[] = "FAT16 LBA"; 213251Slclee static char EXTLstr[] = "EXT LBA"; 214251Slclee static char LINUXstr[] = "Linux"; 215251Slclee static char CPMstr[] = "CP/M"; 216251Slclee static char NOVstr[] = "Netware 3.x+"; 217251Slclee static char QNXstr[] = "QNX 4.x"; 218251Slclee static char QNX2str[] = "QNX part 2"; 219251Slclee static char QNX3str[] = "QNX part 3"; 220251Slclee static char LINNATstr[] = "Linux native"; 221251Slclee static char NTFSVOL1str[] = "NT volset 1"; 222251Slclee static char NTFSVOL2str[] = "NT volset 2"; 223251Slclee static char BSDstr[] = "BSD OS"; 224251Slclee static char NEXTSTEPstr[] = "NeXTSTEP"; 225251Slclee static char BSDIFSstr[] = "BSDI FS"; 226251Slclee static char BSDISWAPstr[] = "BSDI swap"; 227251Slclee static char Actvstr[] = "Active"; 228251Slclee static char EFIstr[] = "EFI"; 229251Slclee static char NAstr[] = " "; 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* All the user options and flags */ 232251Slclee static char *Dfltdev; /* name of fixed disk drive */ 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* Diagnostic options */ 235251Slclee static int io_wrt = 0; /* write stdin to disk (-w) */ 236251Slclee static int io_rd = 0; /* read disk and write stdout (-r) */ 237251Slclee static char *io_fatt; /* user supplied pattern (-P pattern) */ 238251Slclee static int io_patt = 0; /* write pattern to disk (-P pattern) */ 239251Slclee static int io_lgeom = 0; /* get label geometry (-g) */ 240251Slclee static int io_pgeom = 0; /* get drive physical geometry (-G) */ 241251Slclee static char *io_sgeom = 0; /* set label geometry (-S geom_file) */ 242251Slclee static int io_readonly = 0; /* do not write to disk (-R) */ 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */ 2450Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */ 246251Slclee static int io_offset = 0; /* offset sector (-o offset) */ 247251Slclee static int io_size = 0; /* size in sectors (-s size) */ 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* Partition table flags */ 250251Slclee static int v_flag = 0; /* virtual geometry-HBA flag (-v) */ 251251Slclee static int stdo_flag = 0; /* stdout flag (-W -) */ 252251Slclee static int io_fdisk = 0; /* do fdisk operation */ 253251Slclee static int io_ifdisk = 0; /* interactive partition */ 254251Slclee static int io_nifdisk = 0; /* non-interactive partition (-n) */ 255251Slclee 256251Slclee static int io_adjt = 0; /* check/adjust VTOC (truncate (-t)) */ 257251Slclee static int io_ADJT = 0; /* check/adjust VTOC (delete (-T)) */ 258251Slclee static char *io_ffdisk = 0; /* input fdisk file name (-F file) */ 259251Slclee static char *io_Wfdisk = 0; /* output fdisk file name (-W file) */ 260251Slclee static char *io_Afdisk = 0; /* add entry to partition table (-A) */ 261251Slclee static char *io_Dfdisk = 0; /* delete entry from part. table (-D) */ 262251Slclee 263251Slclee static char *io_mboot = 0; /* master boot record (-b boot_file) */ 264251Slclee 265251Slclee static struct mboot BootCod; /* buffer for master boot record */ 266251Slclee 267251Slclee static int io_wholedisk = 0; /* use whole disk for Solaris (-B) */ 268251Slclee static int io_EFIdisk = 0; /* use whole disk for EFI (-E) */ 269251Slclee static int io_debug = 0; /* activate verbose mode (-d) */ 270251Slclee static int io_image = 0; /* create image using geometry (-I) */ 271251Slclee 272251Slclee static struct mboot *Bootblk; /* pointer to cut/paste sector zero */ 273251Slclee static char *Bootsect; /* pointer to sector zero buffer */ 274251Slclee static char *Nullsect; 275251Slclee static struct vtoc disk_vtoc; /* verify VTOC table */ 276251Slclee static int vt_inval = 0; 277251Slclee static int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */ 278251Slclee static int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */ 279251Slclee 280251Slclee static struct ipart Table[FD_NUMPART]; 281251Slclee static struct ipart Old_Table[FD_NUMPART]; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate /* Disk geometry information */ 284*5169Slclee static struct dk_minfo minfo; 285251Slclee static struct dk_geom disk_geom; 286251Slclee 287*5169Slclee static diskaddr_t dev_capacity; /* number of blocks on device */ 288*5169Slclee static diskaddr_t chs_capacity; /* Numcyl * heads * sectors */ 289*5169Slclee 290251Slclee static int Dev; /* fd for open device */ 2910Sstevel@tonic-gate /* Physical geometry for the drive */ 292251Slclee static int Numcyl; /* number of cylinders */ 293251Slclee static int heads; /* number of heads */ 294251Slclee static int sectors; /* number of sectors per track */ 295251Slclee static int acyl; /* number of alternate sectors */ 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */ 298251Slclee static int hba_Numcyl; /* number of cylinders */ 299251Slclee static int hba_heads; /* number of heads */ 300251Slclee static int hba_sectors; /* number of sectors per track */ 301251Slclee 302251Slclee static int sectsiz; /* sector size */ 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* Load functions for fdisk table modification */ 3050Sstevel@tonic-gate #define LOADFILE 0 /* load fdisk from file */ 3060Sstevel@tonic-gate #define LOADDEL 1 /* delete an fdisk entry */ 3070Sstevel@tonic-gate #define LOADADD 2 /* add an fdisk entry */ 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate #define CBUFLEN 80 310251Slclee static char s[CBUFLEN]; 311251Slclee 312251Slclee static void update_disk_and_exit(boolean_t table_changed); 313251Slclee int main(int argc, char *argv[]); 314251Slclee static int read_geom(char *sgeom); 315251Slclee static void dev_mboot_read(void); 316251Slclee static void dev_mboot_write(int sect, char *buff, int bootsiz); 317251Slclee static void mboot_read(void); 318251Slclee static void fill_patt(void); 319251Slclee static void abs_read(void); 320251Slclee static void abs_write(void); 321251Slclee static void load(int funct, char *file); 322251Slclee static void Set_Table_CHS_Values(int ti); 323251Slclee static int insert_tbl(int id, int act, 324251Slclee int bhead, int bsect, int bcyl, 325251Slclee int ehead, int esect, int ecyl, 326251Slclee int rsect, int numsect); 327251Slclee static int verify_tbl(void); 328251Slclee static int pars_fdisk(char *line, 329251Slclee int *id, int *act, 330251Slclee int *bhead, int *bsect, int *bcyl, 331251Slclee int *ehead, int *esect, int *ecyl, 332251Slclee int *rsect, int *numsect); 333251Slclee static int validate_part(int id, int rsect, int numsect); 334251Slclee static void stage0(void); 335251Slclee static int pcreate(void); 336251Slclee static int specify(uchar_t tsystid); 337251Slclee static void dispmenu(void); 338251Slclee static int pchange(void); 339251Slclee static int ppartid(void); 340251Slclee static char pdelete(void); 341251Slclee static void rm_blanks(char *s); 342251Slclee static int getcyl(void); 343251Slclee static void disptbl(void); 344251Slclee static void print_Table(void); 345251Slclee static void copy_Table_to_Old_Table(void); 346251Slclee static void nulltbl(void); 347251Slclee static void copy_Bootblk_to_Table(void); 348251Slclee static void fill_ipart(char *bootptr, struct ipart *partp); 349251Slclee #ifdef sparc 350251Slclee uchar_t getbyte(char **bp); 351251Slclee uint32_t getlong(char **bp); 352251Slclee #endif 353251Slclee static void copy_Table_to_Bootblk(void); 354251Slclee static int TableChanged(void); 355251Slclee static void ffile_write(char *file); 356251Slclee static void fix_slice(void); 357251Slclee static int yesno(void); 358251Slclee static int readvtoc(void); 359251Slclee static int writevtoc(void); 360251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 361251Slclee static int clear_efi(void); 362251Slclee static void clear_vtoc(int table, int part); 363251Slclee static int lecture_and_query(char *warning, char *devname); 364251Slclee static void sanity_check_provided_device(char *devname, int fd); 365251Slclee static char *get_node(char *devname); 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate static void 3680Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed) 3690Sstevel@tonic-gate { 3700Sstevel@tonic-gate if (table_changed) { 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Copy the new table back to the sector buffer 3730Sstevel@tonic-gate * and write it to disk 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate copy_Table_to_Bootblk(); 3760Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 3770Sstevel@tonic-gate } 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate /* If the VTOC table is wrong fix it (truncation only) */ 3800Sstevel@tonic-gate if (io_adjt) 3810Sstevel@tonic-gate fix_slice(); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate exit(0); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * main 3900Sstevel@tonic-gate * Process command-line options. 3910Sstevel@tonic-gate */ 392251Slclee int 3930Sstevel@tonic-gate main(int argc, char *argv[]) 3940Sstevel@tonic-gate { 395251Slclee int c, i; 3960Sstevel@tonic-gate extern int optind; 3970Sstevel@tonic-gate extern char *optarg; 3980Sstevel@tonic-gate int errflg = 0; 3990Sstevel@tonic-gate int diag_cnt = 0; 4000Sstevel@tonic-gate int openmode; 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate setbuf(stderr, 0); /* so all output gets out on exit */ 4030Sstevel@tonic-gate setbuf(stdout, 0); 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* Process the options. */ 4060Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE")) 4070Sstevel@tonic-gate != EOF) { 4080Sstevel@tonic-gate switch (c) { 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate case 'o': 4110Sstevel@tonic-gate io_offset = strtoul(optarg, 0, 0); 4120Sstevel@tonic-gate continue; 4130Sstevel@tonic-gate case 's': 4140Sstevel@tonic-gate io_size = strtoul(optarg, 0, 0); 4150Sstevel@tonic-gate continue; 4160Sstevel@tonic-gate case 'P': 4170Sstevel@tonic-gate diag_cnt++; 4180Sstevel@tonic-gate io_patt++; 4190Sstevel@tonic-gate io_fatt = optarg; 4200Sstevel@tonic-gate continue; 4210Sstevel@tonic-gate case 'w': 4220Sstevel@tonic-gate diag_cnt++; 4230Sstevel@tonic-gate io_wrt++; 4240Sstevel@tonic-gate continue; 4250Sstevel@tonic-gate case 'r': 4260Sstevel@tonic-gate diag_cnt++; 4270Sstevel@tonic-gate io_rd++; 4280Sstevel@tonic-gate continue; 4290Sstevel@tonic-gate case 'd': 4300Sstevel@tonic-gate io_debug++; 4310Sstevel@tonic-gate continue; 4320Sstevel@tonic-gate case 'I': 4330Sstevel@tonic-gate io_image++; 4340Sstevel@tonic-gate continue; 4350Sstevel@tonic-gate case 'R': 4360Sstevel@tonic-gate io_readonly++; 4370Sstevel@tonic-gate continue; 4380Sstevel@tonic-gate case 'S': 4390Sstevel@tonic-gate diag_cnt++; 4400Sstevel@tonic-gate io_sgeom = optarg; 4410Sstevel@tonic-gate continue; 4420Sstevel@tonic-gate case 'T': 4430Sstevel@tonic-gate io_ADJT++; 444251Slclee /* FALLTHRU */ 4450Sstevel@tonic-gate case 't': 4460Sstevel@tonic-gate io_adjt++; 4470Sstevel@tonic-gate continue; 4480Sstevel@tonic-gate case 'B': 4490Sstevel@tonic-gate io_wholedisk++; 4500Sstevel@tonic-gate io_fdisk++; 4510Sstevel@tonic-gate continue; 4520Sstevel@tonic-gate case 'E': 4530Sstevel@tonic-gate io_EFIdisk++; 4540Sstevel@tonic-gate io_fdisk++; 4550Sstevel@tonic-gate continue; 4560Sstevel@tonic-gate case 'g': 4570Sstevel@tonic-gate diag_cnt++; 4580Sstevel@tonic-gate io_lgeom++; 4590Sstevel@tonic-gate continue; 4600Sstevel@tonic-gate case 'G': 4610Sstevel@tonic-gate diag_cnt++; 4620Sstevel@tonic-gate io_pgeom++; 4630Sstevel@tonic-gate continue; 4640Sstevel@tonic-gate case 'n': 4650Sstevel@tonic-gate io_nifdisk++; 4660Sstevel@tonic-gate io_fdisk++; 4670Sstevel@tonic-gate continue; 4680Sstevel@tonic-gate case 'F': 4690Sstevel@tonic-gate io_fdisk++; 4700Sstevel@tonic-gate io_ffdisk = optarg; 4710Sstevel@tonic-gate continue; 4720Sstevel@tonic-gate case 'b': 4730Sstevel@tonic-gate io_mboot = optarg; 4740Sstevel@tonic-gate continue; 4750Sstevel@tonic-gate case 'W': 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * If '-' is the -W argument, then write 4780Sstevel@tonic-gate * to standard output, otherwise write 4790Sstevel@tonic-gate * to the specified file. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate if (strncmp(optarg, "-", 1) == 0) 4820Sstevel@tonic-gate stdo_flag = 1; 4830Sstevel@tonic-gate else 4840Sstevel@tonic-gate io_Wfdisk = optarg; 4850Sstevel@tonic-gate io_fdisk++; 4860Sstevel@tonic-gate continue; 4870Sstevel@tonic-gate case 'A': 4880Sstevel@tonic-gate io_fdisk++; 4890Sstevel@tonic-gate io_Afdisk = optarg; 4900Sstevel@tonic-gate continue; 4910Sstevel@tonic-gate case 'D': 4920Sstevel@tonic-gate io_fdisk++; 4930Sstevel@tonic-gate io_Dfdisk = optarg; 4940Sstevel@tonic-gate continue; 4950Sstevel@tonic-gate case 'h': 496251Slclee (void) fprintf(stderr, "%s\n", Usage); 497251Slclee (void) fprintf(stderr, "%s\n", Usage1); 4980Sstevel@tonic-gate exit(0); 499251Slclee /* FALLTHRU */ 5000Sstevel@tonic-gate case 'v': 5010Sstevel@tonic-gate v_flag = 1; 5020Sstevel@tonic-gate continue; 5030Sstevel@tonic-gate case '?': 5040Sstevel@tonic-gate errflg++; 5050Sstevel@tonic-gate break; 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate break; 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate if (io_image && io_sgeom && diag_cnt == 1) { 5110Sstevel@tonic-gate diag_cnt = 0; 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate /* User option checking */ 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* By default, run in interactive mode */ 5170Sstevel@tonic-gate if (!io_fdisk && !diag_cnt && !io_nifdisk) { 5180Sstevel@tonic-gate io_ifdisk++; 5190Sstevel@tonic-gate io_fdisk++; 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) { 5220Sstevel@tonic-gate errflg++; 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate /* Was any error detected? */ 5260Sstevel@tonic-gate if (errflg || argc == optind) { 527251Slclee (void) fprintf(stderr, "%s\n", Usage); 528251Slclee (void) fprintf(stderr, 5290Sstevel@tonic-gate "\nDetailed help is available with the -h option.\n"); 5300Sstevel@tonic-gate exit(2); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate /* Figure out the correct device node to open */ 5350Sstevel@tonic-gate Dfltdev = get_node(argv[optind]); 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate if (io_readonly) 5380Sstevel@tonic-gate openmode = O_RDONLY; 5390Sstevel@tonic-gate else 5400Sstevel@tonic-gate openmode = O_RDWR|O_CREAT; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate if ((Dev = open(Dfltdev, openmode, 0666)) == -1) { 543251Slclee (void) fprintf(stderr, 544251Slclee "fdisk: Cannot open device %s.\n", 545251Slclee Dfltdev); 5460Sstevel@tonic-gate exit(1); 5470Sstevel@tonic-gate } 548*5169Slclee /* 549*5169Slclee * not all disk (or disklike) drivers support DKIOCGMEDIAINFO 550*5169Slclee * in that case leave the minfo structure zeroed 551*5169Slclee */ 552*5169Slclee if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) { 553*5169Slclee memset(&minfo, 0, sizeof (minfo)); 554*5169Slclee } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate /* Get the disk geometry */ 5570Sstevel@tonic-gate if (!io_image) { 5580Sstevel@tonic-gate /* Get disk's HBA (virtual) geometry */ 5590Sstevel@tonic-gate errno = 0; 5600Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) { 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate /* 5630Sstevel@tonic-gate * If ioctl isn't implemented on this platform, then 5640Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v), 5650Sstevel@tonic-gate * otherwise use the virtual geometry. 5660Sstevel@tonic-gate */ 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate if (errno == ENOTTY) { 5690Sstevel@tonic-gate v_flag = 0; 5700Sstevel@tonic-gate no_virtgeom_ioctl = 1; 5710Sstevel@tonic-gate } else if (errno == EINVAL) { 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * This means that the ioctl exists, but 5740Sstevel@tonic-gate * is invalid for this disk, meaning the 5750Sstevel@tonic-gate * disk doesn't have an HBA geometry 5760Sstevel@tonic-gate * (like, say, it's larger than 8GB). 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate v_flag = 0; 5790Sstevel@tonic-gate hba_Numcyl = hba_heads = hba_sectors = 0; 5800Sstevel@tonic-gate } else { 5810Sstevel@tonic-gate (void) fprintf(stderr, 5820Sstevel@tonic-gate "%s: Cannot get virtual disk geometry.\n", 5830Sstevel@tonic-gate argv[optind]); 5840Sstevel@tonic-gate exit(1); 5850Sstevel@tonic-gate } 5860Sstevel@tonic-gate } else { 5870Sstevel@tonic-gate /* save virtual geometry values obtained by ioctl */ 5880Sstevel@tonic-gate hba_Numcyl = disk_geom.dkg_ncyl; 5890Sstevel@tonic-gate hba_heads = disk_geom.dkg_nhead; 5900Sstevel@tonic-gate hba_sectors = disk_geom.dkg_nsect; 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate errno = 0; 5940Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 5950Sstevel@tonic-gate if (errno == ENOTTY) { 5960Sstevel@tonic-gate no_physgeom_ioctl = 1; 5970Sstevel@tonic-gate } else { 5980Sstevel@tonic-gate (void) fprintf(stderr, 5990Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 6000Sstevel@tonic-gate argv[optind]); 6010Sstevel@tonic-gate exit(1); 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate /* 6060Sstevel@tonic-gate * Call DKIOCGGEOM if the ioctls for physical and virtual 6070Sstevel@tonic-gate * geometry fail. Get both from this generic call. 6080Sstevel@tonic-gate */ 6090Sstevel@tonic-gate if (no_virtgeom_ioctl && no_physgeom_ioctl) { 6100Sstevel@tonic-gate errno = 0; 6110Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6120Sstevel@tonic-gate (void) fprintf(stderr, 6130Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6140Sstevel@tonic-gate argv[optind]); 6150Sstevel@tonic-gate exit(1); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate } 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6200Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6210Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 6220Sstevel@tonic-gate sectsiz = 512; 6230Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /* 6260Sstevel@tonic-gate * if hba geometry was not set by DKIOC_VIRTGEOM 6270Sstevel@tonic-gate * or we got an invalid hba geometry 6280Sstevel@tonic-gate * then set hba geometry based on max values 6290Sstevel@tonic-gate */ 6300Sstevel@tonic-gate if (no_virtgeom_ioctl || 631251Slclee disk_geom.dkg_ncyl == 0 || 632251Slclee disk_geom.dkg_nhead == 0 || 633251Slclee disk_geom.dkg_nsect == 0 || 6340Sstevel@tonic-gate disk_geom.dkg_ncyl > MAX_CYL || 6350Sstevel@tonic-gate disk_geom.dkg_nhead > MAX_HEAD || 6360Sstevel@tonic-gate disk_geom.dkg_nsect > MAX_SECT) { 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate /* 6390Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v) 6400Sstevel@tonic-gate */ 6410Sstevel@tonic-gate v_flag = 0; 6420Sstevel@tonic-gate hba_sectors = MAX_SECT; 6430Sstevel@tonic-gate hba_heads = MAX_HEAD + 1; 6440Sstevel@tonic-gate hba_Numcyl = (Numcyl * heads * sectors) / 6450Sstevel@tonic-gate (hba_sectors * hba_heads); 6460Sstevel@tonic-gate } 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate if (io_debug) { 649251Slclee (void) fprintf(stderr, "Physical Geometry:\n"); 650251Slclee (void) fprintf(stderr, 6510Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6520Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6530Sstevel@tonic-gate Numcyl, 6540Sstevel@tonic-gate heads, 6550Sstevel@tonic-gate sectors, 6560Sstevel@tonic-gate sectsiz, 657251Slclee Numcyl * heads * sectors, 658251Slclee (Numcyl * heads * sectors * sectsiz) / 1048576); 659251Slclee (void) fprintf(stderr, "Virtual (HBA) Geometry:\n"); 660251Slclee (void) fprintf(stderr, 6610Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6620Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6630Sstevel@tonic-gate hba_Numcyl, 6640Sstevel@tonic-gate hba_heads, 6650Sstevel@tonic-gate hba_sectors, 6660Sstevel@tonic-gate sectsiz, 667251Slclee hba_Numcyl * hba_heads * hba_sectors, 668251Slclee (hba_Numcyl * hba_heads * hba_sectors * sectsiz) / 669251Slclee 1048576); 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* If user has requested a geometry report just do it and exit */ 6740Sstevel@tonic-gate if (io_lgeom) { 6750Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6760Sstevel@tonic-gate (void) fprintf(stderr, 6770Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6780Sstevel@tonic-gate argv[optind]); 6790Sstevel@tonic-gate exit(1); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6820Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6830Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 6840Sstevel@tonic-gate sectsiz = 512; 6850Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 686251Slclee (void) printf("* Label geometry for device %s\n", Dfltdev); 687251Slclee (void) printf( 688251Slclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 6890Sstevel@tonic-gate " SECSIZ\n"); 690251Slclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 6910Sstevel@tonic-gate Numcyl, 6920Sstevel@tonic-gate disk_geom.dkg_ncyl, 6930Sstevel@tonic-gate disk_geom.dkg_acyl, 6940Sstevel@tonic-gate disk_geom.dkg_bcyl, 6950Sstevel@tonic-gate heads, 6960Sstevel@tonic-gate sectors, 6970Sstevel@tonic-gate sectsiz); 6980Sstevel@tonic-gate exit(0); 6990Sstevel@tonic-gate } else if (io_pgeom) { 7000Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 7010Sstevel@tonic-gate (void) fprintf(stderr, 7020Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 7030Sstevel@tonic-gate argv[optind]); 7040Sstevel@tonic-gate exit(1); 7050Sstevel@tonic-gate } 706251Slclee (void) printf("* Physical geometry for device %s\n", Dfltdev); 707251Slclee (void) printf( 708251Slclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 7090Sstevel@tonic-gate " SECSIZ\n"); 710251Slclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 7110Sstevel@tonic-gate disk_geom.dkg_pcyl, 7120Sstevel@tonic-gate disk_geom.dkg_ncyl, 7130Sstevel@tonic-gate disk_geom.dkg_acyl, 7140Sstevel@tonic-gate disk_geom.dkg_bcyl, 7150Sstevel@tonic-gate disk_geom.dkg_nhead, 7160Sstevel@tonic-gate disk_geom.dkg_nsect, 7170Sstevel@tonic-gate sectsiz); 7180Sstevel@tonic-gate exit(0); 7190Sstevel@tonic-gate } else if (io_sgeom) { 7200Sstevel@tonic-gate if (read_geom(io_sgeom)) { 7210Sstevel@tonic-gate exit(1); 7220Sstevel@tonic-gate } else if (!io_image) { 7230Sstevel@tonic-gate exit(0); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 727*5169Slclee /* 728*5169Slclee * some drivers may not support DKIOCGMEDIAINFO 729*5169Slclee * in that case use CHS 730*5169Slclee */ 731*5169Slclee chs_capacity = Numcyl * heads * sectors; 732*5169Slclee dev_capacity = chs_capacity; 733*5169Slclee if (minfo.dki_capacity > 0) 734*5169Slclee dev_capacity = minfo.dki_capacity; 735*5169Slclee 7360Sstevel@tonic-gate /* Allocate memory to hold three complete sectors */ 7370Sstevel@tonic-gate Bootsect = (char *)malloc(3 * sectsiz); 7380Sstevel@tonic-gate if (Bootsect == NULL) { 739251Slclee (void) fprintf(stderr, 7400Sstevel@tonic-gate "fdisk: Unable to obtain enough buffer memory" 7410Sstevel@tonic-gate " (%d bytes).\n", 742251Slclee 3 * sectsiz); 7430Sstevel@tonic-gate exit(1); 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate Nullsect = Bootsect + sectsiz; 7470Sstevel@tonic-gate /* Zero out the "NULL" sector */ 7480Sstevel@tonic-gate for (i = 0; i < sectsiz; i++) { 7490Sstevel@tonic-gate Nullsect[i] = 0; 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate /* Find out what the user wants done */ 7530Sstevel@tonic-gate if (io_rd) { /* abs disk read */ 7540Sstevel@tonic-gate abs_read(); /* will not return */ 7550Sstevel@tonic-gate } else if (io_wrt && !io_readonly) { 7560Sstevel@tonic-gate abs_write(); /* will not return */ 7570Sstevel@tonic-gate } else if (io_patt && !io_readonly) { 7580Sstevel@tonic-gate fill_patt(); /* will not return */ 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate /* This is the fdisk edit, the real reason for the program. */ 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate sanity_check_provided_device(Dfltdev, Dev); 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate /* Get the new BOOT program in case we write a new fdisk table */ 7670Sstevel@tonic-gate mboot_read(); 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate /* Read from disk master boot */ 7700Sstevel@tonic-gate dev_mboot_read(); 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * Verify and copy the device's fdisk table. This will be used 7740Sstevel@tonic-gate * as the prototype mboot if the device's mboot looks invalid. 7750Sstevel@tonic-gate */ 7760Sstevel@tonic-gate Bootblk = (struct mboot *)Bootsect; 7770Sstevel@tonic-gate copy_Bootblk_to_Table(); 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /* save away a copy of Table in Old_Table for sensing changes */ 7800Sstevel@tonic-gate copy_Table_to_Old_Table(); 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate /* Load fdisk table from specified file (-F fdisk_file) */ 7830Sstevel@tonic-gate if (io_ffdisk) { 7840Sstevel@tonic-gate /* Load and verify user-specified table parameters */ 7850Sstevel@tonic-gate load(LOADFILE, io_ffdisk); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate /* Does user want to delete or add an entry? */ 7890Sstevel@tonic-gate if (io_Dfdisk) { 7900Sstevel@tonic-gate load(LOADDEL, io_Dfdisk); 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate if (io_Afdisk) { 7930Sstevel@tonic-gate load(LOADADD, io_Afdisk); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) { 7970Sstevel@tonic-gate /* Check if there is no fdisk table */ 7980Sstevel@tonic-gate if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) { 7990Sstevel@tonic-gate if (io_ifdisk && !io_wholedisk && !io_EFIdisk) { 800251Slclee (void) printf( 801251Slclee "No fdisk table exists. The default" 802251Slclee " partition for the disk is:\n\n" 803251Slclee " a 100%% \"SOLARIS System\" " 804251Slclee "partition\n\n" 805251Slclee "Type \"y\" to accept the default " 8060Sstevel@tonic-gate "partition, otherwise type \"n\" to " 8070Sstevel@tonic-gate "edit the\n partition table.\n"); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate /* Edit the partition table as directed */ 8110Sstevel@tonic-gate if (io_wholedisk ||(io_ifdisk && yesno())) { 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* Default scenario */ 8140Sstevel@tonic-gate nulltbl(); 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate /* now set up UNIX System partition */ 8170Sstevel@tonic-gate Table[0].bootid = ACTIVE; 8180Sstevel@tonic-gate Table[0].relsect = lel(heads * sectors); 819251Slclee Table[0].numsect = lel((long)((Numcyl - 1) * 8200Sstevel@tonic-gate heads * sectors)); 8210Sstevel@tonic-gate Table[0].systid = SUNIXOS2; /* Solaris */ 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate /* calculate CHS values for table entry 0 */ 8240Sstevel@tonic-gate Set_Table_CHS_Values(0); 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8270Sstevel@tonic-gate } else if (io_EFIdisk) { 8280Sstevel@tonic-gate /* create an EFI partition for the whole disk */ 8290Sstevel@tonic-gate nulltbl(); 8300Sstevel@tonic-gate i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1, 831*5169Slclee dev_capacity - 1); 8320Sstevel@tonic-gate if (i != 0) { 833251Slclee (void) fprintf(stderr, 834251Slclee "Error creating EFI partition\n"); 8350Sstevel@tonic-gate exit(1); 8360Sstevel@tonic-gate } 8370Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate /* Display complete fdisk table entries for debugging purposes */ 8430Sstevel@tonic-gate if (io_debug) { 844251Slclee (void) fprintf(stderr, "Partition Table Entry Values:\n"); 8450Sstevel@tonic-gate print_Table(); 8460Sstevel@tonic-gate if (io_ifdisk) { 847251Slclee (void) fprintf(stderr, "\n"); 848251Slclee (void) fprintf(stderr, "Press Enter to continue.\n"); 849251Slclee (void) gets(s); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate /* Interactive fdisk mode */ 8540Sstevel@tonic-gate if (io_ifdisk) { 855251Slclee (void) printf(CLR_SCR); 8560Sstevel@tonic-gate disptbl(); 857251Slclee for (;;) { 858251Slclee stage0(); 8590Sstevel@tonic-gate copy_Bootblk_to_Table(); 8600Sstevel@tonic-gate disptbl(); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* If user wants to write the table to a file, do it */ 8650Sstevel@tonic-gate if (io_Wfdisk) 8660Sstevel@tonic-gate ffile_write(io_Wfdisk); 8670Sstevel@tonic-gate else if (stdo_flag) 8680Sstevel@tonic-gate ffile_write((char *)stdout); 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate update_disk_and_exit(TableChanged() == 1); 871251Slclee return (0); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate /* 8750Sstevel@tonic-gate * read_geom 8760Sstevel@tonic-gate * Read geometry from specified file (-S). 8770Sstevel@tonic-gate */ 8780Sstevel@tonic-gate 879251Slclee static int 880251Slclee read_geom(char *sgeom) 8810Sstevel@tonic-gate { 8820Sstevel@tonic-gate char line[256]; 8830Sstevel@tonic-gate FILE *fp; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate /* open the prototype file */ 8860Sstevel@tonic-gate if ((fp = fopen(sgeom, "r")) == NULL) { 8870Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot open file %s.\n", 8880Sstevel@tonic-gate io_sgeom); 8890Sstevel@tonic-gate return (1); 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* Read a line from the file */ 8930Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 8940Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 8950Sstevel@tonic-gate continue; 8960Sstevel@tonic-gate else { 8970Sstevel@tonic-gate line[strlen(line)] = '\0'; 898251Slclee if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d", 8990Sstevel@tonic-gate &disk_geom.dkg_pcyl, 9000Sstevel@tonic-gate &disk_geom.dkg_ncyl, 9010Sstevel@tonic-gate &disk_geom.dkg_acyl, 9020Sstevel@tonic-gate &disk_geom.dkg_bcyl, 9030Sstevel@tonic-gate &disk_geom.dkg_nhead, 9040Sstevel@tonic-gate &disk_geom.dkg_nsect, 9050Sstevel@tonic-gate §siz) != 7) { 9060Sstevel@tonic-gate (void) fprintf(stderr, 9070Sstevel@tonic-gate "Syntax error:\n \"%s\".\n", 9080Sstevel@tonic-gate line); 9090Sstevel@tonic-gate return (1); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate break; 9120Sstevel@tonic-gate } /* else */ 9130Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (!io_image) { 9160Sstevel@tonic-gate if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) { 9170Sstevel@tonic-gate (void) fprintf(stderr, 9180Sstevel@tonic-gate "fdisk: Cannot set label geometry.\n"); 9190Sstevel@tonic-gate return (1); 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate } else { 9220Sstevel@tonic-gate Numcyl = hba_Numcyl = disk_geom.dkg_ncyl; 9230Sstevel@tonic-gate heads = hba_heads = disk_geom.dkg_nhead; 9240Sstevel@tonic-gate sectors = hba_sectors = disk_geom.dkg_nsect; 9250Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 9260Sstevel@tonic-gate } 9270Sstevel@tonic-gate 928251Slclee (void) fclose(fp); 9290Sstevel@tonic-gate return (0); 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * dev_mboot_read 9340Sstevel@tonic-gate * Read the master boot sector from the device. 9350Sstevel@tonic-gate */ 936251Slclee static void 937251Slclee dev_mboot_read(void) 9380Sstevel@tonic-gate { 9390Sstevel@tonic-gate if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) { 9400Sstevel@tonic-gate perror("Error in ioctl DKIOCGMBOOT"); 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate if (errno == 0) 9430Sstevel@tonic-gate return; 9440Sstevel@tonic-gate if (lseek(Dev, 0, SEEK_SET) == -1) { 945251Slclee (void) fprintf(stderr, 9460Sstevel@tonic-gate "fdisk: Error seeking to partition table on %s.\n", 9470Sstevel@tonic-gate Dfltdev); 9480Sstevel@tonic-gate if (!io_image) 9490Sstevel@tonic-gate exit(1); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 952251Slclee (void) fprintf(stderr, 9530Sstevel@tonic-gate "fdisk: Error reading partition table from %s.\n", 9540Sstevel@tonic-gate Dfltdev); 9550Sstevel@tonic-gate if (!io_image) 9560Sstevel@tonic-gate exit(1); 9570Sstevel@tonic-gate } 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * dev_mboot_write 9620Sstevel@tonic-gate * Write the master boot sector to the device. 9630Sstevel@tonic-gate */ 964251Slclee static void 9650Sstevel@tonic-gate dev_mboot_write(int sect, char *buff, int bootsiz) 9660Sstevel@tonic-gate { 9670Sstevel@tonic-gate int new_pt, old_pt, error; 9680Sstevel@tonic-gate int clr_efi = -1, old_solaris = -1, new_solaris = -1; 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate if (io_readonly) 971251Slclee return; 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate if (io_debug) { 974251Slclee (void) fprintf(stderr, "About to write fdisk table:\n"); 9750Sstevel@tonic-gate print_Table(); 9760Sstevel@tonic-gate if (io_ifdisk) { 977251Slclee (void) fprintf(stderr, "Press Enter to continue.\n"); 978251Slclee (void) gets(s); 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate /* see if the old table had EFI or Solaris partitions */ 9830Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 9840Sstevel@tonic-gate if (Old_Table[old_pt].systid == SUNIXOS || 9850Sstevel@tonic-gate Old_Table[old_pt].systid == SUNIXOS2) { 9860Sstevel@tonic-gate old_solaris = old_pt; 9870Sstevel@tonic-gate } else if (Old_Table[old_pt].systid == EFI_PMBR) { 9880Sstevel@tonic-gate clr_efi = old_pt; 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate /* look to see if Solaris partition changed in relsect/numsect */ 9930Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * if this is not a Solaris partition, ignore it 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate if (Table[new_pt].systid != SUNIXOS && 9990Sstevel@tonic-gate Table[new_pt].systid != SUNIXOS2) 10000Sstevel@tonic-gate continue; 10010Sstevel@tonic-gate 10020Sstevel@tonic-gate /* 10030Sstevel@tonic-gate * if there was no previous Solaris partition 10040Sstevel@tonic-gate * or if the old partition was in a different place 10050Sstevel@tonic-gate * or if the old partition was a different size 10060Sstevel@tonic-gate * then this must be a new Solaris partition 10070Sstevel@tonic-gate */ 10080Sstevel@tonic-gate if (old_solaris == -1 || 10090Sstevel@tonic-gate Old_Table[old_solaris].relsect != Table[new_pt].relsect || 10100Sstevel@tonic-gate Old_Table[old_solaris].numsect != Table[new_pt].numsect) { 10110Sstevel@tonic-gate new_solaris = new_pt; 10120Sstevel@tonic-gate break; 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate /* look to see if a EFI partition changed in relsect/numsect */ 10170Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 10180Sstevel@tonic-gate if (Table[new_pt].systid != EFI_PMBR) 10190Sstevel@tonic-gate continue; 10200Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 1021*5169Slclee if ((Old_Table[old_pt].systid == 1022*5169Slclee Table[new_pt].systid) && 1023*5169Slclee (Old_Table[old_pt].relsect == 1024*5169Slclee Table[new_pt].relsect) && 1025*5169Slclee (Old_Table[old_pt].numsect == 1026*5169Slclee Table[new_pt].numsect)) 1027*5169Slclee break; 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate /* 10310Sstevel@tonic-gate * if EFI partition changed, set the flag to clear 10320Sstevel@tonic-gate * the EFI GPT 10330Sstevel@tonic-gate */ 10340Sstevel@tonic-gate if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) { 10350Sstevel@tonic-gate clr_efi = 0; 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate break; 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate /* clear labels if necessary */ 10410Sstevel@tonic-gate if (clr_efi >= 0) { 10420Sstevel@tonic-gate if (io_debug) { 1043251Slclee (void) fprintf(stderr, "Clearing EFI labels\n"); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate if ((error = clear_efi()) != 0) { 10460Sstevel@tonic-gate if (io_debug) { 1047251Slclee (void) fprintf(stderr, 1048251Slclee "\tError %d clearing EFI labels" 10490Sstevel@tonic-gate " (probably no EFI labels exist)\n", 10500Sstevel@tonic-gate error); 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate } 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate if (new_solaris >= 0) { 10560Sstevel@tonic-gate if (io_debug) { 1057251Slclee (void) fprintf(stderr, "Clearing VTOC labels from NEW" 10580Sstevel@tonic-gate " table\n"); 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate clear_vtoc(NEW, new_solaris); 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) { 1064251Slclee (void) fprintf(stderr, 10650Sstevel@tonic-gate "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n", 10660Sstevel@tonic-gate Dfltdev); 10670Sstevel@tonic-gate } 10680Sstevel@tonic-gate if (errno == 0) 10690Sstevel@tonic-gate return; 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /* write to disk drive */ 10720Sstevel@tonic-gate if (lseek(Dev, sect, SEEK_SET) == -1) { 1073251Slclee (void) fprintf(stderr, 10740Sstevel@tonic-gate "fdisk: Error seeking to master boot record on %s.\n", 10750Sstevel@tonic-gate Dfltdev); 10760Sstevel@tonic-gate exit(1); 10770Sstevel@tonic-gate } 10780Sstevel@tonic-gate if (write(Dev, buff, bootsiz) != bootsiz) { 1079251Slclee (void) fprintf(stderr, 10800Sstevel@tonic-gate "fdisk: Error writing master boot record to %s.\n", 10810Sstevel@tonic-gate Dfltdev); 10820Sstevel@tonic-gate exit(1); 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate /* 10870Sstevel@tonic-gate * mboot_read 10880Sstevel@tonic-gate * Read the prototype boot records from the files. 10890Sstevel@tonic-gate */ 1090251Slclee static void 1091251Slclee mboot_read(void) 10920Sstevel@tonic-gate { 10930Sstevel@tonic-gate int mDev, i; 10940Sstevel@tonic-gate struct ipart *part; 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate #if defined(i386) || defined(sparc) 10970Sstevel@tonic-gate /* 10980Sstevel@tonic-gate * If the master boot file hasn't been specified, use the 10990Sstevel@tonic-gate * implementation architecture name to generate the default one. 11000Sstevel@tonic-gate */ 11010Sstevel@tonic-gate if (io_mboot == (char *)0) { 11020Sstevel@tonic-gate /* 11030Sstevel@tonic-gate * Bug ID 1249035: 11040Sstevel@tonic-gate * The mboot file must be delivered on all platforms 11050Sstevel@tonic-gate * and installed in a non-platform-dependent 11060Sstevel@tonic-gate * directory; i.e., /usr/lib/fs/ufs. 11070Sstevel@tonic-gate */ 11080Sstevel@tonic-gate io_mboot = "/usr/lib/fs/ufs/mboot"; 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate /* First read in the master boot record */ 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate /* Open the master boot proto file */ 11140Sstevel@tonic-gate if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) { 1115251Slclee (void) fprintf(stderr, 11160Sstevel@tonic-gate "fdisk: Cannot open master boot file %s.\n", 11170Sstevel@tonic-gate io_mboot); 11180Sstevel@tonic-gate exit(1); 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate /* Read the master boot program */ 11220Sstevel@tonic-gate if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof 11230Sstevel@tonic-gate (struct mboot)) { 1124251Slclee (void) fprintf(stderr, 11250Sstevel@tonic-gate "fdisk: Cannot read master boot file %s.\n", 11260Sstevel@tonic-gate io_mboot); 11270Sstevel@tonic-gate exit(1); 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate /* Is this really a master boot record? */ 11310Sstevel@tonic-gate if (les(BootCod.signature) != MBB_MAGIC) { 1132251Slclee (void) fprintf(stderr, 11330Sstevel@tonic-gate "fdisk: Invalid master boot file %s.\n", io_mboot); 1134251Slclee (void) fprintf(stderr, 1135251Slclee "Bad magic number: is %x, but should be %x.\n", 11360Sstevel@tonic-gate les(BootCod.signature), MBB_MAGIC); 11370Sstevel@tonic-gate exit(1); 11380Sstevel@tonic-gate } 11390Sstevel@tonic-gate 1140251Slclee (void) close(mDev); 11410Sstevel@tonic-gate #else 11420Sstevel@tonic-gate #error fdisk needs to be ported to new architecture 11430Sstevel@tonic-gate #endif 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /* Zero out the partitions part of this record */ 11460Sstevel@tonic-gate part = (struct ipart *)BootCod.parts; 11470Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++, part++) { 1148251Slclee (void) memset(part, 0, sizeof (struct ipart)); 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate /* 11540Sstevel@tonic-gate * fill_patt 11550Sstevel@tonic-gate * Fill the disk with user/sector number pattern. 11560Sstevel@tonic-gate */ 1157251Slclee static void 1158251Slclee fill_patt(void) 11590Sstevel@tonic-gate { 1160251Slclee int *buff_ptr, i; 11610Sstevel@tonic-gate int io_fpatt = 0; 11620Sstevel@tonic-gate int io_ipatt = 0; 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate if (strncmp(io_fatt, "#", 1) != 0) { 11650Sstevel@tonic-gate io_fpatt++; 11660Sstevel@tonic-gate io_ipatt = strtoul(io_fatt, 0, 0); 11670Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 11680Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 1169*5169Slclee *buff_ptr = io_ipatt; 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * Fill disk with pattern based on block number. 11740Sstevel@tonic-gate * Write to the disk at absolute relative block io_offset 11750Sstevel@tonic-gate * for io_size blocks. 11760Sstevel@tonic-gate */ 11770Sstevel@tonic-gate while (io_size--) { 11780Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 11790Sstevel@tonic-gate if (!io_fpatt) { 11800Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 11810Sstevel@tonic-gate *buff_ptr = io_offset; 11820Sstevel@tonic-gate } 11830Sstevel@tonic-gate /* Write the data to disk */ 11840Sstevel@tonic-gate if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) { 1185251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 1186*5169Slclee Dfltdev); 11870Sstevel@tonic-gate exit(1); 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1190251Slclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 1191*5169Slclee Dfltdev); 11920Sstevel@tonic-gate exit(1); 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate } /* while (--io_size); */ 11950Sstevel@tonic-gate } 11960Sstevel@tonic-gate 11970Sstevel@tonic-gate /* 11980Sstevel@tonic-gate * abs_read 11990Sstevel@tonic-gate * Read from the disk at absolute relative block io_offset for 12000Sstevel@tonic-gate * io_size blocks. Write the data to standard ouput (-r). 12010Sstevel@tonic-gate */ 1202251Slclee static void 1203251Slclee abs_read(void) 1204251Slclee { 12050Sstevel@tonic-gate int c; 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate while (io_size--) { 12080Sstevel@tonic-gate if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) { 1209251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12100Sstevel@tonic-gate Dfltdev); 12110Sstevel@tonic-gate exit(1); 12120Sstevel@tonic-gate } 12130Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 1214251Slclee (void) fprintf(stderr, "fdisk: Error reading %s.\n", 12150Sstevel@tonic-gate Dfltdev); 12160Sstevel@tonic-gate exit(1); 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate /* Write to standard ouptut */ 1220251Slclee if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12210Sstevel@tonic-gate if (c >= 0) { 12220Sstevel@tonic-gate if (io_debug) 1223251Slclee (void) fprintf(stderr, 1224251Slclee "fdisk: Output warning: %d of %d" 1225251Slclee " characters written.\n", 1226251Slclee c, sectsiz); 12270Sstevel@tonic-gate exit(2); 12280Sstevel@tonic-gate } else { 12290Sstevel@tonic-gate perror("write error on output file."); 12300Sstevel@tonic-gate exit(2); 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate } /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */ 12330Sstevel@tonic-gate /* != sectsiz) */ 12340Sstevel@tonic-gate } /* while (--io_size); */ 12350Sstevel@tonic-gate exit(0); 12360Sstevel@tonic-gate } 12370Sstevel@tonic-gate 12380Sstevel@tonic-gate /* 12390Sstevel@tonic-gate * abs_write 12400Sstevel@tonic-gate * Read the data from standard input. Write to the disk at 12410Sstevel@tonic-gate * absolute relative block io_offset for io_size blocks (-w). 12420Sstevel@tonic-gate */ 1243251Slclee static void 1244251Slclee abs_write(void) 12450Sstevel@tonic-gate { 12460Sstevel@tonic-gate int c, i; 12470Sstevel@tonic-gate 12480Sstevel@tonic-gate while (io_size--) { 12490Sstevel@tonic-gate int part_exit = 0; 12500Sstevel@tonic-gate /* Read from standard input */ 12510Sstevel@tonic-gate if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12520Sstevel@tonic-gate if (c >= 0) { 12530Sstevel@tonic-gate if (io_debug) 1254251Slclee (void) fprintf(stderr, 12550Sstevel@tonic-gate "fdisk: WARNING: Incomplete read (%d of" 12560Sstevel@tonic-gate " %d characters read) on input file.\n", 1257*5169Slclee c, sectsiz); 12580Sstevel@tonic-gate /* Fill pattern to mark partial sector in buf */ 12590Sstevel@tonic-gate for (i = c; i < sectsiz; ) { 12600Sstevel@tonic-gate Bootsect[i++] = 0x41; 12610Sstevel@tonic-gate Bootsect[i++] = 0x62; 12620Sstevel@tonic-gate Bootsect[i++] = 0x65; 12630Sstevel@tonic-gate Bootsect[i++] = 0; 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate part_exit++; 12660Sstevel@tonic-gate } else { 12670Sstevel@tonic-gate perror("read error on input file."); 12680Sstevel@tonic-gate exit(2); 12690Sstevel@tonic-gate } 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate } 12720Sstevel@tonic-gate /* Write to disk drive */ 12730Sstevel@tonic-gate if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) { 1274251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12750Sstevel@tonic-gate Dfltdev); 12760Sstevel@tonic-gate exit(1); 12770Sstevel@tonic-gate } 12780Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1279251Slclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 12800Sstevel@tonic-gate Dfltdev); 12810Sstevel@tonic-gate exit(1); 12820Sstevel@tonic-gate } 12830Sstevel@tonic-gate if (part_exit) 12840Sstevel@tonic-gate exit(0); 12850Sstevel@tonic-gate } /* while (--io_size); */ 12860Sstevel@tonic-gate exit(1); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate 1289251Slclee 12900Sstevel@tonic-gate /* 12910Sstevel@tonic-gate * load 12920Sstevel@tonic-gate * Load will either read the fdisk table from a file or add or 12930Sstevel@tonic-gate * delete an entry (-A, -D, -F). 12940Sstevel@tonic-gate */ 12950Sstevel@tonic-gate 1296251Slclee static void 1297251Slclee load(int funct, char *file) 12980Sstevel@tonic-gate { 12990Sstevel@tonic-gate int id; 13000Sstevel@tonic-gate int act; 13010Sstevel@tonic-gate int bhead; 13020Sstevel@tonic-gate int bsect; 13030Sstevel@tonic-gate int bcyl; 13040Sstevel@tonic-gate int ehead; 13050Sstevel@tonic-gate int esect; 13060Sstevel@tonic-gate int ecyl; 13070Sstevel@tonic-gate int rsect; 13080Sstevel@tonic-gate int numsect; 13090Sstevel@tonic-gate char line[256]; 13100Sstevel@tonic-gate int i = 0; 13110Sstevel@tonic-gate int j; 13120Sstevel@tonic-gate FILE *fp; 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate switch (funct) { 13150Sstevel@tonic-gate 1316*5169Slclee case LOADFILE: 13170Sstevel@tonic-gate 13180Sstevel@tonic-gate /* 13190Sstevel@tonic-gate * Zero out the table before loading it, which will 13200Sstevel@tonic-gate * force it to be updated on disk later (-F 13210Sstevel@tonic-gate * fdisk_file). 13220Sstevel@tonic-gate */ 13230Sstevel@tonic-gate nulltbl(); 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate /* Open the prototype file */ 13260Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) { 13270Sstevel@tonic-gate (void) fprintf(stderr, 13280Sstevel@tonic-gate "fdisk: Cannot open prototype partition file %s.\n", 13290Sstevel@tonic-gate file); 13300Sstevel@tonic-gate exit(1); 13310Sstevel@tonic-gate } 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate /* Read a line from the file */ 13340Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 13350Sstevel@tonic-gate if (pars_fdisk(line, &id, &act, &bhead, &bsect, 13360Sstevel@tonic-gate &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) { 13370Sstevel@tonic-gate continue; 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate /* 13410Sstevel@tonic-gate * Validate the partition. It cannot start at sector 13420Sstevel@tonic-gate * 0 unless it is UNUSED or already exists 13430Sstevel@tonic-gate */ 13440Sstevel@tonic-gate if (validate_part(id, rsect, numsect) < 0) { 13450Sstevel@tonic-gate (void) fprintf(stderr, 13460Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13470Sstevel@tonic-gate line); 13480Sstevel@tonic-gate exit(1); 13490Sstevel@tonic-gate } 13500Sstevel@tonic-gate /* 13510Sstevel@tonic-gate * Find an unused entry to use and put the entry 13520Sstevel@tonic-gate * in table 13530Sstevel@tonic-gate */ 13540Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, 13550Sstevel@tonic-gate esect, ecyl, rsect, numsect) < 0) { 13560Sstevel@tonic-gate (void) fprintf(stderr, 13570Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13580Sstevel@tonic-gate line); 13590Sstevel@tonic-gate exit(1); 13600Sstevel@tonic-gate } 13610Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate if (verify_tbl() < 0) { 1364251Slclee (void) fprintf(stderr, 13650Sstevel@tonic-gate "fdisk: Cannot create partition table\n"); 13660Sstevel@tonic-gate exit(1); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate 1369251Slclee (void) fclose(fp); 13700Sstevel@tonic-gate return; 13710Sstevel@tonic-gate 1372*5169Slclee case LOADDEL: 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate /* Parse the user-supplied deletion line (-D) */ 1375251Slclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, 1376251Slclee &ehead, &esect, &ecyl, &rsect, &numsect)) { 1377251Slclee (void) fprintf(stderr, 1378251Slclee "fdisk: Syntax error \"%s\"\n", file); 1379251Slclee exit(1); 1380251Slclee } 13810Sstevel@tonic-gate 13820Sstevel@tonic-gate /* Find the exact entry in the table */ 13830Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 13840Sstevel@tonic-gate if (Table[i].systid == id && 13850Sstevel@tonic-gate Table[i].bootid == act && 13860Sstevel@tonic-gate Table[i].beghead == bhead && 13870Sstevel@tonic-gate Table[i].begsect == ((bsect & 0x3f) | 1388*5169Slclee (uchar_t)((bcyl>>2) & 0xc0)) && 1389251Slclee Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 13900Sstevel@tonic-gate Table[i].endhead == ehead && 13910Sstevel@tonic-gate Table[i].endsect == ((esect & 0x3f) | 1392*5169Slclee (uchar_t)((ecyl>>2) & 0xc0)) && 1393251Slclee Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 13940Sstevel@tonic-gate Table[i].relsect == lel(rsect) && 13950Sstevel@tonic-gate Table[i].numsect == lel(numsect)) { 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate /* 13980Sstevel@tonic-gate * Found the entry. Now move rest of 13990Sstevel@tonic-gate * entries up toward the top of the 14000Sstevel@tonic-gate * table, leaving available entries at 14010Sstevel@tonic-gate * the end of the fdisk table. 14020Sstevel@tonic-gate */ 1403251Slclee for (j = i; j < FD_NUMPART - 1; j++) { 1404251Slclee Table[j].systid = Table[j + 1].systid; 1405251Slclee Table[j].bootid = Table[j + 1].bootid; 1406251Slclee Table[j].beghead = Table[j + 1].beghead; 1407251Slclee Table[j].begsect = Table[j + 1].begsect; 1408251Slclee Table[j].begcyl = Table[j + 1].begcyl; 1409251Slclee Table[j].endhead = Table[j + 1].endhead; 1410251Slclee Table[j].endsect = Table[j + 1].endsect; 1411251Slclee Table[j].endcyl = Table[j + 1].endcyl; 1412251Slclee Table[j].relsect = Table[j + 1].relsect; 1413251Slclee Table[j].numsect = Table[j + 1].numsect; 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate /* 14170Sstevel@tonic-gate * Mark the last entry as unused in case 14180Sstevel@tonic-gate * all table entries were in use prior 14190Sstevel@tonic-gate * to the deletion. 14200Sstevel@tonic-gate */ 14210Sstevel@tonic-gate 1422251Slclee Table[FD_NUMPART - 1].systid = UNUSED; 1423251Slclee Table[FD_NUMPART - 1].bootid = 0; 14240Sstevel@tonic-gate return; 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate } 1427251Slclee (void) fprintf(stderr, 14280Sstevel@tonic-gate "fdisk: Entry does not match any existing partition:\n" 14290Sstevel@tonic-gate " \"%s\"\n", 14300Sstevel@tonic-gate file); 14310Sstevel@tonic-gate exit(1); 1432251Slclee /* FALLTHRU */ 14330Sstevel@tonic-gate 1434*5169Slclee case LOADADD: 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate /* Parse the user-supplied addition line (-A) */ 1437251Slclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead, 1438251Slclee &esect, &ecyl, &rsect, &numsect)) { 1439251Slclee (void) fprintf(stderr, 1440251Slclee "fdisk: Syntax error \"%s\"\n", file); 1441251Slclee exit(1); 1442251Slclee } 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate /* Validate the partition. It cannot start at sector 0 */ 14450Sstevel@tonic-gate if (rsect == 0) { 14460Sstevel@tonic-gate (void) fprintf(stderr, 14470Sstevel@tonic-gate "fdisk: New partition cannot start at sector 0:\n" 14480Sstevel@tonic-gate " \"%s\".\n", 14490Sstevel@tonic-gate file); 14500Sstevel@tonic-gate exit(1); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate /* 14540Sstevel@tonic-gate * if the user wishes to add an EFI partition, we need 14550Sstevel@tonic-gate * more extensive validation. rsect should be 1, and 14560Sstevel@tonic-gate * numsect should equal the entire disk capacity - 1 14570Sstevel@tonic-gate */ 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate if (id == EFI_PMBR) { 14600Sstevel@tonic-gate if (rsect != 1) { 14610Sstevel@tonic-gate (void) fprintf(stderr, 14620Sstevel@tonic-gate "fdisk: EFI partitions must start at sector" 14630Sstevel@tonic-gate " 1 (input rsect = %d)\n", rsect); 14640Sstevel@tonic-gate exit(1); 14650Sstevel@tonic-gate } 14660Sstevel@tonic-gate 1467*5169Slclee if (numsect != dev_capacity - 1) { 14680Sstevel@tonic-gate (void) fprintf(stderr, 14690Sstevel@tonic-gate "fdisk: EFI partitions must encompass the " 1470251Slclee "entire disk\n" 1471*5169Slclee "(input numsect: %d - avail: %llu)\n", 1472251Slclee numsect, 1473*5169Slclee dev_capacity - 1); 14740Sstevel@tonic-gate exit(1); 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate /* Find unused entry for use and put entry in table */ 14790Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect, 14800Sstevel@tonic-gate ecyl, rsect, numsect) < 0) { 14810Sstevel@tonic-gate (void) fprintf(stderr, 14820Sstevel@tonic-gate "fdisk: Invalid entry could not be inserted:\n" 14830Sstevel@tonic-gate " \"%s\"\n", 14840Sstevel@tonic-gate file); 14850Sstevel@tonic-gate exit(1); 14860Sstevel@tonic-gate } 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate /* Make sure new entry does not overlap existing entry */ 14890Sstevel@tonic-gate if (verify_tbl() < 0) { 1490251Slclee (void) fprintf(stderr, 1491251Slclee "fdisk: Cannot create partition \"%s\"\n", file); 14920Sstevel@tonic-gate exit(1); 14930Sstevel@tonic-gate } 14940Sstevel@tonic-gate } /* switch funct */ 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate /* 14980Sstevel@tonic-gate * Set_Table_CHS_Values 14990Sstevel@tonic-gate * 15000Sstevel@tonic-gate * This will calculate the CHS values for beginning and ending CHS 15010Sstevel@tonic-gate * for a single partition table entry (ti) based on the relsect 15020Sstevel@tonic-gate * and numsect values contained in the partion table entry. 15030Sstevel@tonic-gate * 15040Sstevel@tonic-gate * hba_heads and hba_sectors contain the number of heads and sectors. 15050Sstevel@tonic-gate * 15060Sstevel@tonic-gate * If the number of cylinders exceeds the MAX_CYL, 15070Sstevel@tonic-gate * then maximum values will be placed in the corresponding chs entry. 15080Sstevel@tonic-gate */ 15090Sstevel@tonic-gate static void 15100Sstevel@tonic-gate Set_Table_CHS_Values(int ti) 15110Sstevel@tonic-gate { 15120Sstevel@tonic-gate uint32_t lba, cy, hd, sc; 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate lba = (uint32_t)Table[ti].relsect; 15150Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 15160Sstevel@tonic-gate /* 15170Sstevel@tonic-gate * the lba address cannot be expressed in CHS value 15180Sstevel@tonic-gate * so store the maximum CHS field values in the CHS fields. 15190Sstevel@tonic-gate */ 15200Sstevel@tonic-gate cy = MAX_CYL + 1; 15210Sstevel@tonic-gate hd = MAX_HEAD; 15220Sstevel@tonic-gate sc = MAX_SECT; 15230Sstevel@tonic-gate } else { 15240Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 15250Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 15260Sstevel@tonic-gate sc = lba % hba_sectors + 1; 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate Table[ti].begcyl = cy & 0xff; 1529251Slclee Table[ti].beghead = (uchar_t)hd; 1530251Slclee Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate /* 15330Sstevel@tonic-gate * This code is identical to the code above 15340Sstevel@tonic-gate * except that it works on ending CHS values 15350Sstevel@tonic-gate */ 15360Sstevel@tonic-gate lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1); 15370Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 15380Sstevel@tonic-gate cy = MAX_CYL + 1; 15390Sstevel@tonic-gate hd = MAX_HEAD; 15400Sstevel@tonic-gate sc = MAX_SECT; 15410Sstevel@tonic-gate } else { 15420Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 15430Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 15440Sstevel@tonic-gate sc = lba % hba_sectors + 1; 15450Sstevel@tonic-gate } 15460Sstevel@tonic-gate Table[ti].endcyl = cy & 0xff; 1547251Slclee Table[ti].endhead = (uchar_t)hd; 1548251Slclee Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate /* 15520Sstevel@tonic-gate * insert_tbl 15530Sstevel@tonic-gate * Insert entry into fdisk table. Check all user-supplied values 15540Sstevel@tonic-gate * for the entry, but not the validity relative to other table 15550Sstevel@tonic-gate * entries! 15560Sstevel@tonic-gate */ 1557251Slclee static int 1558251Slclee insert_tbl( 1559251Slclee int id, int act, 1560251Slclee int bhead, int bsect, int bcyl, 1561251Slclee int ehead, int esect, int ecyl, 1562251Slclee int rsect, int numsect) 15630Sstevel@tonic-gate { 15640Sstevel@tonic-gate int i; 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate /* validate partition size */ 1567*5169Slclee if (rsect + numsect > dev_capacity) { 1568251Slclee (void) fprintf(stderr, 15690Sstevel@tonic-gate "fdisk: Partition table exceeds the size of the disk.\n"); 15700Sstevel@tonic-gate return (-1); 15710Sstevel@tonic-gate } 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* find UNUSED partition table entry */ 15740Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 15750Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 15760Sstevel@tonic-gate break; 15770Sstevel@tonic-gate } 15780Sstevel@tonic-gate } 15790Sstevel@tonic-gate if (i >= FD_NUMPART) { 1580251Slclee (void) fprintf(stderr, "fdisk: Partition table is full.\n"); 15810Sstevel@tonic-gate return (-1); 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate 1585251Slclee Table[i].systid = (uchar_t)id; 1586251Slclee Table[i].bootid = (uchar_t)act; 15870Sstevel@tonic-gate Table[i].numsect = lel(numsect); 15880Sstevel@tonic-gate Table[i].relsect = lel(rsect); 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate /* 15910Sstevel@tonic-gate * If we have been called with a valid geometry, use it 15920Sstevel@tonic-gate * valid means non-zero values that fit in the BIOS fields 15930Sstevel@tonic-gate */ 15940Sstevel@tonic-gate if (0 < bsect && bsect <= MAX_SECT && 15950Sstevel@tonic-gate 0 <= bhead && bhead <= MAX_HEAD && 15960Sstevel@tonic-gate 0 < esect && esect <= MAX_SECT && 15970Sstevel@tonic-gate 0 <= ehead && ehead <= MAX_HEAD) { 15980Sstevel@tonic-gate if (bcyl > MAX_CYL) 15990Sstevel@tonic-gate bcyl = MAX_CYL + 1; 16000Sstevel@tonic-gate if (ecyl > MAX_CYL) 16010Sstevel@tonic-gate ecyl = MAX_CYL + 1; 16020Sstevel@tonic-gate Table[i].begcyl = bcyl & 0xff; 16030Sstevel@tonic-gate Table[i].endcyl = ecyl & 0xff; 1604251Slclee Table[i].beghead = (uchar_t)bhead; 1605251Slclee Table[i].endhead = (uchar_t)ehead; 1606251Slclee Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect); 16070Sstevel@tonic-gate Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect; 16080Sstevel@tonic-gate } else { 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate /* 16110Sstevel@tonic-gate * The specified values are invalid, 16120Sstevel@tonic-gate * so calculate the values based on hba_heads, hba_sectors 16130Sstevel@tonic-gate */ 16140Sstevel@tonic-gate Set_Table_CHS_Values(i); 16150Sstevel@tonic-gate } 16160Sstevel@tonic-gate 16170Sstevel@tonic-gate /* 16180Sstevel@tonic-gate * return partition index 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate return (i); 16210Sstevel@tonic-gate } 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate /* 16240Sstevel@tonic-gate * verify_tbl 16250Sstevel@tonic-gate * Verify that no partition entries overlap or exceed the size of 16260Sstevel@tonic-gate * the disk. 16270Sstevel@tonic-gate */ 1628251Slclee static int 1629251Slclee verify_tbl(void) 16300Sstevel@tonic-gate { 16310Sstevel@tonic-gate int i, j, rsect, numsect; 16320Sstevel@tonic-gate int noMoreParts = 0; 16330Sstevel@tonic-gate int numParts = 0; 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate /* Make sure new entry does not overlap an existing entry */ 1636251Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 16370Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 16380Sstevel@tonic-gate numParts++; 16390Sstevel@tonic-gate /* 16400Sstevel@tonic-gate * No valid partitions allowed after an UNUSED or 16410Sstevel@tonic-gate * EFI_PMBR part 16420Sstevel@tonic-gate */ 16430Sstevel@tonic-gate if (noMoreParts) { 16440Sstevel@tonic-gate return (-1); 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate /* 16480Sstevel@tonic-gate * EFI_PMBR partitions must be the only partition 16490Sstevel@tonic-gate * and must be Table entry 0 16500Sstevel@tonic-gate */ 16510Sstevel@tonic-gate if (Table[i].systid == EFI_PMBR) { 16520Sstevel@tonic-gate if (i == 0) { 16530Sstevel@tonic-gate noMoreParts = 1; 16540Sstevel@tonic-gate } else { 16550Sstevel@tonic-gate return (-1); 16560Sstevel@tonic-gate } 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate if (Table[i].relsect != 1) { 1659251Slclee (void) fprintf(stderr, "ERROR: " 16600Sstevel@tonic-gate "Invalid starting sector " 16610Sstevel@tonic-gate "for EFI_PMBR partition:\n" 16620Sstevel@tonic-gate "relsect %d " 16630Sstevel@tonic-gate "(should be 1)\n", 16640Sstevel@tonic-gate Table[i].relsect); 16650Sstevel@tonic-gate 16660Sstevel@tonic-gate return (-1); 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate 1669*5169Slclee if (Table[i].numsect != dev_capacity - 1) { 1670251Slclee (void) fprintf(stderr, "ERROR: " 16710Sstevel@tonic-gate "EFI_PMBR partition must " 16720Sstevel@tonic-gate "encompass the entire " 1673251Slclee "disk.\n numsect %d - " 1674*5169Slclee "actual %llu\n", 16750Sstevel@tonic-gate Table[i].numsect, 1676*5169Slclee dev_capacity - 1); 16770Sstevel@tonic-gate 16780Sstevel@tonic-gate return (-1); 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate /* make sure the partition isn't larger than the disk */ 16830Sstevel@tonic-gate rsect = lel(Table[i].relsect); 16840Sstevel@tonic-gate numsect = lel(Table[i].numsect); 1685*5169Slclee if ((rsect + numsect) > dev_capacity) { 16860Sstevel@tonic-gate return (-1); 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate 1689251Slclee for (j = i + 1; j < FD_NUMPART; j++) { 16900Sstevel@tonic-gate if (Table[j].systid != UNUSED) { 16910Sstevel@tonic-gate int t_relsect = lel(Table[j].relsect); 16920Sstevel@tonic-gate int t_numsect = lel(Table[j].numsect); 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate if (noMoreParts) { 1695251Slclee (void) fprintf(stderr, 16960Sstevel@tonic-gate "Cannot add partition to " 16970Sstevel@tonic-gate "table; no more partitions " 16980Sstevel@tonic-gate "allowed\n"); 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate if (io_debug) { 1701251Slclee (void) fprintf(stderr, 17020Sstevel@tonic-gate "DEBUG: Current " 17030Sstevel@tonic-gate "partition:\t" 17040Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1705251Slclee "%d:%d:%d:%d:%d\n" 17060Sstevel@tonic-gate " Next " 17070Sstevel@tonic-gate "partition:\t\t" 17080Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1709251Slclee "%d:%d:%d:%d:%d\n", 17100Sstevel@tonic-gate Table[i].systid, 17110Sstevel@tonic-gate Table[i].bootid, 17120Sstevel@tonic-gate Table[i].begcyl, 17130Sstevel@tonic-gate Table[i].beghead, 17140Sstevel@tonic-gate Table[i].begsect, 17150Sstevel@tonic-gate Table[i].endcyl, 17160Sstevel@tonic-gate Table[i].endhead, 17170Sstevel@tonic-gate Table[i].endsect, 17180Sstevel@tonic-gate Table[i].relsect, 17190Sstevel@tonic-gate Table[i].numsect, 17200Sstevel@tonic-gate Table[j].systid, 17210Sstevel@tonic-gate Table[j].bootid, 17220Sstevel@tonic-gate Table[j].begcyl, 17230Sstevel@tonic-gate Table[j].beghead, 17240Sstevel@tonic-gate Table[j].begsect, 17250Sstevel@tonic-gate Table[j].endcyl, 17260Sstevel@tonic-gate Table[j].endhead, 17270Sstevel@tonic-gate Table[j].endsect, 17280Sstevel@tonic-gate Table[j].relsect, 17290Sstevel@tonic-gate Table[j].numsect); 17300Sstevel@tonic-gate } 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate return (-1); 17330Sstevel@tonic-gate } 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate if ((rsect >= 17360Sstevel@tonic-gate (t_relsect + t_numsect)) || 1737251Slclee ((rsect + numsect) <= t_relsect)) { 17380Sstevel@tonic-gate continue; 17390Sstevel@tonic-gate } else { 1740251Slclee (void) fprintf(stderr, "ERROR: " 17410Sstevel@tonic-gate "current partition overlaps" 17420Sstevel@tonic-gate " following partition\n"); 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate return (-1); 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate } 17480Sstevel@tonic-gate } else { 17490Sstevel@tonic-gate noMoreParts = 1; 17500Sstevel@tonic-gate } 17510Sstevel@tonic-gate } 17520Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 17530Sstevel@tonic-gate if (noMoreParts || 17540Sstevel@tonic-gate ((lel(Table[i].relsect) + lel(Table[i].numsect)) > 1755*5169Slclee dev_capacity)) { 17560Sstevel@tonic-gate return (-1); 17570Sstevel@tonic-gate } 17580Sstevel@tonic-gate } 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate return (numParts); 17610Sstevel@tonic-gate } 17620Sstevel@tonic-gate 17630Sstevel@tonic-gate /* 17640Sstevel@tonic-gate * pars_fdisk 17650Sstevel@tonic-gate * Parse user-supplied data to set up fdisk partitions 17660Sstevel@tonic-gate * (-A, -D, -F). 17670Sstevel@tonic-gate */ 1768251Slclee static int 1769251Slclee pars_fdisk( 1770251Slclee char *line, 1771251Slclee int *id, int *act, 1772251Slclee int *bhead, int *bsect, int *bcyl, 1773251Slclee int *ehead, int *esect, int *ecyl, 1774251Slclee int *rsect, int *numsect) 17750Sstevel@tonic-gate { 17760Sstevel@tonic-gate int i; 17770Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 1778*5169Slclee return (1); 17790Sstevel@tonic-gate line[strlen(line)] = '\0'; 17800Sstevel@tonic-gate for (i = 0; i < strlen(line); i++) { 17810Sstevel@tonic-gate if (line[i] == '\0') { 17820Sstevel@tonic-gate break; 17830Sstevel@tonic-gate } else if (line[i] == ':') { 17840Sstevel@tonic-gate line[i] = ' '; 17850Sstevel@tonic-gate } 17860Sstevel@tonic-gate } 1787251Slclee if (sscanf(line, "%d %d %d %d %d %d %d %d %d %d", 17880Sstevel@tonic-gate id, act, bhead, bsect, bcyl, ehead, esect, ecyl, 17890Sstevel@tonic-gate rsect, numsect) != 10) { 17900Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error:\n \"%s\".\n", line); 17910Sstevel@tonic-gate exit(1); 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate return (0); 17940Sstevel@tonic-gate } 17950Sstevel@tonic-gate 17960Sstevel@tonic-gate /* 17970Sstevel@tonic-gate * validate_part 17980Sstevel@tonic-gate * Validate that a new partition does not start at sector 0. Only UNUSED 17990Sstevel@tonic-gate * partitions and previously existing partitions are allowed to start at 0. 18000Sstevel@tonic-gate */ 1801251Slclee static int 1802251Slclee validate_part(int id, int rsect, int numsect) 18030Sstevel@tonic-gate { 18040Sstevel@tonic-gate int i; 18050Sstevel@tonic-gate if ((id != UNUSED) && (rsect == 0)) { 18060Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 18070Sstevel@tonic-gate if ((Old_Table[i].systid == id) && 18080Sstevel@tonic-gate (Old_Table[i].relsect == lel(rsect)) && 1809*5169Slclee (Old_Table[i].numsect == lel(numsect))) 1810*5169Slclee return (0); 18110Sstevel@tonic-gate } 1812251Slclee (void) fprintf(stderr, 1813251Slclee "New partition cannot start at sector 0\n"); 18140Sstevel@tonic-gate return (-1); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate return (0); 18170Sstevel@tonic-gate } 18180Sstevel@tonic-gate 18190Sstevel@tonic-gate /* 18200Sstevel@tonic-gate * stage0 18210Sstevel@tonic-gate * Print out interactive menu and process user input. 18220Sstevel@tonic-gate */ 1823251Slclee static void 1824251Slclee stage0(void) 18250Sstevel@tonic-gate { 1826251Slclee dispmenu(); 1827251Slclee for (;;) { 1828251Slclee (void) printf(Q_LINE); 1829251Slclee (void) printf("Enter Selection: "); 1830251Slclee (void) gets(s); 18310Sstevel@tonic-gate rm_blanks(s); 18320Sstevel@tonic-gate while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) { 1833251Slclee (void) printf(E_LINE); /* Clear any previous error */ 1834251Slclee (void) printf( 1835251Slclee "Enter a one-digit number between 1 and 6."); 1836251Slclee (void) printf(Q_LINE); 1837251Slclee (void) printf("Enter Selection: "); 1838251Slclee (void) gets(s); 18390Sstevel@tonic-gate rm_blanks(s); 18400Sstevel@tonic-gate } 1841251Slclee (void) printf(E_LINE); 18420Sstevel@tonic-gate switch (s[0]) { 18430Sstevel@tonic-gate case '1': 18440Sstevel@tonic-gate if (pcreate() == -1) 18450Sstevel@tonic-gate return; 18460Sstevel@tonic-gate break; 18470Sstevel@tonic-gate case '2': 18480Sstevel@tonic-gate if (pchange() == -1) 18490Sstevel@tonic-gate return; 18500Sstevel@tonic-gate break; 18510Sstevel@tonic-gate case '3': 18520Sstevel@tonic-gate if (pdelete() == -1) 18530Sstevel@tonic-gate return; 18540Sstevel@tonic-gate break; 18550Sstevel@tonic-gate case '4': 18560Sstevel@tonic-gate if (ppartid() == -1) 18570Sstevel@tonic-gate return; 18580Sstevel@tonic-gate break; 18590Sstevel@tonic-gate case '5': 18600Sstevel@tonic-gate /* update disk partition table, if changed */ 18610Sstevel@tonic-gate if (TableChanged() == 1) { 18620Sstevel@tonic-gate copy_Table_to_Bootblk(); 18630Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 18640Sstevel@tonic-gate } 18650Sstevel@tonic-gate /* 18660Sstevel@tonic-gate * If the VTOC table is wrong fix it 18670Sstevel@tonic-gate * (truncate only) 18680Sstevel@tonic-gate */ 18690Sstevel@tonic-gate if (io_adjt) { 18700Sstevel@tonic-gate fix_slice(); 18710Sstevel@tonic-gate } 1872251Slclee (void) close(Dev); 18730Sstevel@tonic-gate exit(0); 1874251Slclee /* FALLTHRU */ 18750Sstevel@tonic-gate case '6': 18760Sstevel@tonic-gate /* 18770Sstevel@tonic-gate * If the VTOC table is wrong fix it 18780Sstevel@tonic-gate * (truncate only) 18790Sstevel@tonic-gate */ 18800Sstevel@tonic-gate if (io_adjt) { 18810Sstevel@tonic-gate fix_slice(); 18820Sstevel@tonic-gate } 1883251Slclee (void) close(Dev); 18840Sstevel@tonic-gate exit(0); 1885251Slclee /* FALLTHRU */ 18860Sstevel@tonic-gate default: 18870Sstevel@tonic-gate break; 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate copy_Table_to_Bootblk(); 18900Sstevel@tonic-gate disptbl(); 1891251Slclee dispmenu(); 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate } 18940Sstevel@tonic-gate 18950Sstevel@tonic-gate /* 18960Sstevel@tonic-gate * pcreate 18970Sstevel@tonic-gate * Create partition entry in the table (interactive mode). 18980Sstevel@tonic-gate */ 1899251Slclee static int 1900251Slclee pcreate(void) 19010Sstevel@tonic-gate { 1902251Slclee uchar_t tsystid = 'z'; 19030Sstevel@tonic-gate int i, j; 19040Sstevel@tonic-gate int rsect = 1; 19050Sstevel@tonic-gate int retCode = 0; 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate i = 0; 1908251Slclee for (;;) { 19090Sstevel@tonic-gate if (i == FD_NUMPART) { 1910251Slclee (void) printf(E_LINE); 1911251Slclee (void) printf( 1912251Slclee "The partition table is full!\n" 1913251Slclee "You must delete a partition before creating" 19140Sstevel@tonic-gate " a new one.\n"); 19150Sstevel@tonic-gate return (-1); 19160Sstevel@tonic-gate } 19170Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 19180Sstevel@tonic-gate break; 19190Sstevel@tonic-gate } 19200Sstevel@tonic-gate i++; 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate j = 0; 19240Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 19250Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 19260Sstevel@tonic-gate j += lel(Table[i].numsect); 19270Sstevel@tonic-gate } 1928*5169Slclee if (j >= chs_capacity) { 1929251Slclee (void) printf(E_LINE); 1930251Slclee (void) printf("There is no more room on the disk for" 19310Sstevel@tonic-gate " another partition.\n"); 1932251Slclee (void) printf( 1933251Slclee "You must delete a partition before creating" 19340Sstevel@tonic-gate " a new one.\n"); 19350Sstevel@tonic-gate return (-1); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate } 19380Sstevel@tonic-gate while (tsystid == 'z') { 1939251Slclee (void) printf(Q_LINE); 1940251Slclee (void) printf( 1941251Slclee "Select the partition type to create:\n" 1942251Slclee " 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n" 1943251Slclee " 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n" 1944251Slclee " 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n" 1945251Slclee " D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? "); 1946251Slclee (void) gets(s); 19470Sstevel@tonic-gate rm_blanks(s); 19480Sstevel@tonic-gate if (s[1] != 0) { 1949251Slclee (void) printf(E_LINE); 1950251Slclee (void) printf("Invalid selection, try again."); 19510Sstevel@tonic-gate continue; 19520Sstevel@tonic-gate } 19530Sstevel@tonic-gate switch (s[0]) { 19540Sstevel@tonic-gate case '0': /* exit */ 1955*5169Slclee (void) printf(E_LINE); 1956*5169Slclee return (-1); 19570Sstevel@tonic-gate case '1': /* Solaris partition */ 1958*5169Slclee tsystid = SUNIXOS2; 1959*5169Slclee break; 19600Sstevel@tonic-gate case '2': /* UNIX partition */ 1961*5169Slclee tsystid = UNIXOS; 1962*5169Slclee break; 19630Sstevel@tonic-gate case '3': /* PCIXOS partition */ 1964*5169Slclee tsystid = PCIXOS; 1965*5169Slclee break; 19660Sstevel@tonic-gate case '4': /* OTHEROS System partition */ 1967*5169Slclee tsystid = OTHEROS; 1968*5169Slclee break; 19690Sstevel@tonic-gate case '5': 1970*5169Slclee tsystid = DOSOS12; /* DOS 12 bit fat */ 1971*5169Slclee break; 19720Sstevel@tonic-gate case '6': 1973*5169Slclee tsystid = DOSOS16; /* DOS 16 bit fat */ 1974*5169Slclee break; 19750Sstevel@tonic-gate case '7': 1976*5169Slclee tsystid = EXTDOS; 1977*5169Slclee break; 19780Sstevel@tonic-gate case '8': 1979*5169Slclee tsystid = DOSHUGE; 1980*5169Slclee break; 19810Sstevel@tonic-gate case '9': 1982*5169Slclee tsystid = FDISK_FAT95; /* FAT16, need extended int13 */ 1983*5169Slclee break; 19840Sstevel@tonic-gate case 'a': /* x86 Boot partition */ 19850Sstevel@tonic-gate case 'A': 1986*5169Slclee tsystid = X86BOOT; 1987*5169Slclee break; 19880Sstevel@tonic-gate case 'b': /* Diagnostic boot partition */ 19890Sstevel@tonic-gate case 'B': 1990*5169Slclee tsystid = DIAGPART; 1991*5169Slclee break; 19920Sstevel@tonic-gate case 'c': /* FAT32 */ 19930Sstevel@tonic-gate case 'C': 1994*5169Slclee tsystid = FDISK_WINDOWS; 1995*5169Slclee break; 19960Sstevel@tonic-gate case 'd': /* FAT32 and need extended int13 */ 19970Sstevel@tonic-gate case 'D': 1998*5169Slclee tsystid = FDISK_EXT_WIN; 1999*5169Slclee break; 20000Sstevel@tonic-gate case 'e': /* Extended partition, need extended int13 */ 20010Sstevel@tonic-gate case 'E': 2002*5169Slclee tsystid = FDISK_EXTLBA; 2003*5169Slclee break; 20040Sstevel@tonic-gate case 'f': 20050Sstevel@tonic-gate case 'F': 2006*5169Slclee tsystid = EFI_PMBR; 2007*5169Slclee break; 20080Sstevel@tonic-gate default: 2009*5169Slclee (void) printf(E_LINE); 2010*5169Slclee (void) printf("Invalid selection, try again."); 2011*5169Slclee continue; 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate } 20140Sstevel@tonic-gate 2015251Slclee (void) printf(E_LINE); 20160Sstevel@tonic-gate 20170Sstevel@tonic-gate if (tsystid != EFI_PMBR) { 20180Sstevel@tonic-gate /* create the new partition */ 20190Sstevel@tonic-gate i = specify(tsystid); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate if (i != -1) { 20220Sstevel@tonic-gate /* see if it should be the active partition */ 2023251Slclee (void) printf(E_LINE); 2024251Slclee (void) printf(Q_LINE); 2025251Slclee 2026251Slclee (void) printf( 2027251Slclee "Should this become the active partition? If " 2028251Slclee "yes, it will be activated\n" 2029251Slclee "each time the computer is reset or turned on.\n" 2030251Slclee "Please type \"y\" or \"n\". "); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate if (yesno()) { 2033251Slclee (void) printf(E_LINE); 20340Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 20350Sstevel@tonic-gate if (j == i) { 20360Sstevel@tonic-gate Table[j].bootid = ACTIVE; 2037251Slclee (void) printf(E_LINE); 2038251Slclee (void) printf( 2039251Slclee "Partition %d is now " 20400Sstevel@tonic-gate "the active partition.", 2041251Slclee j + 1); 20420Sstevel@tonic-gate } else { 20430Sstevel@tonic-gate Table[j].bootid = 0; 20440Sstevel@tonic-gate } 20450Sstevel@tonic-gate } 20460Sstevel@tonic-gate } else { 20470Sstevel@tonic-gate Table[i].bootid = 0; 20480Sstevel@tonic-gate } 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate /* set up the return code */ 20510Sstevel@tonic-gate i = 1; 20520Sstevel@tonic-gate } 20530Sstevel@tonic-gate } else { 20540Sstevel@tonic-gate /* 20550Sstevel@tonic-gate * partitions of type EFI_PMBR must be the only partitions in 20560Sstevel@tonic-gate * the table 20570Sstevel@tonic-gate * 20580Sstevel@tonic-gate * First, make sure there were no errors the table is 20590Sstevel@tonic-gate * empty 20600Sstevel@tonic-gate */ 20610Sstevel@tonic-gate retCode = verify_tbl(); 20620Sstevel@tonic-gate 20630Sstevel@tonic-gate if (retCode < 0) { 2064251Slclee (void) fprintf(stderr, 20650Sstevel@tonic-gate "fdisk: Cannot create EFI partition table; \n" 20660Sstevel@tonic-gate "current partition table is invalid.\n"); 20670Sstevel@tonic-gate return (-1); 20680Sstevel@tonic-gate } else if (retCode > 0) { 2069251Slclee (void) printf( 2070251Slclee "An EFI partition must be the only partition on " 2071251Slclee "disk. You may manually delete existing\n" 2072251Slclee "partitions, or fdisk can do it.\n" 2073251Slclee "Do you want fdisk to destroy existing " 2074251Slclee "partitions?\n" 2075251Slclee "Please type \"y\" or \"n\". "); 20760Sstevel@tonic-gate 20770Sstevel@tonic-gate if (yesno()) { 20780Sstevel@tonic-gate nulltbl(); 20790Sstevel@tonic-gate } else { 20800Sstevel@tonic-gate return (-1); 20810Sstevel@tonic-gate } 20820Sstevel@tonic-gate } 20830Sstevel@tonic-gate 20840Sstevel@tonic-gate /* create the table entry - i should be 0 */ 20850Sstevel@tonic-gate i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, rsect, 2086*5169Slclee dev_capacity - rsect); 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate if (i != 0) { 2089251Slclee (void) printf("Error creating EFI partition!!!\n"); 20900Sstevel@tonic-gate i = -1; 20910Sstevel@tonic-gate } else { 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate /* EFI partitions are currently never active */ 20940Sstevel@tonic-gate Table[i].bootid = 0; 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate /* set up the return code */ 20970Sstevel@tonic-gate i = 1; 20980Sstevel@tonic-gate } 20990Sstevel@tonic-gate } 21000Sstevel@tonic-gate 21010Sstevel@tonic-gate return (i); 21020Sstevel@tonic-gate } 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate /* 21050Sstevel@tonic-gate * specify 21060Sstevel@tonic-gate * Query the user to specify the size of the new partition in 21070Sstevel@tonic-gate * terms of percentage of the disk or by specifying the starting 21080Sstevel@tonic-gate * cylinder and length in cylinders. 21090Sstevel@tonic-gate */ 2110251Slclee static int 2111251Slclee specify(uchar_t tsystid) 21120Sstevel@tonic-gate { 2113*5169Slclee int i, j, percent = -1; 21140Sstevel@tonic-gate int cyl, cylen, first_free, size_free; 2115*5169Slclee int max_free; 2116*5169Slclee int cyl_size; 21170Sstevel@tonic-gate struct ipart *partition[FD_NUMPART]; 21180Sstevel@tonic-gate 2119*5169Slclee cyl_size = heads * sectors; 2120*5169Slclee 2121*5169Slclee /* 2122*5169Slclee * make a local copy of the partition table 2123*5169Slclee * and sort it into relsect order 2124*5169Slclee */ 2125*5169Slclee for (i = 0; i < FD_NUMPART; i++) 2126*5169Slclee partition[i] = &Table[i]; 2127*5169Slclee 2128*5169Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 2129*5169Slclee if (partition[i]->systid == UNUSED) 2130*5169Slclee break; 2131*5169Slclee for (j = i + 1; j < FD_NUMPART; j++) { 2132*5169Slclee if (partition[j]->systid == UNUSED) 2133*5169Slclee break; 2134*5169Slclee if (lel(partition[j]->relsect) < 2135*5169Slclee lel(partition[i]->relsect)) { 2136*5169Slclee struct ipart *temp = partition[i]; 2137*5169Slclee partition[i] = partition[j]; 2138*5169Slclee partition[j] = temp; 2139*5169Slclee } 2140*5169Slclee } 2141*5169Slclee } 2142*5169Slclee 2143251Slclee (void) printf(Q_LINE); 2144251Slclee (void) printf( 2145251Slclee "Specify the percentage of disk to use for this partition\n" 2146251Slclee "(or type \"c\" to specify the size in cylinders). "); 2147251Slclee (void) gets(s); 21480Sstevel@tonic-gate rm_blanks(s); 21490Sstevel@tonic-gate if (s[0] != 'c') { /* Specify size in percentage of disk */ 2150*5169Slclee i = 0; 2151*5169Slclee while (s[i] != '\0') { 2152*5169Slclee if (s[i] < '0' || s[i] > '9') { 2153*5169Slclee (void) printf(E_LINE); 2154*5169Slclee (void) printf("Invalid percentage value " 2155*5169Slclee "specified; retry the operation."); 2156*5169Slclee return (-1); 2157*5169Slclee } 2158*5169Slclee i++; 2159*5169Slclee if (i > 3) { 2160*5169Slclee (void) printf(E_LINE); 2161*5169Slclee (void) printf("Invalid percentage value " 2162*5169Slclee "specified; retry the operation."); 2163*5169Slclee return (-1); 2164*5169Slclee } 2165*5169Slclee } 2166*5169Slclee if ((percent = atoi(s)) > 100) { 2167*5169Slclee (void) printf(E_LINE); 2168*5169Slclee (void) printf( 2169*5169Slclee "Percentage value is too large. The value must be" 2170*5169Slclee " between 1 and 100;\nretry the operation.\n"); 2171*5169Slclee return (-1); 21720Sstevel@tonic-gate } 2173*5169Slclee if (percent < 1) { 2174*5169Slclee (void) printf(E_LINE); 2175*5169Slclee (void) printf( 2176*5169Slclee "Percentage value is too small. The value must be" 2177*5169Slclee " between 1 and 100;\nretry the operation.\n"); 2178*5169Slclee return (-1); 2179*5169Slclee } 2180*5169Slclee 2181*5169Slclee 2182*5169Slclee if (percent == 100) 2183*5169Slclee cylen = Numcyl - 1; 2184*5169Slclee else 2185*5169Slclee cylen = (Numcyl * percent) / 100; 2186*5169Slclee 2187*5169Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 2188*5169Slclee if ((tsystid == DOSOS12) && 2189*5169Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 2190*5169Slclee int n; 2191*5169Slclee n = MAXDOS * 100 / (int)(cyl_size) / Numcyl; 2192*5169Slclee (void) printf(E_LINE); 2193*5169Slclee (void) printf("Maximum size for a DOS partition " 2194*5169Slclee "is %d%%; retry the operation.", 2195*5169Slclee n <= 100 ? n : 100); 2196*5169Slclee return (-1); 21970Sstevel@tonic-gate } 2198*5169Slclee 2199*5169Slclee 2200*5169Slclee max_free = 0; 2201*5169Slclee for (i = 0; i < FD_NUMPART; i++) { 2202*5169Slclee 2203*5169Slclee /* 2204*5169Slclee * check for free space before partition i 2205*5169Slclee * where i varies from 0 to 3 2206*5169Slclee * 2207*5169Slclee * freespace after partition 3 is unusable 2208*5169Slclee * because there are no free partitions 2209*5169Slclee * 2210*5169Slclee * freespace begins at the end of previous partition 2211*5169Slclee * or cylinder 1 2212*5169Slclee */ 2213*5169Slclee if (i) { 2214*5169Slclee /* Not an empty table */ 2215*5169Slclee first_free = lel(partition[i - 1]->relsect) + 2216*5169Slclee lel(partition[i - 1]->numsect); 2217*5169Slclee } else { 2218*5169Slclee first_free = cyl_size; 2219*5169Slclee } 2220*5169Slclee 2221*5169Slclee /* 2222*5169Slclee * freespace ends before the current partition 2223*5169Slclee * or the end of the disk (chs end) 2224*5169Slclee */ 2225*5169Slclee if (partition[i]->systid == UNUSED) { 2226*5169Slclee size_free = chs_capacity - first_free; 2227*5169Slclee } else { 2228*5169Slclee size_free = 2229*5169Slclee lel(partition[i]->relsect) - first_free; 2230*5169Slclee } 2231*5169Slclee 2232*5169Slclee /* save largest free space */ 2233*5169Slclee if (max_free < size_free) 2234*5169Slclee max_free = size_free; 2235*5169Slclee 2236*5169Slclee if ((cylen * cyl_size) <= size_free) { 2237*5169Slclee /* We found a place to use */ 2238*5169Slclee break; 2239*5169Slclee } 2240*5169Slclee if (partition[i]->systid == UNUSED) { 2241*5169Slclee (void) printf(E_LINE); 2242*5169Slclee max_free /= (cyl_size); 2243*5169Slclee (void) fprintf(stderr, "fdisk: " 2244*5169Slclee "Maximum percentage available is %d\n", 2245*5169Slclee 100 * max_free / Numcyl); 2246*5169Slclee return (-1); 2247*5169Slclee } 22480Sstevel@tonic-gate } 2249*5169Slclee 2250*5169Slclee (void) printf(E_LINE); 2251*5169Slclee if (i >= FD_NUMPART) { 2252*5169Slclee (void) fprintf(stderr, 2253*5169Slclee "fdisk: Partition table is full.\n"); 2254*5169Slclee return (-1); 2255*5169Slclee } 2256*5169Slclee 2257*5169Slclee if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 2258*5169Slclee first_free, cylen * cyl_size)) >= 0) { 2259*5169Slclee return (i); 2260*5169Slclee } 2261*5169Slclee return (-1); 2262*5169Slclee } else { 2263*5169Slclee 2264*5169Slclee /* Specifying size in cylinders */ 2265251Slclee (void) printf(E_LINE); 2266*5169Slclee (void) printf(Q_LINE); 2267*5169Slclee (void) printf("Enter starting cylinder number: "); 2268*5169Slclee if ((cyl = getcyl()) == -1) { 2269*5169Slclee (void) printf(E_LINE); 2270*5169Slclee (void) printf("Invalid number; retry the operation."); 2271*5169Slclee return (-1); 2272*5169Slclee } 2273*5169Slclee if (cyl == 0) { 2274*5169Slclee (void) printf(E_LINE); 2275*5169Slclee (void) printf( 2276*5169Slclee "New partition cannot start at cylinder 0.\n"); 2277*5169Slclee return (-1); 2278*5169Slclee } 2279*5169Slclee if (cyl >= (unsigned int)Numcyl) { 2280*5169Slclee (void) printf(E_LINE); 2281*5169Slclee (void) printf( 2282*5169Slclee "Cylinder %d is out of bounds, " 2283*5169Slclee "the maximum is %d.\n", 2284*5169Slclee cyl, Numcyl - 1); 2285*5169Slclee return (-1); 2286*5169Slclee } 2287*5169Slclee (void) printf(Q_LINE); 2288*5169Slclee (void) printf("Enter partition size in cylinders: "); 2289*5169Slclee if ((cylen = getcyl()) == -1) { 2290*5169Slclee (void) printf(E_LINE); 2291*5169Slclee (void) printf("Invalid number, retry the operation."); 2292*5169Slclee return (-1); 2293*5169Slclee } 2294*5169Slclee 2295*5169Slclee for (i = 0; i < FD_NUMPART; i++) { 2296*5169Slclee uint32_t t_relsect, t_numsect; 2297*5169Slclee 2298*5169Slclee if (partition[i]->systid == UNUSED) 2299*5169Slclee break; 2300*5169Slclee t_relsect = lel(partition[i]->relsect); 2301*5169Slclee t_numsect = lel(partition[i]->numsect); 2302*5169Slclee 2303*5169Slclee if (cyl * cyl_size >= t_relsect && 2304*5169Slclee cyl * cyl_size < t_relsect + t_numsect) { 2305*5169Slclee (void) printf(E_LINE); 2306*5169Slclee (void) printf( 2307*5169Slclee "Cylinder %d is already allocated" 2308*5169Slclee "\nretry the operation.", 2309*5169Slclee cyl); 2310*5169Slclee return (-1); 2311*5169Slclee } 2312*5169Slclee 2313*5169Slclee if (cyl * cyl_size < t_relsect && 2314*5169Slclee (cyl + cylen - 1) * cyl_size > t_relsect) { 2315*5169Slclee (void) printf(E_LINE); 2316*5169Slclee (void) printf( 2317*5169Slclee "Maximum size for partition is %u cylinders" 2318*5169Slclee "\nretry the operation.", 2319*5169Slclee (t_relsect - cyl * cyl_size) / cyl_size); 2320*5169Slclee return (-1); 2321*5169Slclee } 2322*5169Slclee } 2323*5169Slclee 2324*5169Slclee /* Verify partition doesn't exceed disk size */ 2325*5169Slclee if (cyl + cylen > Numcyl) { 2326*5169Slclee (void) printf(E_LINE); 2327*5169Slclee (void) printf( 2328*5169Slclee "Maximum size for partition is %d cylinders;" 2329*5169Slclee "\nretry the operation.", 2330*5169Slclee Numcyl - cyl); 2331*5169Slclee return (-1); 2332*5169Slclee } 2333*5169Slclee 2334*5169Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 2335*5169Slclee if ((tsystid == DOSOS12) && 2336*5169Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 2337*5169Slclee (void) printf(E_LINE); 2338*5169Slclee (void) printf( 2339*5169Slclee "Maximum size for a %s partition is %ld cylinders;" 2340*5169Slclee "\nretry the operation.", 2341*5169Slclee Dstr, MAXDOS / (int)(cyl_size)); 2342*5169Slclee return (-1); 2343*5169Slclee } 2344*5169Slclee 2345251Slclee (void) printf(E_LINE); 2346*5169Slclee i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 2347*5169Slclee cyl * cyl_size, cylen * cyl_size); 2348*5169Slclee if (i < 0) 2349*5169Slclee return (-1); 2350*5169Slclee 2351*5169Slclee if (verify_tbl() < 0) { 2352*5169Slclee (void) printf(E_LINE); 2353*5169Slclee (void) printf("fdisk: Cannot create partition table\n"); 2354*5169Slclee return (-1); 2355*5169Slclee } 2356*5169Slclee 2357*5169Slclee return (i); 23580Sstevel@tonic-gate } 23590Sstevel@tonic-gate } 23600Sstevel@tonic-gate 23610Sstevel@tonic-gate /* 23620Sstevel@tonic-gate * dispmenu 23630Sstevel@tonic-gate * Display command menu (interactive mode). 23640Sstevel@tonic-gate */ 2365251Slclee static void 2366251Slclee dispmenu(void) 23670Sstevel@tonic-gate { 2368251Slclee (void) printf(M_LINE); 2369251Slclee (void) printf( 2370251Slclee "SELECT ONE OF THE FOLLOWING:\n" 2371251Slclee " 1. Create a partition\n" 2372251Slclee " 2. Specify the active partition\n" 2373251Slclee " 3. Delete a partition\n" 2374251Slclee " 4. Change between Solaris and Solaris2 Partition IDs\n" 2375251Slclee " 5. Exit (update disk configuration and exit)\n" 2376251Slclee " 6. Cancel (exit without updating disk configuration)\n"); 23770Sstevel@tonic-gate } 23780Sstevel@tonic-gate 23790Sstevel@tonic-gate /* 23800Sstevel@tonic-gate * pchange 23810Sstevel@tonic-gate * Change the ACTIVE designation of a partition. 23820Sstevel@tonic-gate */ 2383251Slclee static int 2384251Slclee pchange(void) 23850Sstevel@tonic-gate { 23860Sstevel@tonic-gate char s[80]; 23870Sstevel@tonic-gate int i, j; 23880Sstevel@tonic-gate 2389251Slclee for (;;) { 2390251Slclee (void) printf(Q_LINE); 23910Sstevel@tonic-gate { 2392251Slclee (void) printf( 2393251Slclee "Specify the partition number to boot from" 23940Sstevel@tonic-gate " (or specify 0 for none): "); 23950Sstevel@tonic-gate } 2396251Slclee (void) gets(s); 23970Sstevel@tonic-gate rm_blanks(s); 23980Sstevel@tonic-gate if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) { 2399251Slclee (void) printf(E_LINE); 2400251Slclee (void) printf( 2401251Slclee "Invalid response, please specify a number" 24020Sstevel@tonic-gate " between 0 and 4.\n"); 24030Sstevel@tonic-gate } else { 24040Sstevel@tonic-gate break; 24050Sstevel@tonic-gate } 24060Sstevel@tonic-gate } 24070Sstevel@tonic-gate if (s[0] == '0') { /* No active partitions */ 24080Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 24090Sstevel@tonic-gate if (Table[i].systid != UNUSED && 24100Sstevel@tonic-gate Table[i].bootid == ACTIVE) 24110Sstevel@tonic-gate Table[i].bootid = 0; 24120Sstevel@tonic-gate } 2413251Slclee (void) printf(E_LINE); 2414251Slclee (void) printf( 2415251Slclee "No partition is currently marked as active."); 24160Sstevel@tonic-gate return (0); 24170Sstevel@tonic-gate } else { /* User has selected a partition to be active */ 24180Sstevel@tonic-gate i = s[0] - '1'; 24190Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2420251Slclee (void) printf(E_LINE); 2421251Slclee (void) printf("Partition does not exist."); 24220Sstevel@tonic-gate return (-1); 24230Sstevel@tonic-gate } 24240Sstevel@tonic-gate /* a DOS-DATA or EXT-DOS partition cannot be active */ 24250Sstevel@tonic-gate else if ((Table[i].systid == DOSDATA) || 24260Sstevel@tonic-gate (Table[i].systid == EXTDOS) || 24270Sstevel@tonic-gate (Table[i].systid == FDISK_EXTLBA)) { 2428251Slclee (void) printf(E_LINE); 2429251Slclee (void) printf( 2430251Slclee "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions " 24310Sstevel@tonic-gate "cannot be made active.\n"); 2432251Slclee (void) printf("Select another partition."); 24330Sstevel@tonic-gate return (-1); 24340Sstevel@tonic-gate } 24350Sstevel@tonic-gate Table[i].bootid = ACTIVE; 24360Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 24370Sstevel@tonic-gate if (j != i) 24380Sstevel@tonic-gate Table[j].bootid = 0; 24390Sstevel@tonic-gate } 24400Sstevel@tonic-gate } 2441251Slclee (void) printf(E_LINE); 24420Sstevel@tonic-gate { 2443251Slclee (void) printf( 2444251Slclee "Partition %d is now active. The system will start up" 2445251Slclee " from this\n", i + 1); 2446251Slclee (void) printf("partition after the next reboot."); 24470Sstevel@tonic-gate } 24480Sstevel@tonic-gate return (1); 24490Sstevel@tonic-gate } 24500Sstevel@tonic-gate 24510Sstevel@tonic-gate /* 24520Sstevel@tonic-gate * Change between SOLARIS and SOLARIS2 partition id 24530Sstevel@tonic-gate */ 2454251Slclee static int 2455251Slclee ppartid(void) 24560Sstevel@tonic-gate { 24570Sstevel@tonic-gate char *p, s[80]; 24580Sstevel@tonic-gate int i; 24590Sstevel@tonic-gate 24600Sstevel@tonic-gate for (;;) { 2461251Slclee (void) printf(Q_LINE); 2462251Slclee (void) printf("Specify the partition number to change" 2463*5169Slclee " (or enter 0 to exit): "); 2464251Slclee if (!fgets(s, sizeof (s), stdin)) 2465251Slclee return (1); 24660Sstevel@tonic-gate i = strtol(s, &p, 10); 24670Sstevel@tonic-gate 24680Sstevel@tonic-gate if (*p != '\n' || i < 0 || i > FD_NUMPART) { 2469251Slclee (void) printf(E_LINE); 2470251Slclee (void) printf( 2471251Slclee "Invalid response, retry the operation.\n"); 24720Sstevel@tonic-gate continue; 24730Sstevel@tonic-gate } 24740Sstevel@tonic-gate 24750Sstevel@tonic-gate if (i == 0) { 24760Sstevel@tonic-gate /* exit delete command */ 2477251Slclee (void) printf(E_LINE); /* clear error message */ 24780Sstevel@tonic-gate return (1); 24790Sstevel@tonic-gate } 24800Sstevel@tonic-gate 24810Sstevel@tonic-gate i -= 1; 24820Sstevel@tonic-gate if (Table[i].systid == SUNIXOS) { 24830Sstevel@tonic-gate Table[i].systid = SUNIXOS2; 24840Sstevel@tonic-gate } else if (Table[i].systid == SUNIXOS2) { 24850Sstevel@tonic-gate Table[i].systid = SUNIXOS; 24860Sstevel@tonic-gate } else { 2487251Slclee (void) printf(E_LINE); 2488251Slclee (void) printf( 2489251Slclee "Partition %d is not a Solaris partition.", 24900Sstevel@tonic-gate i + 1); 24910Sstevel@tonic-gate continue; 24920Sstevel@tonic-gate } 24930Sstevel@tonic-gate 2494251Slclee (void) printf(E_LINE); 2495251Slclee (void) printf("Partition %d has been changed.", i + 1); 24960Sstevel@tonic-gate return (1); 24970Sstevel@tonic-gate } 24980Sstevel@tonic-gate } 24990Sstevel@tonic-gate 25000Sstevel@tonic-gate /* 25010Sstevel@tonic-gate * pdelete 25020Sstevel@tonic-gate * Remove partition entry from the table (interactive mode). 25030Sstevel@tonic-gate */ 2504251Slclee static char 2505251Slclee pdelete(void) 25060Sstevel@tonic-gate { 25070Sstevel@tonic-gate char s[80]; 25080Sstevel@tonic-gate int i, j; 25090Sstevel@tonic-gate char pactive; 25100Sstevel@tonic-gate 2511251Slclee DEL1: (void) printf(Q_LINE); 2512251Slclee (void) printf("Specify the partition number to delete" 25130Sstevel@tonic-gate " (or enter 0 to exit): "); 2514251Slclee (void) gets(s); 25150Sstevel@tonic-gate rm_blanks(s); 25160Sstevel@tonic-gate if ((s[0] == '0')) { /* exit delete command */ 2517251Slclee (void) printf(E_LINE); /* clear error message */ 25180Sstevel@tonic-gate return (1); 25190Sstevel@tonic-gate } 25200Sstevel@tonic-gate /* Accept only a single digit between 1 and 4 */ 25210Sstevel@tonic-gate if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) { 2522251Slclee (void) printf(E_LINE); 2523251Slclee (void) printf("Invalid response, retry the operation.\n"); 25240Sstevel@tonic-gate goto DEL1; 25250Sstevel@tonic-gate } else { /* Found a digit between 1 and 4 */ 25260Sstevel@tonic-gate --i; /* Structure begins with element 0 */ 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate 25290Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2530251Slclee (void) printf(E_LINE); 2531251Slclee (void) printf("Partition %d does not exist.", i + 1); 25320Sstevel@tonic-gate return (-1); 25330Sstevel@tonic-gate } 25340Sstevel@tonic-gate 2535251Slclee (void) printf(Q_LINE); 2536251Slclee (void) printf("Are you sure you want to delete partition %d?" 2537251Slclee " This will make all files and \n", i + 1); 2538251Slclee (void) printf("programs in this partition inaccessible (type" 25390Sstevel@tonic-gate " \"y\" or \"n\"). "); 25400Sstevel@tonic-gate 2541251Slclee (void) printf(E_LINE); 25420Sstevel@tonic-gate if (! yesno()) { 25430Sstevel@tonic-gate return (1); 25440Sstevel@tonic-gate } 25450Sstevel@tonic-gate 25460Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) { 25470Sstevel@tonic-gate pactive = 1; 25480Sstevel@tonic-gate } else { 25490Sstevel@tonic-gate pactive = 0; 25500Sstevel@tonic-gate } 25510Sstevel@tonic-gate 25520Sstevel@tonic-gate for (j = i; j < FD_NUMPART - 1; j++) { 2553*5169Slclee Table[j] = Table[j + 1]; 25540Sstevel@tonic-gate } 25550Sstevel@tonic-gate 25560Sstevel@tonic-gate Table[j].systid = UNUSED; 25570Sstevel@tonic-gate Table[j].numsect = 0; 25580Sstevel@tonic-gate Table[j].relsect = 0; 25590Sstevel@tonic-gate Table[j].bootid = 0; 2560251Slclee (void) printf(E_LINE); 2561251Slclee (void) printf("Partition %d has been deleted.", i + 1); 25620Sstevel@tonic-gate 25630Sstevel@tonic-gate if (pactive) { 2564*5169Slclee (void) printf(" This was the active partition."); 25650Sstevel@tonic-gate } 25660Sstevel@tonic-gate 25670Sstevel@tonic-gate return (1); 25680Sstevel@tonic-gate } 25690Sstevel@tonic-gate 25700Sstevel@tonic-gate /* 25710Sstevel@tonic-gate * rm_blanks 25720Sstevel@tonic-gate * Remove blanks from strings of user responses. 25730Sstevel@tonic-gate */ 2574251Slclee static void 2575251Slclee rm_blanks(char *s) 25760Sstevel@tonic-gate { 25770Sstevel@tonic-gate register int i, j; 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate for (i = 0; i < CBUFLEN; i++) { 25800Sstevel@tonic-gate if ((s[i] == ' ') || (s[i] == '\t')) 25810Sstevel@tonic-gate continue; 25820Sstevel@tonic-gate else 25830Sstevel@tonic-gate /* Found first non-blank character of the string */ 25840Sstevel@tonic-gate break; 25850Sstevel@tonic-gate } 25860Sstevel@tonic-gate for (j = 0; i < CBUFLEN; j++, i++) { 25870Sstevel@tonic-gate if ((s[j] = s[i]) == '\0') { 25880Sstevel@tonic-gate /* Reached end of string */ 25890Sstevel@tonic-gate return; 25900Sstevel@tonic-gate } 25910Sstevel@tonic-gate } 25920Sstevel@tonic-gate } 25930Sstevel@tonic-gate 25940Sstevel@tonic-gate /* 25950Sstevel@tonic-gate * getcyl 25960Sstevel@tonic-gate * Take the user-specified cylinder number and convert it from a 25970Sstevel@tonic-gate * string to a decimal value. 25980Sstevel@tonic-gate */ 2599251Slclee static int 2600251Slclee getcyl(void) 26010Sstevel@tonic-gate { 26020Sstevel@tonic-gate int slen, i, j; 26030Sstevel@tonic-gate unsigned int cyl; 2604251Slclee (void) gets(s); 26050Sstevel@tonic-gate rm_blanks(s); 26060Sstevel@tonic-gate slen = strlen(s); 26070Sstevel@tonic-gate j = 1; 26080Sstevel@tonic-gate cyl = 0; 2609251Slclee for (i = slen - 1; i >= 0; i--) { 26100Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 26110Sstevel@tonic-gate return (-1); 26120Sstevel@tonic-gate } 2613251Slclee cyl += (j * (s[i] - '0')); 26140Sstevel@tonic-gate j *= 10; 26150Sstevel@tonic-gate } 26160Sstevel@tonic-gate return (cyl); 26170Sstevel@tonic-gate } 26180Sstevel@tonic-gate 26190Sstevel@tonic-gate /* 26200Sstevel@tonic-gate * disptbl 26210Sstevel@tonic-gate * Display the current fdisk table; determine percentage 26220Sstevel@tonic-gate * of the disk used for each partition. 26230Sstevel@tonic-gate */ 2624251Slclee static void 2625251Slclee disptbl(void) 26260Sstevel@tonic-gate { 26270Sstevel@tonic-gate int i; 26280Sstevel@tonic-gate unsigned int startcyl, endcyl, length, percent, remainder; 26290Sstevel@tonic-gate char *stat, *type; 26300Sstevel@tonic-gate 26310Sstevel@tonic-gate if ((heads == 0) || (sectors == 0)) { 2632251Slclee (void) printf("WARNING: critical disk geometry information" 2633*5169Slclee " missing!\n"); 2634251Slclee (void) printf("\theads = %d, sectors = %d\n", heads, sectors); 26350Sstevel@tonic-gate exit(1); 26360Sstevel@tonic-gate } 26370Sstevel@tonic-gate 2638251Slclee (void) printf(HOME); 2639251Slclee (void) printf(T_LINE); 2640251Slclee (void) printf(" Total disk size is %d cylinders\n", Numcyl); 2641251Slclee (void) printf(" Cylinder size is %d (512 byte) blocks\n\n", 2642251Slclee heads * sectors); 2643251Slclee (void) printf( 2644251Slclee " Cylinders\n"); 2645251Slclee (void) printf( 2646251Slclee " Partition Status Type Start End Length" 26470Sstevel@tonic-gate " %%\n"); 2648251Slclee (void) printf( 2649251Slclee " ========= ====== ============ ===== === ======" 26500Sstevel@tonic-gate " ==="); 26510Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 26520Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2653251Slclee (void) printf("\n"); 2654251Slclee (void) printf(CLR_LIN); 26550Sstevel@tonic-gate continue; 26560Sstevel@tonic-gate } 26570Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) 2658*5169Slclee stat = Actvstr; 26590Sstevel@tonic-gate else 2660*5169Slclee stat = NAstr; 26610Sstevel@tonic-gate switch (Table[i].systid) { 26620Sstevel@tonic-gate case UNIXOS: 2663*5169Slclee type = Ustr; 2664*5169Slclee break; 26650Sstevel@tonic-gate case SUNIXOS: 2666*5169Slclee type = SUstr; 2667*5169Slclee break; 26680Sstevel@tonic-gate case SUNIXOS2: 2669*5169Slclee type = SU2str; 2670*5169Slclee break; 26710Sstevel@tonic-gate case X86BOOT: 2672*5169Slclee type = X86str; 2673*5169Slclee break; 26740Sstevel@tonic-gate case DOSOS12: 2675*5169Slclee type = Dstr; 2676*5169Slclee break; 26770Sstevel@tonic-gate case DOSOS16: 2678*5169Slclee type = D16str; 2679*5169Slclee break; 26800Sstevel@tonic-gate case EXTDOS: 2681*5169Slclee type = EDstr; 2682*5169Slclee break; 26830Sstevel@tonic-gate case DOSDATA: 2684*5169Slclee type = DDstr; 2685*5169Slclee break; 26860Sstevel@tonic-gate case DOSHUGE: 2687*5169Slclee type = DBstr; 2688*5169Slclee break; 26890Sstevel@tonic-gate case PCIXOS: 2690*5169Slclee type = PCstr; 2691*5169Slclee break; 26920Sstevel@tonic-gate case DIAGPART: 2693*5169Slclee type = DIAGstr; 2694*5169Slclee break; 26950Sstevel@tonic-gate case FDISK_IFS: 2696*5169Slclee type = IFSstr; 2697*5169Slclee break; 26980Sstevel@tonic-gate case FDISK_AIXBOOT: 2699*5169Slclee type = AIXstr; 2700*5169Slclee break; 27010Sstevel@tonic-gate case FDISK_AIXDATA: 2702*5169Slclee type = AIXDstr; 2703*5169Slclee break; 27040Sstevel@tonic-gate case FDISK_OS2BOOT: 2705*5169Slclee type = OS2str; 2706*5169Slclee break; 27070Sstevel@tonic-gate case FDISK_WINDOWS: 2708*5169Slclee type = WINstr; 2709*5169Slclee break; 27100Sstevel@tonic-gate case FDISK_EXT_WIN: 2711*5169Slclee type = EWINstr; 2712*5169Slclee break; 27130Sstevel@tonic-gate case FDISK_FAT95: 2714*5169Slclee type = FAT95str; 2715*5169Slclee break; 27160Sstevel@tonic-gate case FDISK_EXTLBA: 2717*5169Slclee type = EXTLstr; 2718*5169Slclee break; 27190Sstevel@tonic-gate case FDISK_LINUX: 2720*5169Slclee type = LINUXstr; 2721*5169Slclee break; 27220Sstevel@tonic-gate case FDISK_CPM: 2723*5169Slclee type = CPMstr; 2724*5169Slclee break; 27250Sstevel@tonic-gate case FDISK_NOVELL3: 2726*5169Slclee type = NOVstr; 2727*5169Slclee break; 27280Sstevel@tonic-gate case FDISK_QNX4: 2729*5169Slclee type = QNXstr; 2730*5169Slclee break; 27310Sstevel@tonic-gate case FDISK_QNX42: 2732*5169Slclee type = QNX2str; 2733*5169Slclee break; 27340Sstevel@tonic-gate case FDISK_QNX43: 2735*5169Slclee type = QNX3str; 2736*5169Slclee break; 27370Sstevel@tonic-gate case FDISK_LINUXNAT: 2738*5169Slclee type = LINNATstr; 2739*5169Slclee break; 27400Sstevel@tonic-gate case FDISK_NTFSVOL1: 2741*5169Slclee type = NTFSVOL1str; 2742*5169Slclee break; 27430Sstevel@tonic-gate case FDISK_NTFSVOL2: 2744*5169Slclee type = NTFSVOL2str; 2745*5169Slclee break; 27460Sstevel@tonic-gate case FDISK_BSD: 2747*5169Slclee type = BSDstr; 2748*5169Slclee break; 27490Sstevel@tonic-gate case FDISK_NEXTSTEP: 2750*5169Slclee type = NEXTSTEPstr; 2751*5169Slclee break; 27520Sstevel@tonic-gate case FDISK_BSDIFS: 2753*5169Slclee type = BSDIFSstr; 2754*5169Slclee break; 27550Sstevel@tonic-gate case FDISK_BSDISWAP: 2756*5169Slclee type = BSDISWAPstr; 2757*5169Slclee break; 27580Sstevel@tonic-gate case EFI_PMBR: 2759*5169Slclee type = EFIstr; 2760*5169Slclee break; 27610Sstevel@tonic-gate default: 2762*5169Slclee type = Ostr; 2763*5169Slclee break; 27640Sstevel@tonic-gate } 2765251Slclee startcyl = lel(Table[i].relsect) / (heads * sectors); 27660Sstevel@tonic-gate length = lel(Table[i].numsect) / (long)(heads * sectors); 27670Sstevel@tonic-gate if (lel(Table[i].numsect) % (long)(heads * sectors)) 27680Sstevel@tonic-gate length++; 27690Sstevel@tonic-gate endcyl = startcyl + length - 1; 27700Sstevel@tonic-gate percent = length * 100 / Numcyl; 2771251Slclee if ((remainder = (length * 100 % Numcyl)) != 0) { 27720Sstevel@tonic-gate if ((remainder * 100 / Numcyl) > 50) { 27730Sstevel@tonic-gate /* round up */ 27740Sstevel@tonic-gate percent++; 27750Sstevel@tonic-gate } 27760Sstevel@tonic-gate /* Else leave the percent as is since it's already */ 27770Sstevel@tonic-gate /* rounded down */ 27780Sstevel@tonic-gate } 27790Sstevel@tonic-gate if (percent > 100) 27800Sstevel@tonic-gate percent = 100; 2781251Slclee (void) printf( 2782251Slclee "\n %d %s %-12.12s %4d %4d %4d" 2783251Slclee " %3d", 2784251Slclee i + 1, stat, type, startcyl, endcyl, length, percent); 27850Sstevel@tonic-gate } 27860Sstevel@tonic-gate /* Print warning message if table is empty */ 27870Sstevel@tonic-gate if (Table[0].systid == UNUSED) { 2788251Slclee (void) printf(W_LINE); 2789251Slclee (void) printf("WARNING: no partitions are defined!"); 27900Sstevel@tonic-gate } else { 27910Sstevel@tonic-gate /* Clear the warning line */ 2792251Slclee (void) printf(W_LINE); 27930Sstevel@tonic-gate } 27940Sstevel@tonic-gate } 27950Sstevel@tonic-gate 27960Sstevel@tonic-gate /* 27970Sstevel@tonic-gate * print_Table 27980Sstevel@tonic-gate * Write the detailed fdisk table to standard error for 27990Sstevel@tonic-gate * the selected disk device. 28000Sstevel@tonic-gate */ 2801251Slclee static void 2802251Slclee print_Table(void) 2803251Slclee { 28040Sstevel@tonic-gate int i; 28050Sstevel@tonic-gate 2806251Slclee (void) fprintf(stderr, 28070Sstevel@tonic-gate " SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT" 28080Sstevel@tonic-gate " NUMSECT\n"); 28090Sstevel@tonic-gate 28100Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2811251Slclee (void) fprintf(stderr, " %-5d ", Table[i].systid); 2812251Slclee (void) fprintf(stderr, "%-3d ", Table[i].bootid); 2813251Slclee (void) fprintf(stderr, "%-5d ", Table[i].beghead); 2814251Slclee (void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f); 2815*5169Slclee (void) fprintf(stderr, "%-8d ", 2816*5169Slclee (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl); 28170Sstevel@tonic-gate 2818251Slclee (void) fprintf(stderr, "%-5d ", Table[i].endhead); 2819251Slclee (void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f); 2820*5169Slclee (void) fprintf(stderr, "%-8d ", 2821*5169Slclee (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl); 2822251Slclee (void) fprintf(stderr, "%-9d ", lel(Table[i].relsect)); 2823251Slclee (void) fprintf(stderr, "%-9d\n", lel(Table[i].numsect)); 28240Sstevel@tonic-gate 28250Sstevel@tonic-gate } 28260Sstevel@tonic-gate } 28270Sstevel@tonic-gate 28280Sstevel@tonic-gate /* 28290Sstevel@tonic-gate * copy_Table_to_Old_Table 28300Sstevel@tonic-gate * Copy Table into Old_Table. The function only copies the systid, 28310Sstevel@tonic-gate * numsect, relsect, and bootid values because they are the only 28320Sstevel@tonic-gate * ones compared when determining if Table has changed. 28330Sstevel@tonic-gate */ 2834251Slclee static void 2835251Slclee copy_Table_to_Old_Table(void) 28360Sstevel@tonic-gate { 28370Sstevel@tonic-gate int i; 28380Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2839*5169Slclee (void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0])); 28400Sstevel@tonic-gate } 28410Sstevel@tonic-gate } 28420Sstevel@tonic-gate 28430Sstevel@tonic-gate /* 28440Sstevel@tonic-gate * nulltbl 28450Sstevel@tonic-gate * Zero out the systid, numsect, relsect, and bootid values in the 28460Sstevel@tonic-gate * fdisk table. 28470Sstevel@tonic-gate */ 2848251Slclee static void 2849251Slclee nulltbl(void) 28500Sstevel@tonic-gate { 28510Sstevel@tonic-gate int i; 28520Sstevel@tonic-gate 28530Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2854*5169Slclee Table[i].systid = UNUSED; 2855*5169Slclee Table[i].numsect = lel(UNUSED); 2856*5169Slclee Table[i].relsect = lel(UNUSED); 2857*5169Slclee Table[i].bootid = 0; 28580Sstevel@tonic-gate } 28590Sstevel@tonic-gate } 28600Sstevel@tonic-gate 28610Sstevel@tonic-gate /* 28620Sstevel@tonic-gate * copy_Bootblk_to_Table 28630Sstevel@tonic-gate * Copy the bytes from the boot record to an internal "Table". 28640Sstevel@tonic-gate * All unused are padded with zeros starting at offset 446. 28650Sstevel@tonic-gate */ 2866251Slclee static void 2867251Slclee copy_Bootblk_to_Table(void) 28680Sstevel@tonic-gate { 28690Sstevel@tonic-gate int i, j; 28700Sstevel@tonic-gate char *bootptr; 28710Sstevel@tonic-gate struct ipart iparts[FD_NUMPART]; 28720Sstevel@tonic-gate 28730Sstevel@tonic-gate /* Get an aligned copy of the partition tables */ 2874251Slclee (void) memcpy(iparts, Bootblk->parts, sizeof (iparts)); 28750Sstevel@tonic-gate bootptr = (char *)iparts; /* Points to start of partition table */ 28760Sstevel@tonic-gate if (les(Bootblk->signature) != MBB_MAGIC) { 28770Sstevel@tonic-gate /* Signature is missing */ 28780Sstevel@tonic-gate nulltbl(); 2879251Slclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 28800Sstevel@tonic-gate return; 28810Sstevel@tonic-gate } 28820Sstevel@tonic-gate /* 28830Sstevel@tonic-gate * When the DOS fdisk command deletes a partition, it is not 28840Sstevel@tonic-gate * recognized by the old algorithm. The algorithm that 28850Sstevel@tonic-gate * follows looks at each entry in the Bootrec and copies all 28860Sstevel@tonic-gate * those that are valid. 28870Sstevel@tonic-gate */ 28880Sstevel@tonic-gate j = 0; 28890Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 28900Sstevel@tonic-gate if (iparts[i].systid == 0) { 28910Sstevel@tonic-gate /* Null entry */ 28920Sstevel@tonic-gate bootptr += sizeof (struct ipart); 28930Sstevel@tonic-gate } else { 2894251Slclee fill_ipart(bootptr, &Table[j]); 28950Sstevel@tonic-gate j++; 28960Sstevel@tonic-gate bootptr += sizeof (struct ipart); 28970Sstevel@tonic-gate } 28980Sstevel@tonic-gate } 28990Sstevel@tonic-gate for (i = j; i < FD_NUMPART; i++) { 29000Sstevel@tonic-gate Table[i].systid = UNUSED; 29010Sstevel@tonic-gate Table[i].numsect = lel(UNUSED); 29020Sstevel@tonic-gate Table[i].relsect = lel(UNUSED); 29030Sstevel@tonic-gate Table[i].bootid = 0; 29040Sstevel@tonic-gate 29050Sstevel@tonic-gate } 29060Sstevel@tonic-gate /* For now, always replace the bootcode with ours */ 2907251Slclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 29080Sstevel@tonic-gate copy_Table_to_Bootblk(); 29090Sstevel@tonic-gate } 29100Sstevel@tonic-gate 29110Sstevel@tonic-gate /* 29120Sstevel@tonic-gate * fill_ipart 29130Sstevel@tonic-gate * Initialize ipart structure values. 29140Sstevel@tonic-gate */ 2915251Slclee static void 29160Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp) 29170Sstevel@tonic-gate { 29180Sstevel@tonic-gate #ifdef sparc 29190Sstevel@tonic-gate /* Packing struct ipart for Sparc */ 2920251Slclee partp->bootid = getbyte(&bootptr); 2921251Slclee partp->beghead = getbyte(&bootptr); 2922251Slclee partp->begsect = getbyte(&bootptr); 2923251Slclee partp->begcyl = getbyte(&bootptr); 2924251Slclee partp->systid = getbyte(&bootptr); 2925251Slclee partp->endhead = getbyte(&bootptr); 2926251Slclee partp->endsect = getbyte(&bootptr); 2927251Slclee partp->endcyl = getbyte(&bootptr); 2928251Slclee partp->relsect = (int32_t)getlong(&bootptr); 2929251Slclee partp->numsect = (int32_t)getlong(&bootptr); 29300Sstevel@tonic-gate #else 29310Sstevel@tonic-gate *partp = *(struct ipart *)bootptr; 29320Sstevel@tonic-gate #endif 29330Sstevel@tonic-gate } 29340Sstevel@tonic-gate 29350Sstevel@tonic-gate /* 2936251Slclee * getbyte, getlong 29370Sstevel@tonic-gate * Get a byte, a short, or a long (SPARC only). 29380Sstevel@tonic-gate */ 29390Sstevel@tonic-gate #ifdef sparc 2940251Slclee uchar_t 2941251Slclee getbyte(char **bp) 29420Sstevel@tonic-gate { 2943251Slclee uchar_t b; 2944251Slclee 2945251Slclee b = (uchar_t)**bp; 29460Sstevel@tonic-gate *bp = *bp + 1; 29470Sstevel@tonic-gate return (b); 29480Sstevel@tonic-gate } 29490Sstevel@tonic-gate 2950251Slclee uint32_t 2951251Slclee getlong(char **bp) 29520Sstevel@tonic-gate { 2953251Slclee int32_t b, bh, bl; 29540Sstevel@tonic-gate 29550Sstevel@tonic-gate bh = ((**bp) << 8) | *(*bp + 1); 29560Sstevel@tonic-gate *bp += 2; 29570Sstevel@tonic-gate bl = ((**bp) << 8) | *(*bp + 1); 29580Sstevel@tonic-gate *bp += 2; 29590Sstevel@tonic-gate 29600Sstevel@tonic-gate b = (bh << 16) | bl; 2961251Slclee return ((uint32_t)b); 29620Sstevel@tonic-gate } 29630Sstevel@tonic-gate #endif 29640Sstevel@tonic-gate 29650Sstevel@tonic-gate /* 29660Sstevel@tonic-gate * copy_Table_to_Bootblk 29670Sstevel@tonic-gate * Copy the table into the 512 boot record. Note that the unused 29680Sstevel@tonic-gate * entries will always be the last ones in the table and they are 29690Sstevel@tonic-gate * marked with 100 in sysind. The the unused portion of the table 29700Sstevel@tonic-gate * is padded with zeros in the bytes after the used entries. 29710Sstevel@tonic-gate */ 2972251Slclee static void 2973251Slclee copy_Table_to_Bootblk(void) 29740Sstevel@tonic-gate { 29750Sstevel@tonic-gate struct ipart *boot_ptr, *tbl_ptr; 29760Sstevel@tonic-gate 29770Sstevel@tonic-gate boot_ptr = (struct ipart *)Bootblk->parts; 29780Sstevel@tonic-gate tbl_ptr = (struct ipart *)&Table[0].bootid; 29790Sstevel@tonic-gate for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid; 29800Sstevel@tonic-gate tbl_ptr++, boot_ptr++) { 2981*5169Slclee if (tbl_ptr->systid == UNUSED) 2982*5169Slclee (void) memset(boot_ptr, 0, sizeof (struct ipart)); 2983*5169Slclee else 2984*5169Slclee (void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart)); 29850Sstevel@tonic-gate } 29860Sstevel@tonic-gate Bootblk->signature = les(MBB_MAGIC); 29870Sstevel@tonic-gate } 29880Sstevel@tonic-gate 29890Sstevel@tonic-gate /* 29900Sstevel@tonic-gate * TableChanged 29910Sstevel@tonic-gate * Check for any changes in the partition table. 29920Sstevel@tonic-gate */ 2993251Slclee static int 2994251Slclee TableChanged(void) 29950Sstevel@tonic-gate { 29960Sstevel@tonic-gate int i, changed; 29970Sstevel@tonic-gate 29980Sstevel@tonic-gate changed = 0; 29990Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 3000*5169Slclee if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) { 3001*5169Slclee /* Partition table changed, write back to disk */ 3002*5169Slclee changed = 1; 3003*5169Slclee } 30040Sstevel@tonic-gate } 30050Sstevel@tonic-gate 30060Sstevel@tonic-gate return (changed); 30070Sstevel@tonic-gate } 30080Sstevel@tonic-gate 30090Sstevel@tonic-gate /* 30100Sstevel@tonic-gate * ffile_write 30110Sstevel@tonic-gate * Display contents of partition table to standard output or 30120Sstevel@tonic-gate * another file name without writing it to the disk (-W file). 30130Sstevel@tonic-gate */ 3014251Slclee static void 3015251Slclee ffile_write(char *file) 30160Sstevel@tonic-gate { 30170Sstevel@tonic-gate register int i; 30180Sstevel@tonic-gate FILE *fp; 30190Sstevel@tonic-gate 30200Sstevel@tonic-gate /* 30210Sstevel@tonic-gate * If file isn't standard output, then it's a file name. 30220Sstevel@tonic-gate * Open file and write it. 30230Sstevel@tonic-gate */ 30240Sstevel@tonic-gate if (file != (char *)stdout) { 3025*5169Slclee if ((fp = fopen(file, "w")) == NULL) { 3026*5169Slclee (void) fprintf(stderr, 3027*5169Slclee "fdisk: Cannot open output file %s.\n", 3028*5169Slclee file); 3029*5169Slclee exit(1); 3030*5169Slclee } 30310Sstevel@tonic-gate } 30320Sstevel@tonic-gate else 3033*5169Slclee fp = stdout; 30340Sstevel@tonic-gate 30350Sstevel@tonic-gate /* 30360Sstevel@tonic-gate * Write the fdisk table information 30370Sstevel@tonic-gate */ 3038251Slclee (void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev); 3039251Slclee (void) fprintf(fp, "* Dimensions:\n"); 3040251Slclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 3041251Slclee (void) fprintf(fp, "* %4d sectors/track\n", sectors); 3042251Slclee (void) fprintf(fp, "* %4d tracks/cylinder\n", heads); 3043251Slclee (void) fprintf(fp, "* %4d cylinders\n", Numcyl); 3044251Slclee (void) fprintf(fp, "*\n"); 30450Sstevel@tonic-gate /* Write virtual (HBA) geometry, if required */ 30460Sstevel@tonic-gate if (v_flag) { 3047251Slclee (void) fprintf(fp, "* HBA Dimensions:\n"); 3048251Slclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 3049251Slclee (void) fprintf(fp, "* %4d sectors/track\n", hba_sectors); 3050251Slclee (void) fprintf(fp, "* %4d tracks/cylinder\n", hba_heads); 3051251Slclee (void) fprintf(fp, "* %4d cylinders\n", hba_Numcyl); 3052251Slclee (void) fprintf(fp, "*\n"); 30530Sstevel@tonic-gate } 3054251Slclee (void) fprintf(fp, "* systid:\n"); 3055251Slclee (void) fprintf(fp, "* 1: DOSOS12\n"); 3056251Slclee (void) fprintf(fp, "* 2: PCIXOS\n"); 3057251Slclee (void) fprintf(fp, "* 4: DOSOS16\n"); 3058251Slclee (void) fprintf(fp, "* 5: EXTDOS\n"); 3059251Slclee (void) fprintf(fp, "* 6: DOSBIG\n"); 3060251Slclee (void) fprintf(fp, "* 7: FDISK_IFS\n"); 3061251Slclee (void) fprintf(fp, "* 8: FDISK_AIXBOOT\n"); 3062251Slclee (void) fprintf(fp, "* 9: FDISK_AIXDATA\n"); 3063251Slclee (void) fprintf(fp, "* 10: FDISK_0S2BOOT\n"); 3064251Slclee (void) fprintf(fp, "* 11: FDISK_WINDOWS\n"); 3065251Slclee (void) fprintf(fp, "* 12: FDISK_EXT_WIN\n"); 3066251Slclee (void) fprintf(fp, "* 14: FDISK_FAT95\n"); 3067251Slclee (void) fprintf(fp, "* 15: FDISK_EXTLBA\n"); 3068251Slclee (void) fprintf(fp, "* 18: DIAGPART\n"); 3069251Slclee (void) fprintf(fp, "* 65: FDISK_LINUX\n"); 3070251Slclee (void) fprintf(fp, "* 82: FDISK_CPM\n"); 3071251Slclee (void) fprintf(fp, "* 86: DOSDATA\n"); 3072251Slclee (void) fprintf(fp, "* 98: OTHEROS\n"); 3073251Slclee (void) fprintf(fp, "* 99: UNIXOS\n"); 3074251Slclee (void) fprintf(fp, "* 101: FDISK_NOVELL3\n"); 3075251Slclee (void) fprintf(fp, "* 119: FDISK_QNX4\n"); 3076251Slclee (void) fprintf(fp, "* 120: FDISK_QNX42\n"); 3077251Slclee (void) fprintf(fp, "* 121: FDISK_QNX43\n"); 3078251Slclee (void) fprintf(fp, "* 130: SUNIXOS\n"); 3079251Slclee (void) fprintf(fp, "* 131: FDISK_LINUXNAT\n"); 3080251Slclee (void) fprintf(fp, "* 134: FDISK_NTFSVOL1\n"); 3081251Slclee (void) fprintf(fp, "* 135: FDISK_NTFSVOL2\n"); 3082251Slclee (void) fprintf(fp, "* 165: FDISK_BSD\n"); 3083251Slclee (void) fprintf(fp, "* 167: FDISK_NEXTSTEP\n"); 3084251Slclee (void) fprintf(fp, "* 183: FDISK_BSDIFS\n"); 3085251Slclee (void) fprintf(fp, "* 184: FDISK_BSDISWAP\n"); 3086251Slclee (void) fprintf(fp, "* 190: X86BOOT\n"); 3087251Slclee (void) fprintf(fp, "* 191: SUNIXOS2\n"); 3088251Slclee (void) fprintf(fp, "* 238: EFI_PMBR\n"); 3089251Slclee (void) fprintf(fp, "* 239: EFI_FS\n"); 3090251Slclee (void) fprintf(fp, "*\n"); 3091251Slclee (void) fprintf(fp, 30920Sstevel@tonic-gate "\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl" 30930Sstevel@tonic-gate " Rsect Numsect\n"); 30940Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 30950Sstevel@tonic-gate if (Table[i].systid != UNUSED) 3096251Slclee (void) fprintf(fp, 30970Sstevel@tonic-gate " %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-8d" 30980Sstevel@tonic-gate " %-8d\n", 30990Sstevel@tonic-gate Table[i].systid, 31000Sstevel@tonic-gate Table[i].bootid, 31010Sstevel@tonic-gate Table[i].beghead, 31020Sstevel@tonic-gate Table[i].begsect & 0x3f, 31030Sstevel@tonic-gate ((Table[i].begcyl & 0xff) | ((Table[i].begsect & 3104*5169Slclee 0xc0) << 2)), 31050Sstevel@tonic-gate Table[i].endhead, 31060Sstevel@tonic-gate Table[i].endsect & 0x3f, 31070Sstevel@tonic-gate ((Table[i].endcyl & 0xff) | ((Table[i].endsect & 3108*5169Slclee 0xc0) << 2)), 31090Sstevel@tonic-gate lel(Table[i].relsect), 31100Sstevel@tonic-gate lel(Table[i].numsect)); 31110Sstevel@tonic-gate } 31120Sstevel@tonic-gate if (fp != stdout) 3113251Slclee (void) fclose(fp); 31140Sstevel@tonic-gate } 31150Sstevel@tonic-gate 31160Sstevel@tonic-gate /* 31170Sstevel@tonic-gate * fix_slice 31180Sstevel@tonic-gate * Read the VTOC table on the Solaris partition and check that no 31190Sstevel@tonic-gate * slices exist that extend past the end of the Solaris partition. 31200Sstevel@tonic-gate * If no Solaris partition exists, nothing is done. 31210Sstevel@tonic-gate */ 3122251Slclee static void 3123251Slclee fix_slice(void) 31240Sstevel@tonic-gate { 31250Sstevel@tonic-gate int i; 31260Sstevel@tonic-gate int numsect; 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate if (io_image) { 3129251Slclee return; 31300Sstevel@tonic-gate } 31310Sstevel@tonic-gate 31320Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 31330Sstevel@tonic-gate if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) { 31340Sstevel@tonic-gate /* 31350Sstevel@tonic-gate * Only the size matters (not starting point), since 31360Sstevel@tonic-gate * VTOC entries are relative to the start of 31370Sstevel@tonic-gate * the partition. 31380Sstevel@tonic-gate */ 31390Sstevel@tonic-gate numsect = lel(Table[i].numsect); 31400Sstevel@tonic-gate break; 31410Sstevel@tonic-gate } 31420Sstevel@tonic-gate } 31430Sstevel@tonic-gate 31440Sstevel@tonic-gate if (i >= FD_NUMPART) { 31450Sstevel@tonic-gate if (!io_nifdisk) { 31460Sstevel@tonic-gate (void) fprintf(stderr, 31470Sstevel@tonic-gate "fdisk: No Solaris partition found - VTOC not" 31480Sstevel@tonic-gate " checked.\n"); 31490Sstevel@tonic-gate } 3150251Slclee return; 31510Sstevel@tonic-gate } 31520Sstevel@tonic-gate 3153251Slclee if (readvtoc() != VTOC_OK) { 31540Sstevel@tonic-gate exit(1); /* Failed to read the VTOC */ 3155*5169Slclee } 3156*5169Slclee for (i = 0; i < V_NUMPAR; i++) { 3157*5169Slclee /* Special case for slice two (entire disk) */ 3158*5169Slclee if (i == 2) { 3159*5169Slclee if (disk_vtoc.v_part[i].p_start != 0) { 3160*5169Slclee (void) fprintf(stderr, 3161*5169Slclee "slice %d starts at %ld, is not at" 3162*5169Slclee " start of partition", 3163*5169Slclee i, disk_vtoc.v_part[i].p_start); 3164*5169Slclee if (!io_nifdisk) { 3165*5169Slclee (void) printf(" adjust ?:"); 3166*5169Slclee if (yesno()) 31670Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 3168*5169Slclee } else { 3169*5169Slclee disk_vtoc.v_part[i].p_start = 0; 3170*5169Slclee (void) fprintf(stderr, " adjusted!\n"); 31710Sstevel@tonic-gate } 3172*5169Slclee 3173*5169Slclee } 3174*5169Slclee if (disk_vtoc.v_part[i].p_size != numsect) { 3175*5169Slclee (void) fprintf(stderr, 3176*5169Slclee "slice %d size %ld does not cover" 3177*5169Slclee " complete partition", 3178*5169Slclee i, disk_vtoc.v_part[i].p_size); 3179*5169Slclee if (!io_nifdisk) { 3180*5169Slclee (void) printf(" adjust ?:"); 3181*5169Slclee if (yesno()) 31820Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 31830Sstevel@tonic-gate numsect; 3184*5169Slclee } else { 3185*5169Slclee disk_vtoc.v_part[i].p_size = numsect; 3186*5169Slclee (void) fprintf(stderr, " adjusted!\n"); 31870Sstevel@tonic-gate } 3188*5169Slclee } 3189*5169Slclee if (disk_vtoc.v_part[i].p_tag != V_BACKUP) { 3190*5169Slclee (void) fprintf(stderr, 3191*5169Slclee "slice %d tag was %d should be %d", 3192*5169Slclee i, disk_vtoc.v_part[i].p_tag, 3193*5169Slclee V_BACKUP); 3194*5169Slclee if (!io_nifdisk) { 3195251Slclee (void) printf(" fix ?:"); 3196*5169Slclee if (yesno()) 31970Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 31980Sstevel@tonic-gate V_BACKUP; 3199*5169Slclee } else { 32000Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = V_BACKUP; 32010Sstevel@tonic-gate (void) fprintf(stderr, " fixed!\n"); 32020Sstevel@tonic-gate } 3203*5169Slclee } 3204*5169Slclee continue; 3205*5169Slclee } 3206*5169Slclee if (io_ADJT) { 3207*5169Slclee if (disk_vtoc.v_part[i].p_start > numsect || 3208*5169Slclee disk_vtoc.v_part[i].p_start + 3209*5169Slclee disk_vtoc.v_part[i].p_size > numsect) { 3210*5169Slclee (void) fprintf(stderr, 3211*5169Slclee "slice %d (start %ld, end %ld)" 3212*5169Slclee " is larger than the partition", 3213*5169Slclee i, disk_vtoc.v_part[i].p_start, 3214*5169Slclee disk_vtoc.v_part[i].p_start + 3215*5169Slclee disk_vtoc.v_part[i].p_size); 3216*5169Slclee if (!io_nifdisk) { 3217*5169Slclee (void) printf(" remove ?:"); 3218*5169Slclee if (yesno()) { 32190Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 32200Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 32210Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 32220Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 32230Sstevel@tonic-gate } 32240Sstevel@tonic-gate } else { 3225*5169Slclee disk_vtoc.v_part[i].p_size = 0; 3226*5169Slclee disk_vtoc.v_part[i].p_start = 0; 3227*5169Slclee disk_vtoc.v_part[i].p_tag = 0; 3228*5169Slclee disk_vtoc.v_part[i].p_flag = 0; 32290Sstevel@tonic-gate (void) fprintf(stderr, 3230*5169Slclee " removed!\n"); 3231*5169Slclee } 3232*5169Slclee } 3233*5169Slclee continue; 3234*5169Slclee } 3235*5169Slclee if (disk_vtoc.v_part[i].p_start > numsect) { 3236*5169Slclee (void) fprintf(stderr, 3237*5169Slclee "slice %d (start %ld) is larger than the partition", 3238*5169Slclee i, disk_vtoc.v_part[i].p_start); 3239*5169Slclee if (!io_nifdisk) { 3240*5169Slclee (void) printf(" remove ?:"); 3241*5169Slclee if (yesno()) { 3242*5169Slclee disk_vtoc.v_part[i].p_size = 0; 3243*5169Slclee disk_vtoc.v_part[i].p_start = 0; 3244*5169Slclee disk_vtoc.v_part[i].p_tag = 0; 3245*5169Slclee disk_vtoc.v_part[i].p_flag = 0; 32460Sstevel@tonic-gate } 3247*5169Slclee } else { 3248*5169Slclee disk_vtoc.v_part[i].p_size = 0; 3249*5169Slclee disk_vtoc.v_part[i].p_start = 0; 3250*5169Slclee disk_vtoc.v_part[i].p_tag = 0; 3251*5169Slclee disk_vtoc.v_part[i].p_flag = 0; 3252*5169Slclee (void) fprintf(stderr, 3253*5169Slclee " removed!\n"); 3254*5169Slclee } 3255*5169Slclee } else if (disk_vtoc.v_part[i].p_start 3256*5169Slclee + disk_vtoc.v_part[i].p_size > numsect) { 3257*5169Slclee (void) fprintf(stderr, 3258*5169Slclee "slice %d (end %ld) is larger" 3259*5169Slclee " than the partition", 3260*5169Slclee i, 3261*5169Slclee disk_vtoc.v_part[i].p_start + 3262*5169Slclee disk_vtoc.v_part[i].p_size); 3263*5169Slclee if (!io_nifdisk) { 3264*5169Slclee (void) printf(" adjust ?:"); 3265*5169Slclee if (yesno()) { 3266*5169Slclee disk_vtoc.v_part[i].p_size = numsect; 3267*5169Slclee } 3268*5169Slclee } else { 3269*5169Slclee disk_vtoc.v_part[i].p_size = numsect; 3270*5169Slclee (void) fprintf(stderr, " adjusted!\n"); 32710Sstevel@tonic-gate } 32720Sstevel@tonic-gate } 32730Sstevel@tonic-gate } 32740Sstevel@tonic-gate #if 1 /* bh for now */ 32750Sstevel@tonic-gate /* Make the VTOC look sane - ha ha */ 32760Sstevel@tonic-gate disk_vtoc.v_version = V_VERSION; 32770Sstevel@tonic-gate disk_vtoc.v_sanity = VTOC_SANE; 32780Sstevel@tonic-gate disk_vtoc.v_nparts = V_NUMPAR; 32790Sstevel@tonic-gate if (disk_vtoc.v_sectorsz == 0) 32800Sstevel@tonic-gate disk_vtoc.v_sectorsz = NBPSCTR; 32810Sstevel@tonic-gate #endif 32820Sstevel@tonic-gate 32830Sstevel@tonic-gate /* Write the VTOC back to the disk */ 32840Sstevel@tonic-gate if (!io_readonly) 3285251Slclee (void) writevtoc(); 32860Sstevel@tonic-gate } 32870Sstevel@tonic-gate 32880Sstevel@tonic-gate /* 32890Sstevel@tonic-gate * yesno 32900Sstevel@tonic-gate * Get yes or no answer. Return 1 for yes and 0 for no. 32910Sstevel@tonic-gate */ 32920Sstevel@tonic-gate 3293251Slclee static int 3294251Slclee yesno(void) 32950Sstevel@tonic-gate { 32960Sstevel@tonic-gate char s[80]; 32970Sstevel@tonic-gate 32980Sstevel@tonic-gate for (;;) { 3299251Slclee (void) gets(s); 33000Sstevel@tonic-gate rm_blanks(s); 33010Sstevel@tonic-gate if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) { 3302251Slclee (void) printf(E_LINE); 3303251Slclee (void) printf("Please answer with \"y\" or \"n\": "); 33040Sstevel@tonic-gate continue; 33050Sstevel@tonic-gate } 33060Sstevel@tonic-gate if (s[0] == 'y') 33070Sstevel@tonic-gate return (1); 33080Sstevel@tonic-gate else 33090Sstevel@tonic-gate return (0); 33100Sstevel@tonic-gate } 33110Sstevel@tonic-gate } 33120Sstevel@tonic-gate 33130Sstevel@tonic-gate /* 33140Sstevel@tonic-gate * readvtoc 33150Sstevel@tonic-gate * Read the VTOC from the Solaris partition of the device. 33160Sstevel@tonic-gate */ 3317251Slclee static int 3318251Slclee readvtoc(void) 33190Sstevel@tonic-gate { 33200Sstevel@tonic-gate int i; 33210Sstevel@tonic-gate int retval = VTOC_OK; 33220Sstevel@tonic-gate 33230Sstevel@tonic-gate if ((i = read_vtoc(Dev, &disk_vtoc)) < VTOC_OK) { 33240Sstevel@tonic-gate if (i == VT_EINVAL) { 33250Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Invalid VTOC.\n"); 33260Sstevel@tonic-gate vt_inval++; 33270Sstevel@tonic-gate retval = VTOC_INVAL; 33280Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 33290Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 3330*5169Slclee "GPT\n"); 33310Sstevel@tonic-gate retval = VTOC_NOTSUP; 33320Sstevel@tonic-gate } else { 33330Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot read VTOC.\n"); 33340Sstevel@tonic-gate retval = VTOC_RWERR; 33350Sstevel@tonic-gate } 33360Sstevel@tonic-gate } 33370Sstevel@tonic-gate return (retval); 33380Sstevel@tonic-gate } 33390Sstevel@tonic-gate 33400Sstevel@tonic-gate /* 33410Sstevel@tonic-gate * writevtoc 33420Sstevel@tonic-gate * Write the VTOC to the Solaris partition on the device. 33430Sstevel@tonic-gate */ 3344251Slclee static int 3345251Slclee writevtoc(void) 33460Sstevel@tonic-gate { 33470Sstevel@tonic-gate int i; 33480Sstevel@tonic-gate int retval = 0; 33490Sstevel@tonic-gate 33500Sstevel@tonic-gate if ((i = write_vtoc(Dev, &disk_vtoc)) != 0) { 33510Sstevel@tonic-gate if (i == VT_EINVAL) { 33520Sstevel@tonic-gate (void) fprintf(stderr, 33530Sstevel@tonic-gate "fdisk: Invalid entry exists in VTOC.\n"); 33540Sstevel@tonic-gate retval = VTOC_INVAL; 33550Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 33560Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 3357*5169Slclee "GPT\n"); 33580Sstevel@tonic-gate retval = VTOC_NOTSUP; 33590Sstevel@tonic-gate } else { 33600Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot write VTOC.\n"); 33610Sstevel@tonic-gate retval = VTOC_RWERR; 33620Sstevel@tonic-gate } 33630Sstevel@tonic-gate } 33640Sstevel@tonic-gate return (retval); 33650Sstevel@tonic-gate } 33660Sstevel@tonic-gate 33670Sstevel@tonic-gate /* 33680Sstevel@tonic-gate * efi_ioctl 33690Sstevel@tonic-gate * issues DKIOCSETEFI IOCTL 33700Sstevel@tonic-gate * (duplicate of private efi_ioctl() in rdwr_efi.c 33710Sstevel@tonic-gate */ 33720Sstevel@tonic-gate static int 33730Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 33740Sstevel@tonic-gate { 33750Sstevel@tonic-gate void *data = dk_ioc->dki_data; 33760Sstevel@tonic-gate int error; 33770Sstevel@tonic-gate 33780Sstevel@tonic-gate dk_ioc->dki_data_64 = (uintptr_t)data; 33790Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc); 33800Sstevel@tonic-gate 33810Sstevel@tonic-gate return (error); 33820Sstevel@tonic-gate } 33830Sstevel@tonic-gate 33840Sstevel@tonic-gate /* 33850Sstevel@tonic-gate * clear_efi 33860Sstevel@tonic-gate * Clear EFI labels from the EFI_PMBR partition on the device 33870Sstevel@tonic-gate * This function is modeled on the libefi(3LIB) call efi_write() 33880Sstevel@tonic-gate */ 3389251Slclee static int 3390251Slclee clear_efi(void) 33910Sstevel@tonic-gate { 33920Sstevel@tonic-gate struct dk_gpt *efi_vtoc; 33930Sstevel@tonic-gate dk_efi_t dk_ioc; 33940Sstevel@tonic-gate 33950Sstevel@tonic-gate /* 33960Sstevel@tonic-gate * see if we can read the EFI label 33970Sstevel@tonic-gate */ 33980Sstevel@tonic-gate if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) { 33990Sstevel@tonic-gate return (VT_ERROR); 34000Sstevel@tonic-gate } 34010Sstevel@tonic-gate 34020Sstevel@tonic-gate /* 34030Sstevel@tonic-gate * set up the dk_ioc structure for writing 34040Sstevel@tonic-gate */ 34050Sstevel@tonic-gate dk_ioc.dki_lba = 1; 34060Sstevel@tonic-gate dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize; 34070Sstevel@tonic-gate 34080Sstevel@tonic-gate if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) { 34090Sstevel@tonic-gate return (VT_ERROR); 34100Sstevel@tonic-gate } 34110Sstevel@tonic-gate 34120Sstevel@tonic-gate /* 34130Sstevel@tonic-gate * clear the primary label 34140Sstevel@tonic-gate */ 34150Sstevel@tonic-gate if (io_debug) { 3416251Slclee (void) fprintf(stderr, 3417251Slclee "\tClearing primary EFI label at block %lld\n", 3418251Slclee dk_ioc.dki_lba); 34190Sstevel@tonic-gate } 34200Sstevel@tonic-gate 34210Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 34220Sstevel@tonic-gate free(dk_ioc.dki_data); 34230Sstevel@tonic-gate switch (errno) { 34240Sstevel@tonic-gate case EIO: 34250Sstevel@tonic-gate return (VT_EIO); 34260Sstevel@tonic-gate case EINVAL: 34270Sstevel@tonic-gate return (VT_EINVAL); 34280Sstevel@tonic-gate default: 34290Sstevel@tonic-gate return (VT_ERROR); 34300Sstevel@tonic-gate } 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate 34330Sstevel@tonic-gate /* 34340Sstevel@tonic-gate * clear the backup partition table 34350Sstevel@tonic-gate */ 34360Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1; 34370Sstevel@tonic-gate dk_ioc.dki_length -= efi_vtoc->efi_lbasize; 34380Sstevel@tonic-gate dk_ioc.dki_data++; 34390Sstevel@tonic-gate if (io_debug) { 3440251Slclee (void) fprintf(stderr, 3441251Slclee "\tClearing backup partition table at block %lld\n", 3442251Slclee dk_ioc.dki_lba); 34430Sstevel@tonic-gate } 34440Sstevel@tonic-gate 34450Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 34460Sstevel@tonic-gate (void) fprintf(stderr, "\tUnable to clear backup EFI label at " 3447*5169Slclee "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1, 3448*5169Slclee errno); 34490Sstevel@tonic-gate } 34500Sstevel@tonic-gate 34510Sstevel@tonic-gate /* 34520Sstevel@tonic-gate * clear the backup label 34530Sstevel@tonic-gate */ 34540Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_lba; 34550Sstevel@tonic-gate dk_ioc.dki_length = efi_vtoc->efi_lbasize; 34560Sstevel@tonic-gate dk_ioc.dki_data--; 34570Sstevel@tonic-gate if (io_debug) { 3458251Slclee (void) fprintf(stderr, "\tClearing backup label at block " 3459251Slclee "%lld\n", dk_ioc.dki_lba); 34600Sstevel@tonic-gate } 34610Sstevel@tonic-gate 34620Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 3463251Slclee (void) fprintf(stderr, 3464251Slclee "\tUnable to clear backup EFI label at " 3465251Slclee "block %llu; errno %d\n", 3466251Slclee efi_vtoc->efi_last_lba, 3467251Slclee errno); 34680Sstevel@tonic-gate } 34690Sstevel@tonic-gate 34700Sstevel@tonic-gate free(dk_ioc.dki_data); 34710Sstevel@tonic-gate efi_free(efi_vtoc); 34720Sstevel@tonic-gate 34730Sstevel@tonic-gate return (0); 34740Sstevel@tonic-gate } 34750Sstevel@tonic-gate 34760Sstevel@tonic-gate /* 34770Sstevel@tonic-gate * clear_vtoc 34780Sstevel@tonic-gate * Clear the VTOC from the current or previous Solaris partition on the 34790Sstevel@tonic-gate * device. 34800Sstevel@tonic-gate */ 3481251Slclee static void 34820Sstevel@tonic-gate clear_vtoc(int table, int part) 34830Sstevel@tonic-gate { 34840Sstevel@tonic-gate struct ipart *clr_table; 34850Sstevel@tonic-gate struct dk_label disk_label; 34860Sstevel@tonic-gate int pcyl, ncyl, backup_block, solaris_offset, count, bytes, seek_byte; 34870Sstevel@tonic-gate 34880Sstevel@tonic-gate #ifdef DEBUG 34890Sstevel@tonic-gate struct dk_label read_label; 34900Sstevel@tonic-gate #endif /* DEBUG */ 34910Sstevel@tonic-gate 34920Sstevel@tonic-gate if (table == OLD) { 34930Sstevel@tonic-gate clr_table = &Old_Table[part]; 34940Sstevel@tonic-gate } else { 34950Sstevel@tonic-gate clr_table = &Table[part]; 34960Sstevel@tonic-gate } 34970Sstevel@tonic-gate 3498251Slclee (void) memset(&disk_label, 0, sizeof (struct dk_label)); 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate seek_byte = (lel(clr_table->relsect) * sectsiz) + VTOC_OFFSET; 35010Sstevel@tonic-gate 35020Sstevel@tonic-gate if (io_debug) { 3503251Slclee (void) fprintf(stderr, "\tClearing primary VTOC at byte %d\n", 35040Sstevel@tonic-gate seek_byte); 35050Sstevel@tonic-gate } 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3508251Slclee (void) fprintf(stderr, 3509251Slclee "\tError seeking to primary label at byte %d\n", 35100Sstevel@tonic-gate seek_byte); 3511251Slclee return; 35120Sstevel@tonic-gate } 35130Sstevel@tonic-gate 35140Sstevel@tonic-gate bytes = write(Dev, &disk_label, sizeof (struct dk_label)); 35150Sstevel@tonic-gate 35160Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3517251Slclee (void) fprintf(stderr, 3518251Slclee "\tWarning: only %d bytes written to clear primary VTOC!\n", 3519251Slclee bytes); 35200Sstevel@tonic-gate } 35210Sstevel@tonic-gate 35220Sstevel@tonic-gate #ifdef DEBUG 35230Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3524251Slclee (void) fprintf(stderr, 3525251Slclee "DEBUG: Error seeking to primary label at byte %d\n", 3526251Slclee seek_byte); 3527251Slclee return; 35280Sstevel@tonic-gate } else { 3529251Slclee (void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n", 35300Sstevel@tonic-gate seek_byte); 35310Sstevel@tonic-gate } 35320Sstevel@tonic-gate 35330Sstevel@tonic-gate bytes = read(Dev, &read_label, sizeof (struct dk_label)); 35340Sstevel@tonic-gate 35350Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3536251Slclee (void) fprintf(stderr, 3537251Slclee "DEBUG: Warning: only %d bytes read of label\n", 35380Sstevel@tonic-gate bytes); 35390Sstevel@tonic-gate } 35400Sstevel@tonic-gate 35410Sstevel@tonic-gate if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) { 3542251Slclee (void) fprintf(stderr, 3543251Slclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 35440Sstevel@tonic-gate } else { 3545251Slclee (void) fprintf(stderr, "DEBUG Good compare of disk_label and " 35460Sstevel@tonic-gate "read_label\n"); 35470Sstevel@tonic-gate } 35480Sstevel@tonic-gate #endif /* DEBUG */ 35490Sstevel@tonic-gate 35500Sstevel@tonic-gate /* Clear backup label */ 35510Sstevel@tonic-gate pcyl = lel(clr_table->numsect) / (heads * sectors); 35520Sstevel@tonic-gate solaris_offset = lel(clr_table->relsect); 35530Sstevel@tonic-gate ncyl = pcyl - acyl; 35540Sstevel@tonic-gate 35550Sstevel@tonic-gate backup_block = ((ncyl + acyl - 1) * 35560Sstevel@tonic-gate (heads * sectors)) + ((heads - 1) * sectors) + 1; 35570Sstevel@tonic-gate 35580Sstevel@tonic-gate for (count = 1; count < 6; count++) { 35590Sstevel@tonic-gate seek_byte = (solaris_offset + backup_block) * 512; 35600Sstevel@tonic-gate 35610Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3562251Slclee (void) fprintf(stderr, 35630Sstevel@tonic-gate "\tError seeking to backup label at byte %d on " 35640Sstevel@tonic-gate "%s.\n", seek_byte, Dfltdev); 3565251Slclee return; 35660Sstevel@tonic-gate } 35670Sstevel@tonic-gate 35680Sstevel@tonic-gate if (io_debug) { 3569251Slclee (void) fprintf(stderr, "\tClearing backup VTOC at" 35700Sstevel@tonic-gate " byte %d (block %d)\n", 35710Sstevel@tonic-gate (solaris_offset + backup_block) * 512, 35720Sstevel@tonic-gate (solaris_offset + backup_block)); 35730Sstevel@tonic-gate } 35740Sstevel@tonic-gate 35750Sstevel@tonic-gate bytes = write(Dev, &disk_label, sizeof (struct dk_label)); 35760Sstevel@tonic-gate 35770Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3578251Slclee (void) fprintf(stderr, 3579251Slclee "\t\tWarning: only %d bytes written to " 35800Sstevel@tonic-gate "clear backup VTOC at block %d!\n", bytes, 35810Sstevel@tonic-gate (solaris_offset + backup_block)); 35820Sstevel@tonic-gate } 35830Sstevel@tonic-gate 35840Sstevel@tonic-gate #ifdef DEBUG 35850Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3586251Slclee (void) fprintf(stderr, 3587251Slclee "DEBUG: Error seeking to backup label at byte %d\n", 3588251Slclee seek_byte); 3589251Slclee return; 35900Sstevel@tonic-gate } else { 3591251Slclee (void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n", 35920Sstevel@tonic-gate seek_byte); 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate 35950Sstevel@tonic-gate bytes = read(Dev, &read_label, sizeof (struct dk_label)); 35960Sstevel@tonic-gate 35970Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3598251Slclee (void) fprintf(stderr, 3599251Slclee "DEBUG: Warning: only %d bytes read of backup label\n", 3600251Slclee bytes); 36010Sstevel@tonic-gate } 36020Sstevel@tonic-gate 36030Sstevel@tonic-gate if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) { 3604251Slclee (void) fprintf(stderr, 3605251Slclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 36060Sstevel@tonic-gate } else { 3607251Slclee (void) fprintf(stderr, 3608251Slclee "DEBUG: Good compare of disk_label and backup " 36090Sstevel@tonic-gate "read_label\n"); 36100Sstevel@tonic-gate } 36110Sstevel@tonic-gate #endif /* DEBUG */ 36120Sstevel@tonic-gate 36130Sstevel@tonic-gate backup_block += 2; 36140Sstevel@tonic-gate } 36150Sstevel@tonic-gate } 36160Sstevel@tonic-gate 36170Sstevel@tonic-gate #define FDISK_STANDARD_LECTURE \ 36180Sstevel@tonic-gate "Fdisk is normally used with the device that " \ 36190Sstevel@tonic-gate "represents the entire fixed disk.\n" \ 36200Sstevel@tonic-gate "(For example, /dev/rdsk/c0d0p0 on x86 or " \ 36210Sstevel@tonic-gate "/dev/rdsk/c0t5d0s2 on sparc).\n" 36220Sstevel@tonic-gate 36230Sstevel@tonic-gate #define FDISK_LECTURE_NOT_SECTOR_ZERO \ 36240Sstevel@tonic-gate "The device does not appear to include absolute\n" \ 36250Sstevel@tonic-gate "sector 0 of the PHYSICAL disk " \ 36260Sstevel@tonic-gate "(the normal location for an fdisk table).\n" 36270Sstevel@tonic-gate 36280Sstevel@tonic-gate #define FDISK_LECTURE_NOT_FULL \ 36290Sstevel@tonic-gate "The device does not appear to encompass the entire PHYSICAL disk.\n" 36300Sstevel@tonic-gate 36310Sstevel@tonic-gate #define FDISK_LECTURE_NO_VTOC \ 36320Sstevel@tonic-gate "Unable to find a volume table of contents.\n" \ 36330Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 36340Sstevel@tonic-gate 36350Sstevel@tonic-gate #define FDISK_LECTURE_NO_GEOM \ 36360Sstevel@tonic-gate "Unable to get geometry from device.\n" \ 36370Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 36380Sstevel@tonic-gate 36390Sstevel@tonic-gate #define FDISK_SHALL_I_CONTINUE \ 36400Sstevel@tonic-gate "Are you sure you want to continue? (y/n) " 36410Sstevel@tonic-gate 36420Sstevel@tonic-gate /* 36430Sstevel@tonic-gate * lecture_and_query 36440Sstevel@tonic-gate * Called when a sanity check fails. This routine gives a warning 36450Sstevel@tonic-gate * specific to the check that fails, followed by a generic lecture 36460Sstevel@tonic-gate * about the "right" device to supply as input. Then, if appropriate, 36470Sstevel@tonic-gate * it will prompt the user on whether or not they want to continue. 36480Sstevel@tonic-gate * Inappropriate times for prompting are when the user has selected 36490Sstevel@tonic-gate * non-interactive mode or read-only mode. 36500Sstevel@tonic-gate */ 3651251Slclee static int 36520Sstevel@tonic-gate lecture_and_query(char *warning, char *devname) 36530Sstevel@tonic-gate { 36540Sstevel@tonic-gate if (io_nifdisk) 36550Sstevel@tonic-gate return (0); 36560Sstevel@tonic-gate 3657251Slclee (void) fprintf(stderr, "WARNING: Device %s: \n", devname); 3658251Slclee (void) fprintf(stderr, "%s", warning); 3659251Slclee (void) fprintf(stderr, FDISK_STANDARD_LECTURE); 3660251Slclee (void) fprintf(stderr, FDISK_SHALL_I_CONTINUE); 36610Sstevel@tonic-gate 36620Sstevel@tonic-gate return (yesno()); 36630Sstevel@tonic-gate } 36640Sstevel@tonic-gate 3665251Slclee static void 36660Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd) 36670Sstevel@tonic-gate { 36680Sstevel@tonic-gate struct vtoc v; 36690Sstevel@tonic-gate struct dk_geom d; 36700Sstevel@tonic-gate struct part_info pi; 36710Sstevel@tonic-gate long totsize; 36720Sstevel@tonic-gate int idx = -1; 36730Sstevel@tonic-gate 36740Sstevel@tonic-gate /* 36750Sstevel@tonic-gate * First try the PARTINFO ioctl. If it works, we will be able 36760Sstevel@tonic-gate * to tell if they've specified the full disk partition by checking 36770Sstevel@tonic-gate * to see if they've specified a partition that starts at sector 0. 36780Sstevel@tonic-gate */ 36790Sstevel@tonic-gate if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) { 36800Sstevel@tonic-gate if (pi.p_start != 0) { 36810Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 36820Sstevel@tonic-gate devname)) { 36830Sstevel@tonic-gate (void) close(fd); 36840Sstevel@tonic-gate exit(1); 36850Sstevel@tonic-gate } 36860Sstevel@tonic-gate } 36870Sstevel@tonic-gate } else { 36880Sstevel@tonic-gate if ((idx = read_vtoc(fd, &v)) < 0) { 36890Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_VTOC, 36900Sstevel@tonic-gate devname)) { 36910Sstevel@tonic-gate (void) close(fd); 36920Sstevel@tonic-gate exit(1); 36930Sstevel@tonic-gate } 36940Sstevel@tonic-gate return; 36950Sstevel@tonic-gate } 36960Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &d) == -1) { 36970Sstevel@tonic-gate perror(devname); 36980Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_GEOM, 36990Sstevel@tonic-gate devname)) { 37000Sstevel@tonic-gate (void) close(fd); 37010Sstevel@tonic-gate exit(1); 37020Sstevel@tonic-gate } 37030Sstevel@tonic-gate return; 37040Sstevel@tonic-gate } 37050Sstevel@tonic-gate totsize = d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect; 37060Sstevel@tonic-gate if (v.v_part[idx].p_size != totsize) { 37070Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_FULL, 37080Sstevel@tonic-gate devname)) { 37090Sstevel@tonic-gate (void) close(fd); 37100Sstevel@tonic-gate exit(1); 37110Sstevel@tonic-gate } 37120Sstevel@tonic-gate } 37130Sstevel@tonic-gate } 37140Sstevel@tonic-gate } 37150Sstevel@tonic-gate 37160Sstevel@tonic-gate 37170Sstevel@tonic-gate /* 37180Sstevel@tonic-gate * get_node 37190Sstevel@tonic-gate * Called from main to construct the name of the device node to open. 37200Sstevel@tonic-gate * Initially tries to stat the node exactly as provided, if that fails 37210Sstevel@tonic-gate * we prepend the default path (/dev/rdsk/). 37220Sstevel@tonic-gate */ 37230Sstevel@tonic-gate static char * 37240Sstevel@tonic-gate get_node(char *devname) 37250Sstevel@tonic-gate { 37260Sstevel@tonic-gate char *node; 37270Sstevel@tonic-gate struct stat statbuf; 37280Sstevel@tonic-gate size_t space; 37290Sstevel@tonic-gate 37300Sstevel@tonic-gate /* Don't do anything if we are skipping device checks */ 37310Sstevel@tonic-gate if (io_image) 37320Sstevel@tonic-gate return (devname); 37330Sstevel@tonic-gate 37340Sstevel@tonic-gate node = devname; 37350Sstevel@tonic-gate 37360Sstevel@tonic-gate /* Try the node as provided first */ 37370Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 37380Sstevel@tonic-gate /* 37390Sstevel@tonic-gate * Copy the passed in string to a new buffer, prepend the 37400Sstevel@tonic-gate * default path and try again. 37410Sstevel@tonic-gate */ 37420Sstevel@tonic-gate space = strlen(DEFAULT_PATH) + strlen(devname) + 1; 37430Sstevel@tonic-gate 37440Sstevel@tonic-gate if ((node = malloc(space)) == NULL) { 3745251Slclee (void) fprintf(stderr, "fdisk: Unable to obtain memory " 37460Sstevel@tonic-gate "for device node.\n"); 37470Sstevel@tonic-gate exit(1); 37480Sstevel@tonic-gate } 37490Sstevel@tonic-gate 37500Sstevel@tonic-gate /* Copy over the default path and the provided node */ 37510Sstevel@tonic-gate (void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH)); 37520Sstevel@tonic-gate space -= strlen(DEFAULT_PATH); 37530Sstevel@tonic-gate (void) strlcpy(node + strlen(DEFAULT_PATH), devname, space); 37540Sstevel@tonic-gate 37550Sstevel@tonic-gate /* Try to stat it again */ 37560Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 37570Sstevel@tonic-gate /* Failed all options, give up */ 3758251Slclee (void) fprintf(stderr, 3759251Slclee "fdisk: Cannot stat device %s.\n", 37600Sstevel@tonic-gate devname); 37610Sstevel@tonic-gate exit(1); 37620Sstevel@tonic-gate } 37630Sstevel@tonic-gate } 37640Sstevel@tonic-gate 37650Sstevel@tonic-gate /* Make sure the device specified is the raw device */ 37660Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 3767251Slclee (void) fprintf(stderr, 3768251Slclee "fdisk: %s must be a raw device.\n", node); 37690Sstevel@tonic-gate exit(1); 37700Sstevel@tonic-gate } 37710Sstevel@tonic-gate 37720Sstevel@tonic-gate return (node); 37730Sstevel@tonic-gate } 3774