17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51237e847Slclee * Common Development and Distribution License (the "License"). 61237e847Slclee * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 211237e847Slclee 227c478bd9Sstevel@tonic-gate /* 232f8ec719SBarry Harding * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 327c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * PROGRAM: fdisk(1M) 367c478bd9Sstevel@tonic-gate * This program reads the partition table on the specified device and 377c478bd9Sstevel@tonic-gate * also reads the drive parameters. The user can perform various 387c478bd9Sstevel@tonic-gate * operations from a supplied menu or from the command line. Diagnostic 397c478bd9Sstevel@tonic-gate * options are also available. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate #include <stdio.h> 427c478bd9Sstevel@tonic-gate #include <stdlib.h> 437c478bd9Sstevel@tonic-gate #include <string.h> 447c478bd9Sstevel@tonic-gate #include <unistd.h> 457c478bd9Sstevel@tonic-gate #include <errno.h> 467c478bd9Sstevel@tonic-gate #include <fcntl.h> 477c478bd9Sstevel@tonic-gate #include <ctype.h> 487c478bd9Sstevel@tonic-gate #include <sys/stat.h> 497c478bd9Sstevel@tonic-gate #include <sys/types.h> 50342440ecSPrasad Singamsetty #include <limits.h> 517c478bd9Sstevel@tonic-gate #include <sys/param.h> 527c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 537c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h> 547c478bd9Sstevel@tonic-gate #include <sys/byteorder.h> 557c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 587c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 597c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #define CLR_SCR "[1;1H[0J" 627c478bd9Sstevel@tonic-gate #define CLR_LIN "[0K" 637c478bd9Sstevel@tonic-gate #define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \ 647c478bd9Sstevel@tonic-gate "[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H" 657c478bd9Sstevel@tonic-gate #define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K" 667c478bd9Sstevel@tonic-gate #define W_LINE "[12;1H[0K[11;1H[0K" 677c478bd9Sstevel@tonic-gate #define E_LINE "[24;1H[0K[23;1H[0K" 687c478bd9Sstevel@tonic-gate #define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \ 697c478bd9Sstevel@tonic-gate "[0K[18;1H[0K[19;1H[0K[13;1H" 707c478bd9Sstevel@tonic-gate #define T_LINE "[1;1H[0K" 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define DEFAULT_PATH "/dev/rdsk/" 737c478bd9Sstevel@tonic-gate 74f85c7842SSuhasini Peddada /* XXX - should be in fdisk.h, used by sd as well */ 75f85c7842SSuhasini Peddada 76f85c7842SSuhasini Peddada /* 77f85c7842SSuhasini Peddada * the MAX values are the maximum usable values for BIOS chs values 78f85c7842SSuhasini Peddada * The MAX_CYL value of 1022 is the maximum usable value 79f85c7842SSuhasini Peddada * the value of 1023 is a fence value, 80f85c7842SSuhasini Peddada * indicating no CHS geometry exists for the corresponding LBA value. 81f85c7842SSuhasini Peddada * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1) 82f85c7842SSuhasini Peddada * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT) 83f85c7842SSuhasini Peddada */ 84f85c7842SSuhasini Peddada #define MAX_SECT (63) 85f85c7842SSuhasini Peddada #define MAX_CYL (1022) 86f85c7842SSuhasini Peddada #define MAX_HEAD (254) 87f85c7842SSuhasini Peddada 88342440ecSPrasad Singamsetty #define DK_MAX_2TB UINT32_MAX /* Max # of sectors in 2TB */ 89342440ecSPrasad Singamsetty 907c478bd9Sstevel@tonic-gate /* for clear_vtoc() */ 917c478bd9Sstevel@tonic-gate #define OLD 0 927c478bd9Sstevel@tonic-gate #define NEW 1 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* readvtoc/writevtoc return codes */ 957c478bd9Sstevel@tonic-gate #define VTOC_OK 0 /* Good VTOC */ 967c478bd9Sstevel@tonic-gate #define VTOC_INVAL 1 /* invalid VTOC */ 977c478bd9Sstevel@tonic-gate #define VTOC_NOTSUP 2 /* operation not supported - EFI label */ 987c478bd9Sstevel@tonic-gate #define VTOC_RWERR 3 /* couldn't read or write VTOC */ 997c478bd9Sstevel@tonic-gate 100f85c7842SSuhasini Peddada /* 101f85c7842SSuhasini Peddada * Support for fdisk(1M) on the SPARC platform 102f85c7842SSuhasini Peddada * In order to convert little endian values to big endian for SPARC, 103f85c7842SSuhasini Peddada * byte/short and long values must be swapped. 104f85c7842SSuhasini Peddada * These swapping macros will be used to access information in the 105f85c7842SSuhasini Peddada * mboot and ipart structures. 106f85c7842SSuhasini Peddada */ 107f85c7842SSuhasini Peddada 108f85c7842SSuhasini Peddada #ifdef sparc 109f85c7842SSuhasini Peddada #define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF)) 110f85c7842SSuhasini Peddada #define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \ 111f85c7842SSuhasini Peddada (les((unsigned)((val)&0xffff0000)>>16))) 112f85c7842SSuhasini Peddada #else 113f85c7842SSuhasini Peddada #define les(val) (val) 114f85c7842SSuhasini Peddada #define lel(val) (val) 115f85c7842SSuhasini Peddada #endif 116f85c7842SSuhasini Peddada 1177c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1189d5d1945Sbharding #define VTOC_OFFSET 1 1197c478bd9Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8) 1207c478bd9Sstevel@tonic-gate #define VTOC_OFFSET 0 1217c478bd9Sstevel@tonic-gate #else 1227c478bd9Sstevel@tonic-gate #error No VTOC format defined. 1237c478bd9Sstevel@tonic-gate #endif 1247c478bd9Sstevel@tonic-gate 125bb16350dSlclee static char Usage[] = "Usage: fdisk\n" 1267c478bd9Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1277c478bd9Sstevel@tonic-gate "[ -b masterboot ]\n" 1287c478bd9Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1297c478bd9Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n" 1307c478bd9Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n" 1317c478bd9Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice"; 1327c478bd9Sstevel@tonic-gate 133bb16350dSlclee static char Usage1[] = " Partition options:\n" 1347c478bd9Sstevel@tonic-gate " -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1357c478bd9Sstevel@tonic-gate " Create a partition with specific attributes:\n" 1367c478bd9Sstevel@tonic-gate " id = system id number (fdisk.h) for the partition type\n" 1377c478bd9Sstevel@tonic-gate " act = active partition flag (0 is off and 128 is on)\n" 1387c478bd9Sstevel@tonic-gate " bhead = beginning head for start of partition\n" 1397c478bd9Sstevel@tonic-gate " bsect = beginning sector for start of partition\n" 1407c478bd9Sstevel@tonic-gate " bcyl = beginning cylinder for start of partition\n" 1417c478bd9Sstevel@tonic-gate " ehead = ending head for end of partition\n" 1427c478bd9Sstevel@tonic-gate " esect = ending sector for end of partition\n" 1437c478bd9Sstevel@tonic-gate " ecyl = ending cylinder for end of partition\n" 1447c478bd9Sstevel@tonic-gate " rsect = sector number from start of disk for\n" 1457c478bd9Sstevel@tonic-gate " start of partition\n" 1467c478bd9Sstevel@tonic-gate " numsect = partition size in sectors\n" 1477c478bd9Sstevel@tonic-gate " -b master_boot\n" 1487c478bd9Sstevel@tonic-gate " Use master_boot as the master boot file.\n" 1497c478bd9Sstevel@tonic-gate " -B Create one Solaris partition that uses the entire disk.\n" 1507c478bd9Sstevel@tonic-gate " -E Create one EFI partition that uses the entire disk.\n" 1517c478bd9Sstevel@tonic-gate " -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1527c478bd9Sstevel@tonic-gate " Delete a partition. See attribute definitions for -A.\n" 1537c478bd9Sstevel@tonic-gate " -F fdisk_file\n" 1547c478bd9Sstevel@tonic-gate " Use fdisk_file to initialize on-line fdisk table.\n" 1557c478bd9Sstevel@tonic-gate " -I Forego device checks. Generate a file image of what would go\n" 1567c478bd9Sstevel@tonic-gate " on a disk using the geometry specified with the -S option.\n" 1577c478bd9Sstevel@tonic-gate " -n Do not run in interactive mode.\n" 1587c478bd9Sstevel@tonic-gate " -R Open the disk device as read-only.\n" 1597c478bd9Sstevel@tonic-gate " -t Check and adjust VTOC to be consistent with fdisk table.\n" 1607c478bd9Sstevel@tonic-gate " VTOC slices exceeding the partition size will be truncated.\n" 1617c478bd9Sstevel@tonic-gate " -T Check and adjust VTOC to be consistent with fdisk table.\n" 1627c478bd9Sstevel@tonic-gate " VTOC slices exceeding the partition size will be removed.\n" 1637c478bd9Sstevel@tonic-gate " -W fdisk_file\n" 1647c478bd9Sstevel@tonic-gate " Write on-disk table to fdisk_file.\n" 1657c478bd9Sstevel@tonic-gate " -W - Write on-disk table to standard output.\n" 1667c478bd9Sstevel@tonic-gate " -v Display virtual geometry. Must be used with the -W option.\n" 1677c478bd9Sstevel@tonic-gate " Diagnostic options:\n" 1687c478bd9Sstevel@tonic-gate " -d Activate debug information about progress.\n" 1697c478bd9Sstevel@tonic-gate " -g Write label geometry to standard output:\n" 1707c478bd9Sstevel@tonic-gate " PCYL number of physical cylinders\n" 1717c478bd9Sstevel@tonic-gate " NCYL number of usable cylinders\n" 1727c478bd9Sstevel@tonic-gate " ACYL number of alternate cylinders\n" 1737c478bd9Sstevel@tonic-gate " BCYL cylinder offset\n" 1747c478bd9Sstevel@tonic-gate " NHEADS number of heads\n" 1757c478bd9Sstevel@tonic-gate " NSECTORS number of sectors per track\n" 1767c478bd9Sstevel@tonic-gate " SECTSIZ size of a sector in bytes\n" 1777c478bd9Sstevel@tonic-gate " -G Write physical geometry to standard output (see -g).\n" 1787c478bd9Sstevel@tonic-gate " -h Issue this verbose help message.\n" 1797c478bd9Sstevel@tonic-gate " -o offset\n" 1807c478bd9Sstevel@tonic-gate " Block offset from start of disk (default 0). Ignored if\n" 1817c478bd9Sstevel@tonic-gate " -P # specified.\n" 1827c478bd9Sstevel@tonic-gate " -P fill_patt\n" 1837c478bd9Sstevel@tonic-gate " Fill disk with pattern fill_patt. fill_patt can be decimal or\n" 1847c478bd9Sstevel@tonic-gate " hexadecimal and is used as number for constant long word\n" 1857c478bd9Sstevel@tonic-gate " pattern. If fill_patt is \"#\" then pattern of block #\n" 1867c478bd9Sstevel@tonic-gate " for each block. Pattern is put in each block as long words\n" 1877c478bd9Sstevel@tonic-gate " and fills each block (see -o and -s).\n" 1887c478bd9Sstevel@tonic-gate " -r Read from a disk to stdout (see -o and -s).\n" 1897c478bd9Sstevel@tonic-gate " -s size Number of blocks on which to perform operation (see -o).\n" 1907c478bd9Sstevel@tonic-gate " -S geom_file\n" 1917c478bd9Sstevel@tonic-gate " Use geom_file to set the label geometry (see -g).\n" 1927c478bd9Sstevel@tonic-gate " -w Write to a disk from stdin (see -o and -s)."; 1937c478bd9Sstevel@tonic-gate 194bb16350dSlclee static char Ostr[] = "Other OS"; 195bb16350dSlclee static char Dstr[] = "DOS12"; 196bb16350dSlclee static char D16str[] = "DOS16"; 197bb16350dSlclee static char DDstr[] = "DOS-DATA"; 198bb16350dSlclee static char EDstr[] = "EXT-DOS"; 199bb16350dSlclee static char DBstr[] = "DOS-BIG"; 200bb16350dSlclee static char PCstr[] = "PCIX"; 201bb16350dSlclee static char Ustr[] = "UNIX System"; 202bb16350dSlclee static char SUstr[] = "Solaris"; 203bb16350dSlclee static char SU2str[] = "Solaris2"; 204bb16350dSlclee static char X86str[] = "x86 Boot"; 205bb16350dSlclee static char DIAGstr[] = "Diagnostic"; 206bb16350dSlclee static char IFSstr[] = "IFS: NTFS"; 207bb16350dSlclee static char AIXstr[] = "AIX Boot"; 208bb16350dSlclee static char AIXDstr[] = "AIX Data"; 209bb16350dSlclee static char OS2str[] = "OS/2 Boot"; 210bb16350dSlclee static char WINstr[] = "Win95 FAT32"; 211bb16350dSlclee static char EWINstr[] = "Ext Win95"; 212bb16350dSlclee static char FAT95str[] = "FAT16 LBA"; 213bb16350dSlclee static char EXTLstr[] = "EXT LBA"; 214bb16350dSlclee static char LINUXstr[] = "Linux"; 215bb16350dSlclee static char CPMstr[] = "CP/M"; 216bb16350dSlclee static char NOVstr[] = "Netware 3.x+"; 217bb16350dSlclee static char QNXstr[] = "QNX 4.x"; 218bb16350dSlclee static char QNX2str[] = "QNX part 2"; 219bb16350dSlclee static char QNX3str[] = "QNX part 3"; 220bb16350dSlclee static char LINNATstr[] = "Linux native"; 221bb16350dSlclee static char NTFSVOL1str[] = "NT volset 1"; 222bb16350dSlclee static char NTFSVOL2str[] = "NT volset 2"; 223bb16350dSlclee static char BSDstr[] = "BSD OS"; 224bb16350dSlclee static char NEXTSTEPstr[] = "NeXTSTEP"; 225bb16350dSlclee static char BSDIFSstr[] = "BSDI FS"; 226bb16350dSlclee static char BSDISWAPstr[] = "BSDI swap"; 227bb16350dSlclee static char Actvstr[] = "Active"; 228bb16350dSlclee static char EFIstr[] = "EFI"; 229bb16350dSlclee static char NAstr[] = " "; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* All the user options and flags */ 232bb16350dSlclee static char *Dfltdev; /* name of fixed disk drive */ 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* Diagnostic options */ 235bb16350dSlclee static int io_wrt = 0; /* write stdin to disk (-w) */ 236bb16350dSlclee static int io_rd = 0; /* read disk and write stdout (-r) */ 237bb16350dSlclee static char *io_fatt; /* user supplied pattern (-P pattern) */ 238bb16350dSlclee static int io_patt = 0; /* write pattern to disk (-P pattern) */ 239bb16350dSlclee static int io_lgeom = 0; /* get label geometry (-g) */ 240bb16350dSlclee static int io_pgeom = 0; /* get drive physical geometry (-G) */ 241bb16350dSlclee static char *io_sgeom = 0; /* set label geometry (-S geom_file) */ 242bb16350dSlclee static int io_readonly = 0; /* do not write to disk (-R) */ 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */ 2457c478bd9Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */ 2469d5d1945Sbharding static off_t io_offset = 0; /* offset sector (-o offset) */ 2479d5d1945Sbharding static off_t io_size = 0; /* size in sectors (-s size) */ 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* Partition table flags */ 250bb16350dSlclee static int v_flag = 0; /* virtual geometry-HBA flag (-v) */ 251bb16350dSlclee static int stdo_flag = 0; /* stdout flag (-W -) */ 252bb16350dSlclee static int io_fdisk = 0; /* do fdisk operation */ 253bb16350dSlclee static int io_ifdisk = 0; /* interactive partition */ 254bb16350dSlclee static int io_nifdisk = 0; /* non-interactive partition (-n) */ 2557c478bd9Sstevel@tonic-gate 256bb16350dSlclee static int io_adjt = 0; /* check/adjust VTOC (truncate (-t)) */ 257bb16350dSlclee static int io_ADJT = 0; /* check/adjust VTOC (delete (-T)) */ 258bb16350dSlclee static char *io_ffdisk = 0; /* input fdisk file name (-F file) */ 259bb16350dSlclee static char *io_Wfdisk = 0; /* output fdisk file name (-W file) */ 260bb16350dSlclee static char *io_Afdisk = 0; /* add entry to partition table (-A) */ 261bb16350dSlclee static char *io_Dfdisk = 0; /* delete entry from part. table (-D) */ 2627c478bd9Sstevel@tonic-gate 263bb16350dSlclee static char *io_mboot = 0; /* master boot record (-b boot_file) */ 2647c478bd9Sstevel@tonic-gate 265bb16350dSlclee static struct mboot BootCod; /* buffer for master boot record */ 2667c478bd9Sstevel@tonic-gate 267bb16350dSlclee static int io_wholedisk = 0; /* use whole disk for Solaris (-B) */ 268bb16350dSlclee static int io_EFIdisk = 0; /* use whole disk for EFI (-E) */ 269bb16350dSlclee static int io_debug = 0; /* activate verbose mode (-d) */ 270bb16350dSlclee static int io_image = 0; /* create image using geometry (-I) */ 2717c478bd9Sstevel@tonic-gate 272bb16350dSlclee static struct mboot *Bootblk; /* pointer to cut/paste sector zero */ 273bb16350dSlclee static char *Bootsect; /* pointer to sector zero buffer */ 274bb16350dSlclee static char *Nullsect; 275342440ecSPrasad Singamsetty static struct extvtoc disk_vtoc; /* verify VTOC table */ 276bb16350dSlclee static int vt_inval = 0; 277bb16350dSlclee static int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */ 278bb16350dSlclee static int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */ 2797c478bd9Sstevel@tonic-gate 280bb16350dSlclee static struct ipart Table[FD_NUMPART]; 281bb16350dSlclee static struct ipart Old_Table[FD_NUMPART]; 2822f8ec719SBarry Harding static int skip_verify[FD_NUMPART]; /* special case skip sz chk */ 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* Disk geometry information */ 2851237e847Slclee static struct dk_minfo minfo; 286bb16350dSlclee static struct dk_geom disk_geom; 2877c478bd9Sstevel@tonic-gate 288bb16350dSlclee static int Dev; /* fd for open device */ 289342440ecSPrasad Singamsetty 290342440ecSPrasad Singamsetty static diskaddr_t dev_capacity; /* number of blocks on device */ 291342440ecSPrasad Singamsetty static diskaddr_t chs_capacity; /* Numcyl_usable * heads * sectors */ 292342440ecSPrasad Singamsetty 293342440ecSPrasad Singamsetty static int Numcyl_usable; /* Number of usable cylinders */ 294342440ecSPrasad Singamsetty /* used to limit fdisk to 2TB */ 295342440ecSPrasad Singamsetty 2967c478bd9Sstevel@tonic-gate /* Physical geometry for the drive */ 297bb16350dSlclee static int Numcyl; /* number of cylinders */ 298bb16350dSlclee static int heads; /* number of heads */ 299bb16350dSlclee static int sectors; /* number of sectors per track */ 300bb16350dSlclee static int acyl; /* number of alternate sectors */ 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */ 303bb16350dSlclee static int hba_Numcyl; /* number of cylinders */ 304bb16350dSlclee static int hba_heads; /* number of heads */ 305bb16350dSlclee static int hba_sectors; /* number of sectors per track */ 3067c478bd9Sstevel@tonic-gate 307bb16350dSlclee static int sectsiz; /* sector size */ 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /* Load functions for fdisk table modification */ 3107c478bd9Sstevel@tonic-gate #define LOADFILE 0 /* load fdisk from file */ 3117c478bd9Sstevel@tonic-gate #define LOADDEL 1 /* delete an fdisk entry */ 3127c478bd9Sstevel@tonic-gate #define LOADADD 2 /* add an fdisk entry */ 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate #define CBUFLEN 80 315bb16350dSlclee static char s[CBUFLEN]; 3167c478bd9Sstevel@tonic-gate 317bb16350dSlclee static void update_disk_and_exit(boolean_t table_changed); 318bb16350dSlclee int main(int argc, char *argv[]); 319bb16350dSlclee static int read_geom(char *sgeom); 320bb16350dSlclee static void dev_mboot_read(void); 3219d5d1945Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz); 322bb16350dSlclee static void mboot_read(void); 323bb16350dSlclee static void fill_patt(void); 324bb16350dSlclee static void abs_read(void); 325bb16350dSlclee static void abs_write(void); 326bb16350dSlclee static void load(int funct, char *file); 3277c478bd9Sstevel@tonic-gate static void Set_Table_CHS_Values(int ti); 328bb16350dSlclee static int insert_tbl(int id, int act, 329bb16350dSlclee int bhead, int bsect, int bcyl, 330bb16350dSlclee int ehead, int esect, int ecyl, 331342440ecSPrasad Singamsetty uint32_t rsect, uint32_t numsect); 3322f8ec719SBarry Harding static int entry_from_old_table(int id, int act, 3332f8ec719SBarry Harding int bhead, int bsect, int bcyl, 3342f8ec719SBarry Harding int ehead, int esect, int ecyl, 3352f8ec719SBarry Harding uint32_t rsect, uint32_t numsect); 336bb16350dSlclee static int verify_tbl(void); 337bb16350dSlclee static int pars_fdisk(char *line, 338bb16350dSlclee int *id, int *act, 339bb16350dSlclee int *bhead, int *bsect, int *bcyl, 340bb16350dSlclee int *ehead, int *esect, int *ecyl, 341342440ecSPrasad Singamsetty uint32_t *rsect, uint32_t *numsect); 342342440ecSPrasad Singamsetty static int validate_part(int id, uint32_t rsect, uint32_t numsect); 343bb16350dSlclee static void stage0(void); 344bb16350dSlclee static int pcreate(void); 345bb16350dSlclee static int specify(uchar_t tsystid); 346bb16350dSlclee static void dispmenu(void); 347bb16350dSlclee static int pchange(void); 348bb16350dSlclee static int ppartid(void); 349bb16350dSlclee static char pdelete(void); 350bb16350dSlclee static void rm_blanks(char *s); 351bb16350dSlclee static int getcyl(void); 352bb16350dSlclee static void disptbl(void); 353bb16350dSlclee static void print_Table(void); 354bb16350dSlclee static void copy_Table_to_Old_Table(void); 355bb16350dSlclee static void nulltbl(void); 356bb16350dSlclee static void copy_Bootblk_to_Table(void); 357bb16350dSlclee static void fill_ipart(char *bootptr, struct ipart *partp); 358bb16350dSlclee #ifdef sparc 359bb16350dSlclee uchar_t getbyte(char **bp); 360bb16350dSlclee uint32_t getlong(char **bp); 361bb16350dSlclee #endif 362bb16350dSlclee static void copy_Table_to_Bootblk(void); 363bb16350dSlclee static int TableChanged(void); 364bb16350dSlclee static void ffile_write(char *file); 365bb16350dSlclee static void fix_slice(void); 366bb16350dSlclee static int yesno(void); 367bb16350dSlclee static int readvtoc(void); 368bb16350dSlclee static int writevtoc(void); 369bb16350dSlclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 370bb16350dSlclee static int clear_efi(void); 371bb16350dSlclee static void clear_vtoc(int table, int part); 372bb16350dSlclee static int lecture_and_query(char *warning, char *devname); 373bb16350dSlclee static void sanity_check_provided_device(char *devname, int fd); 374bb16350dSlclee static char *get_node(char *devname); 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate static void 3777c478bd9Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed) 3787c478bd9Sstevel@tonic-gate { 3797c478bd9Sstevel@tonic-gate if (table_changed) { 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * Copy the new table back to the sector buffer 3827c478bd9Sstevel@tonic-gate * and write it to disk 3837c478bd9Sstevel@tonic-gate */ 3847c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 3857c478bd9Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* If the VTOC table is wrong fix it (truncation only) */ 3897c478bd9Sstevel@tonic-gate if (io_adjt) 3907c478bd9Sstevel@tonic-gate fix_slice(); 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate exit(0); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 395f85c7842SSuhasini Peddada 396f85c7842SSuhasini Peddada 3977c478bd9Sstevel@tonic-gate /* 3987c478bd9Sstevel@tonic-gate * main 3997c478bd9Sstevel@tonic-gate * Process command-line options. 4007c478bd9Sstevel@tonic-gate */ 401bb16350dSlclee int 4027c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 4037c478bd9Sstevel@tonic-gate { 404bb16350dSlclee int c, i; 4057c478bd9Sstevel@tonic-gate extern int optind; 4067c478bd9Sstevel@tonic-gate extern char *optarg; 4077c478bd9Sstevel@tonic-gate int errflg = 0; 4087c478bd9Sstevel@tonic-gate int diag_cnt = 0; 4097c478bd9Sstevel@tonic-gate int openmode; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate setbuf(stderr, 0); /* so all output gets out on exit */ 4127c478bd9Sstevel@tonic-gate setbuf(stdout, 0); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* Process the options. */ 4157c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE")) 4167c478bd9Sstevel@tonic-gate != EOF) { 4177c478bd9Sstevel@tonic-gate switch (c) { 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate case 'o': 4209d5d1945Sbharding io_offset = (off_t)strtoull(optarg, 0, 0); 4217c478bd9Sstevel@tonic-gate continue; 4227c478bd9Sstevel@tonic-gate case 's': 4239d5d1945Sbharding io_size = (off_t)strtoull(optarg, 0, 0); 4247c478bd9Sstevel@tonic-gate continue; 4257c478bd9Sstevel@tonic-gate case 'P': 4267c478bd9Sstevel@tonic-gate diag_cnt++; 4277c478bd9Sstevel@tonic-gate io_patt++; 4287c478bd9Sstevel@tonic-gate io_fatt = optarg; 4297c478bd9Sstevel@tonic-gate continue; 4307c478bd9Sstevel@tonic-gate case 'w': 4317c478bd9Sstevel@tonic-gate diag_cnt++; 4327c478bd9Sstevel@tonic-gate io_wrt++; 4337c478bd9Sstevel@tonic-gate continue; 4347c478bd9Sstevel@tonic-gate case 'r': 4357c478bd9Sstevel@tonic-gate diag_cnt++; 4367c478bd9Sstevel@tonic-gate io_rd++; 4377c478bd9Sstevel@tonic-gate continue; 4387c478bd9Sstevel@tonic-gate case 'd': 4397c478bd9Sstevel@tonic-gate io_debug++; 4407c478bd9Sstevel@tonic-gate continue; 4417c478bd9Sstevel@tonic-gate case 'I': 4427c478bd9Sstevel@tonic-gate io_image++; 4437c478bd9Sstevel@tonic-gate continue; 4447c478bd9Sstevel@tonic-gate case 'R': 4457c478bd9Sstevel@tonic-gate io_readonly++; 4467c478bd9Sstevel@tonic-gate continue; 4477c478bd9Sstevel@tonic-gate case 'S': 4487c478bd9Sstevel@tonic-gate diag_cnt++; 4497c478bd9Sstevel@tonic-gate io_sgeom = optarg; 4507c478bd9Sstevel@tonic-gate continue; 4517c478bd9Sstevel@tonic-gate case 'T': 4527c478bd9Sstevel@tonic-gate io_ADJT++; 453bb16350dSlclee /* FALLTHRU */ 4547c478bd9Sstevel@tonic-gate case 't': 4557c478bd9Sstevel@tonic-gate io_adjt++; 4567c478bd9Sstevel@tonic-gate continue; 4577c478bd9Sstevel@tonic-gate case 'B': 4587c478bd9Sstevel@tonic-gate io_wholedisk++; 4597c478bd9Sstevel@tonic-gate io_fdisk++; 4607c478bd9Sstevel@tonic-gate continue; 4617c478bd9Sstevel@tonic-gate case 'E': 4627c478bd9Sstevel@tonic-gate io_EFIdisk++; 4637c478bd9Sstevel@tonic-gate io_fdisk++; 4647c478bd9Sstevel@tonic-gate continue; 4657c478bd9Sstevel@tonic-gate case 'g': 4667c478bd9Sstevel@tonic-gate diag_cnt++; 4677c478bd9Sstevel@tonic-gate io_lgeom++; 4687c478bd9Sstevel@tonic-gate continue; 4697c478bd9Sstevel@tonic-gate case 'G': 4707c478bd9Sstevel@tonic-gate diag_cnt++; 4717c478bd9Sstevel@tonic-gate io_pgeom++; 4727c478bd9Sstevel@tonic-gate continue; 4737c478bd9Sstevel@tonic-gate case 'n': 4747c478bd9Sstevel@tonic-gate io_nifdisk++; 4757c478bd9Sstevel@tonic-gate io_fdisk++; 4767c478bd9Sstevel@tonic-gate continue; 4777c478bd9Sstevel@tonic-gate case 'F': 4787c478bd9Sstevel@tonic-gate io_fdisk++; 4797c478bd9Sstevel@tonic-gate io_ffdisk = optarg; 4807c478bd9Sstevel@tonic-gate continue; 4817c478bd9Sstevel@tonic-gate case 'b': 4827c478bd9Sstevel@tonic-gate io_mboot = optarg; 4837c478bd9Sstevel@tonic-gate continue; 4847c478bd9Sstevel@tonic-gate case 'W': 4857c478bd9Sstevel@tonic-gate /* 4867c478bd9Sstevel@tonic-gate * If '-' is the -W argument, then write 4877c478bd9Sstevel@tonic-gate * to standard output, otherwise write 4887c478bd9Sstevel@tonic-gate * to the specified file. 4897c478bd9Sstevel@tonic-gate */ 4907c478bd9Sstevel@tonic-gate if (strncmp(optarg, "-", 1) == 0) 4917c478bd9Sstevel@tonic-gate stdo_flag = 1; 4927c478bd9Sstevel@tonic-gate else 4937c478bd9Sstevel@tonic-gate io_Wfdisk = optarg; 4947c478bd9Sstevel@tonic-gate io_fdisk++; 4957c478bd9Sstevel@tonic-gate continue; 4967c478bd9Sstevel@tonic-gate case 'A': 4977c478bd9Sstevel@tonic-gate io_fdisk++; 4987c478bd9Sstevel@tonic-gate io_Afdisk = optarg; 4997c478bd9Sstevel@tonic-gate continue; 5007c478bd9Sstevel@tonic-gate case 'D': 5017c478bd9Sstevel@tonic-gate io_fdisk++; 5027c478bd9Sstevel@tonic-gate io_Dfdisk = optarg; 5037c478bd9Sstevel@tonic-gate continue; 5047c478bd9Sstevel@tonic-gate case 'h': 505bb16350dSlclee (void) fprintf(stderr, "%s\n", Usage); 506bb16350dSlclee (void) fprintf(stderr, "%s\n", Usage1); 5077c478bd9Sstevel@tonic-gate exit(0); 508bb16350dSlclee /* FALLTHRU */ 5097c478bd9Sstevel@tonic-gate case 'v': 5107c478bd9Sstevel@tonic-gate v_flag = 1; 5117c478bd9Sstevel@tonic-gate continue; 5127c478bd9Sstevel@tonic-gate case '?': 5137c478bd9Sstevel@tonic-gate errflg++; 5147c478bd9Sstevel@tonic-gate break; 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate break; 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate if (io_image && io_sgeom && diag_cnt == 1) { 5207c478bd9Sstevel@tonic-gate diag_cnt = 0; 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate /* User option checking */ 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate /* By default, run in interactive mode */ 5267c478bd9Sstevel@tonic-gate if (!io_fdisk && !diag_cnt && !io_nifdisk) { 5277c478bd9Sstevel@tonic-gate io_ifdisk++; 5287c478bd9Sstevel@tonic-gate io_fdisk++; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) { 5317c478bd9Sstevel@tonic-gate errflg++; 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate /* Was any error detected? */ 5357c478bd9Sstevel@tonic-gate if (errflg || argc == optind) { 536bb16350dSlclee (void) fprintf(stderr, "%s\n", Usage); 537bb16350dSlclee (void) fprintf(stderr, 5387c478bd9Sstevel@tonic-gate "\nDetailed help is available with the -h option.\n"); 5397c478bd9Sstevel@tonic-gate exit(2); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate /* Figure out the correct device node to open */ 5447c478bd9Sstevel@tonic-gate Dfltdev = get_node(argv[optind]); 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate if (io_readonly) 5477c478bd9Sstevel@tonic-gate openmode = O_RDONLY; 5487c478bd9Sstevel@tonic-gate else 5497c478bd9Sstevel@tonic-gate openmode = O_RDWR|O_CREAT; 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate if ((Dev = open(Dfltdev, openmode, 0666)) == -1) { 552bb16350dSlclee (void) fprintf(stderr, 553bb16350dSlclee "fdisk: Cannot open device %s.\n", 554bb16350dSlclee Dfltdev); 5557c478bd9Sstevel@tonic-gate exit(1); 5567c478bd9Sstevel@tonic-gate } 5571237e847Slclee /* 5581237e847Slclee * not all disk (or disklike) drivers support DKIOCGMEDIAINFO 5591237e847Slclee * in that case leave the minfo structure zeroed 5601237e847Slclee */ 5611237e847Slclee if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) { 562*65908c77Syu, larry liu - Sun Microsystems - Beijing China (void) memset(&minfo, 0, sizeof (minfo)); 5631237e847Slclee } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate /* Get the disk geometry */ 5667c478bd9Sstevel@tonic-gate if (!io_image) { 5677c478bd9Sstevel@tonic-gate /* Get disk's HBA (virtual) geometry */ 5687c478bd9Sstevel@tonic-gate errno = 0; 5697c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) { 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * If ioctl isn't implemented on this platform, then 5737c478bd9Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v), 5747c478bd9Sstevel@tonic-gate * otherwise use the virtual geometry. 5757c478bd9Sstevel@tonic-gate */ 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate if (errno == ENOTTY) { 5787c478bd9Sstevel@tonic-gate v_flag = 0; 5797c478bd9Sstevel@tonic-gate no_virtgeom_ioctl = 1; 5807c478bd9Sstevel@tonic-gate } else if (errno == EINVAL) { 5817c478bd9Sstevel@tonic-gate /* 5827c478bd9Sstevel@tonic-gate * This means that the ioctl exists, but 5837c478bd9Sstevel@tonic-gate * is invalid for this disk, meaning the 5847c478bd9Sstevel@tonic-gate * disk doesn't have an HBA geometry 5857c478bd9Sstevel@tonic-gate * (like, say, it's larger than 8GB). 5867c478bd9Sstevel@tonic-gate */ 5877c478bd9Sstevel@tonic-gate v_flag = 0; 5887c478bd9Sstevel@tonic-gate hba_Numcyl = hba_heads = hba_sectors = 0; 5897c478bd9Sstevel@tonic-gate } else { 5907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5917c478bd9Sstevel@tonic-gate "%s: Cannot get virtual disk geometry.\n", 5927c478bd9Sstevel@tonic-gate argv[optind]); 5937c478bd9Sstevel@tonic-gate exit(1); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate } else { 5967c478bd9Sstevel@tonic-gate /* save virtual geometry values obtained by ioctl */ 5977c478bd9Sstevel@tonic-gate hba_Numcyl = disk_geom.dkg_ncyl; 5987c478bd9Sstevel@tonic-gate hba_heads = disk_geom.dkg_nhead; 5997c478bd9Sstevel@tonic-gate hba_sectors = disk_geom.dkg_nsect; 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate errno = 0; 6037c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 6047c478bd9Sstevel@tonic-gate if (errno == ENOTTY) { 6057c478bd9Sstevel@tonic-gate no_physgeom_ioctl = 1; 6067c478bd9Sstevel@tonic-gate } else { 6077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6087c478bd9Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 6097c478bd9Sstevel@tonic-gate argv[optind]); 6107c478bd9Sstevel@tonic-gate exit(1); 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * Call DKIOCGGEOM if the ioctls for physical and virtual 6167c478bd9Sstevel@tonic-gate * geometry fail. Get both from this generic call. 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate if (no_virtgeom_ioctl && no_physgeom_ioctl) { 6197c478bd9Sstevel@tonic-gate errno = 0; 6207c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6227c478bd9Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6237c478bd9Sstevel@tonic-gate argv[optind]); 6247c478bd9Sstevel@tonic-gate exit(1); 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6297c478bd9Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6307c478bd9Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 631*65908c77Syu, larry liu - Sun Microsystems - Beijing China 632*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (minfo.dki_lbsize != 0) 633*65908c77Syu, larry liu - Sun Microsystems - Beijing China sectsiz = minfo.dki_lbsize; 634*65908c77Syu, larry liu - Sun Microsystems - Beijing China else 6357c478bd9Sstevel@tonic-gate sectsiz = 512; 636*65908c77Syu, larry liu - Sun Microsystems - Beijing China 6377c478bd9Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate /* 6407c478bd9Sstevel@tonic-gate * if hba geometry was not set by DKIOC_VIRTGEOM 6417c478bd9Sstevel@tonic-gate * or we got an invalid hba geometry 6427c478bd9Sstevel@tonic-gate * then set hba geometry based on max values 6437c478bd9Sstevel@tonic-gate */ 6447c478bd9Sstevel@tonic-gate if (no_virtgeom_ioctl || 645bb16350dSlclee disk_geom.dkg_ncyl == 0 || 646bb16350dSlclee disk_geom.dkg_nhead == 0 || 647bb16350dSlclee disk_geom.dkg_nsect == 0 || 6487c478bd9Sstevel@tonic-gate disk_geom.dkg_ncyl > MAX_CYL || 6497c478bd9Sstevel@tonic-gate disk_geom.dkg_nhead > MAX_HEAD || 6507c478bd9Sstevel@tonic-gate disk_geom.dkg_nsect > MAX_SECT) { 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate /* 6537c478bd9Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v) 6547c478bd9Sstevel@tonic-gate */ 6557c478bd9Sstevel@tonic-gate v_flag = 0; 6567c478bd9Sstevel@tonic-gate hba_sectors = MAX_SECT; 6577c478bd9Sstevel@tonic-gate hba_heads = MAX_HEAD + 1; 6587c478bd9Sstevel@tonic-gate hba_Numcyl = (Numcyl * heads * sectors) / 6597c478bd9Sstevel@tonic-gate (hba_sectors * hba_heads); 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate if (io_debug) { 663bb16350dSlclee (void) fprintf(stderr, "Physical Geometry:\n"); 664bb16350dSlclee (void) fprintf(stderr, 6657c478bd9Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6667c478bd9Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6677c478bd9Sstevel@tonic-gate Numcyl, 6687c478bd9Sstevel@tonic-gate heads, 6697c478bd9Sstevel@tonic-gate sectors, 6707c478bd9Sstevel@tonic-gate sectsiz, 6717c478bd9Sstevel@tonic-gate Numcyl * heads * sectors, 6727c478bd9Sstevel@tonic-gate (Numcyl * heads * sectors * sectsiz) / 1048576); 673bb16350dSlclee (void) fprintf(stderr, "Virtual (HBA) Geometry:\n"); 674bb16350dSlclee (void) fprintf(stderr, 6757c478bd9Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6767c478bd9Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6777c478bd9Sstevel@tonic-gate hba_Numcyl, 6787c478bd9Sstevel@tonic-gate hba_heads, 6797c478bd9Sstevel@tonic-gate hba_sectors, 6807c478bd9Sstevel@tonic-gate sectsiz, 6817c478bd9Sstevel@tonic-gate hba_Numcyl * hba_heads * hba_sectors, 682bb16350dSlclee (hba_Numcyl * hba_heads * hba_sectors * sectsiz) / 683bb16350dSlclee 1048576); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate /* If user has requested a geometry report just do it and exit */ 6887c478bd9Sstevel@tonic-gate if (io_lgeom) { 6897c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6917c478bd9Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6927c478bd9Sstevel@tonic-gate argv[optind]); 6937c478bd9Sstevel@tonic-gate exit(1); 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6967c478bd9Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6977c478bd9Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 698*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (minfo.dki_lbsize != 0) 699*65908c77Syu, larry liu - Sun Microsystems - Beijing China sectsiz = minfo.dki_lbsize; 700*65908c77Syu, larry liu - Sun Microsystems - Beijing China else 7017c478bd9Sstevel@tonic-gate sectsiz = 512; 702*65908c77Syu, larry liu - Sun Microsystems - Beijing China 7037c478bd9Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 704bb16350dSlclee (void) printf("* Label geometry for device %s\n", Dfltdev); 705bb16350dSlclee (void) printf( 706bb16350dSlclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 7077c478bd9Sstevel@tonic-gate " SECSIZ\n"); 708bb16350dSlclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 7097c478bd9Sstevel@tonic-gate Numcyl, 7107c478bd9Sstevel@tonic-gate disk_geom.dkg_ncyl, 7117c478bd9Sstevel@tonic-gate disk_geom.dkg_acyl, 7127c478bd9Sstevel@tonic-gate disk_geom.dkg_bcyl, 7137c478bd9Sstevel@tonic-gate heads, 7147c478bd9Sstevel@tonic-gate sectors, 7157c478bd9Sstevel@tonic-gate sectsiz); 7167c478bd9Sstevel@tonic-gate exit(0); 7177c478bd9Sstevel@tonic-gate } else if (io_pgeom) { 7187c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 7197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7207c478bd9Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 7217c478bd9Sstevel@tonic-gate argv[optind]); 7227c478bd9Sstevel@tonic-gate exit(1); 7237c478bd9Sstevel@tonic-gate } 724bb16350dSlclee (void) printf("* Physical geometry for device %s\n", Dfltdev); 725bb16350dSlclee (void) printf( 726bb16350dSlclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 7277c478bd9Sstevel@tonic-gate " SECSIZ\n"); 728bb16350dSlclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 7297c478bd9Sstevel@tonic-gate disk_geom.dkg_pcyl, 7307c478bd9Sstevel@tonic-gate disk_geom.dkg_ncyl, 7317c478bd9Sstevel@tonic-gate disk_geom.dkg_acyl, 7327c478bd9Sstevel@tonic-gate disk_geom.dkg_bcyl, 7337c478bd9Sstevel@tonic-gate disk_geom.dkg_nhead, 7347c478bd9Sstevel@tonic-gate disk_geom.dkg_nsect, 7357c478bd9Sstevel@tonic-gate sectsiz); 7367c478bd9Sstevel@tonic-gate exit(0); 7377c478bd9Sstevel@tonic-gate } else if (io_sgeom) { 7387c478bd9Sstevel@tonic-gate if (read_geom(io_sgeom)) { 7397c478bd9Sstevel@tonic-gate exit(1); 7407c478bd9Sstevel@tonic-gate } else if (!io_image) { 7417c478bd9Sstevel@tonic-gate exit(0); 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate 7451237e847Slclee /* 7461237e847Slclee * some drivers may not support DKIOCGMEDIAINFO 7471237e847Slclee * in that case use CHS 7481237e847Slclee */ 749342440ecSPrasad Singamsetty chs_capacity = (diskaddr_t)Numcyl * heads * sectors; 7501237e847Slclee dev_capacity = chs_capacity; 751342440ecSPrasad Singamsetty Numcyl_usable = Numcyl; 752342440ecSPrasad Singamsetty 753342440ecSPrasad Singamsetty if (chs_capacity > DK_MAX_2TB) { 754342440ecSPrasad Singamsetty /* limit to 2TB */ 755342440ecSPrasad Singamsetty Numcyl_usable = DK_MAX_2TB / (heads * sectors); 756342440ecSPrasad Singamsetty chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors; 757342440ecSPrasad Singamsetty } 758342440ecSPrasad Singamsetty 7591237e847Slclee if (minfo.dki_capacity > 0) 7601237e847Slclee dev_capacity = minfo.dki_capacity; 7611237e847Slclee 7627c478bd9Sstevel@tonic-gate /* Allocate memory to hold three complete sectors */ 763*65908c77Syu, larry liu - Sun Microsystems - Beijing China Bootsect = (char *)calloc(3 * sectsiz, 1); 7647c478bd9Sstevel@tonic-gate if (Bootsect == NULL) { 765bb16350dSlclee (void) fprintf(stderr, 7667c478bd9Sstevel@tonic-gate "fdisk: Unable to obtain enough buffer memory" 7677c478bd9Sstevel@tonic-gate " (%d bytes).\n", 7687c478bd9Sstevel@tonic-gate 3 * sectsiz); 7697c478bd9Sstevel@tonic-gate exit(1); 7707c478bd9Sstevel@tonic-gate } 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate Nullsect = Bootsect + sectsiz; 7737c478bd9Sstevel@tonic-gate /* Zero out the "NULL" sector */ 7747c478bd9Sstevel@tonic-gate for (i = 0; i < sectsiz; i++) { 7757c478bd9Sstevel@tonic-gate Nullsect[i] = 0; 7767c478bd9Sstevel@tonic-gate } 7777c478bd9Sstevel@tonic-gate 7787c478bd9Sstevel@tonic-gate /* Find out what the user wants done */ 7797c478bd9Sstevel@tonic-gate if (io_rd) { /* abs disk read */ 7807c478bd9Sstevel@tonic-gate abs_read(); /* will not return */ 7817c478bd9Sstevel@tonic-gate } else if (io_wrt && !io_readonly) { 7827c478bd9Sstevel@tonic-gate abs_write(); /* will not return */ 7837c478bd9Sstevel@tonic-gate } else if (io_patt && !io_readonly) { 7847c478bd9Sstevel@tonic-gate fill_patt(); /* will not return */ 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate /* This is the fdisk edit, the real reason for the program. */ 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate sanity_check_provided_device(Dfltdev, Dev); 7917c478bd9Sstevel@tonic-gate 7927c478bd9Sstevel@tonic-gate /* Get the new BOOT program in case we write a new fdisk table */ 7937c478bd9Sstevel@tonic-gate mboot_read(); 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate /* Read from disk master boot */ 7967c478bd9Sstevel@tonic-gate dev_mboot_read(); 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate /* 7997c478bd9Sstevel@tonic-gate * Verify and copy the device's fdisk table. This will be used 8007c478bd9Sstevel@tonic-gate * as the prototype mboot if the device's mboot looks invalid. 8017c478bd9Sstevel@tonic-gate */ 8027c478bd9Sstevel@tonic-gate Bootblk = (struct mboot *)Bootsect; 8037c478bd9Sstevel@tonic-gate copy_Bootblk_to_Table(); 8047c478bd9Sstevel@tonic-gate 8057c478bd9Sstevel@tonic-gate /* save away a copy of Table in Old_Table for sensing changes */ 8067c478bd9Sstevel@tonic-gate copy_Table_to_Old_Table(); 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate /* Load fdisk table from specified file (-F fdisk_file) */ 8097c478bd9Sstevel@tonic-gate if (io_ffdisk) { 8107c478bd9Sstevel@tonic-gate /* Load and verify user-specified table parameters */ 8117c478bd9Sstevel@tonic-gate load(LOADFILE, io_ffdisk); 8127c478bd9Sstevel@tonic-gate } 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate /* Does user want to delete or add an entry? */ 8157c478bd9Sstevel@tonic-gate if (io_Dfdisk) { 8167c478bd9Sstevel@tonic-gate load(LOADDEL, io_Dfdisk); 8177c478bd9Sstevel@tonic-gate } 8187c478bd9Sstevel@tonic-gate if (io_Afdisk) { 8197c478bd9Sstevel@tonic-gate load(LOADADD, io_Afdisk); 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) { 8237c478bd9Sstevel@tonic-gate /* Check if there is no fdisk table */ 8247c478bd9Sstevel@tonic-gate if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) { 8257c478bd9Sstevel@tonic-gate if (io_ifdisk && !io_wholedisk && !io_EFIdisk) { 826bb16350dSlclee (void) printf( 827bb16350dSlclee "No fdisk table exists. The default" 828bb16350dSlclee " partition for the disk is:\n\n" 829bb16350dSlclee " a 100%% \"SOLARIS System\" " 830bb16350dSlclee "partition\n\n" 831bb16350dSlclee "Type \"y\" to accept the default " 8327c478bd9Sstevel@tonic-gate "partition, otherwise type \"n\" to " 8337c478bd9Sstevel@tonic-gate "edit the\n partition table.\n"); 834342440ecSPrasad Singamsetty 835342440ecSPrasad Singamsetty if (Numcyl > Numcyl_usable) 836342440ecSPrasad Singamsetty (void) printf("WARNING: Disk is larger" 837342440ecSPrasad Singamsetty " than 2TB. Solaris partition will" 838342440ecSPrasad Singamsetty " be limited to 2 TB.\n"); 8397c478bd9Sstevel@tonic-gate } 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate /* Edit the partition table as directed */ 8427c478bd9Sstevel@tonic-gate if (io_wholedisk ||(io_ifdisk && yesno())) { 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate /* Default scenario */ 8457c478bd9Sstevel@tonic-gate nulltbl(); 8467c478bd9Sstevel@tonic-gate /* now set up UNIX System partition */ 8477c478bd9Sstevel@tonic-gate Table[0].bootid = ACTIVE; 848f85c7842SSuhasini Peddada Table[0].relsect = lel(heads * sectors); 849342440ecSPrasad Singamsetty 850342440ecSPrasad Singamsetty Table[0].numsect = 851f85c7842SSuhasini Peddada lel((ulong_t)((Numcyl_usable - 1) * 8527c478bd9Sstevel@tonic-gate heads * sectors)); 853342440ecSPrasad Singamsetty 8547c478bd9Sstevel@tonic-gate Table[0].systid = SUNIXOS2; /* Solaris */ 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* calculate CHS values for table entry 0 */ 8577c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(0); 8587c478bd9Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8597c478bd9Sstevel@tonic-gate } else if (io_EFIdisk) { 8607c478bd9Sstevel@tonic-gate /* create an EFI partition for the whole disk */ 8617c478bd9Sstevel@tonic-gate nulltbl(); 8627c478bd9Sstevel@tonic-gate i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1, 863342440ecSPrasad Singamsetty (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB : 864342440ecSPrasad Singamsetty (dev_capacity - 1)); 8657c478bd9Sstevel@tonic-gate if (i != 0) { 866bb16350dSlclee (void) fprintf(stderr, 867bb16350dSlclee "Error creating EFI partition\n"); 8687c478bd9Sstevel@tonic-gate exit(1); 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate } 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate /* Display complete fdisk table entries for debugging purposes */ 8767c478bd9Sstevel@tonic-gate if (io_debug) { 877bb16350dSlclee (void) fprintf(stderr, "Partition Table Entry Values:\n"); 8787c478bd9Sstevel@tonic-gate print_Table(); 8797c478bd9Sstevel@tonic-gate if (io_ifdisk) { 880bb16350dSlclee (void) fprintf(stderr, "\n"); 881bb16350dSlclee (void) fprintf(stderr, "Press Enter to continue.\n"); 882933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate 8867c478bd9Sstevel@tonic-gate /* Interactive fdisk mode */ 8877c478bd9Sstevel@tonic-gate if (io_ifdisk) { 888bb16350dSlclee (void) printf(CLR_SCR); 8897c478bd9Sstevel@tonic-gate disptbl(); 890bb16350dSlclee for (;;) { 891bb16350dSlclee stage0(); 8927c478bd9Sstevel@tonic-gate copy_Bootblk_to_Table(); 8937c478bd9Sstevel@tonic-gate disptbl(); 8947c478bd9Sstevel@tonic-gate } 8957c478bd9Sstevel@tonic-gate } 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate /* If user wants to write the table to a file, do it */ 8987c478bd9Sstevel@tonic-gate if (io_Wfdisk) 8997c478bd9Sstevel@tonic-gate ffile_write(io_Wfdisk); 9007c478bd9Sstevel@tonic-gate else if (stdo_flag) 9017c478bd9Sstevel@tonic-gate ffile_write((char *)stdout); 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate update_disk_and_exit(TableChanged() == 1); 904bb16350dSlclee return (0); 9057c478bd9Sstevel@tonic-gate } 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate /* 9087c478bd9Sstevel@tonic-gate * read_geom 9097c478bd9Sstevel@tonic-gate * Read geometry from specified file (-S). 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate 912bb16350dSlclee static int 913bb16350dSlclee read_geom(char *sgeom) 9147c478bd9Sstevel@tonic-gate { 9157c478bd9Sstevel@tonic-gate char line[256]; 9167c478bd9Sstevel@tonic-gate FILE *fp; 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate /* open the prototype file */ 9197c478bd9Sstevel@tonic-gate if ((fp = fopen(sgeom, "r")) == NULL) { 9207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot open file %s.\n", 9217c478bd9Sstevel@tonic-gate io_sgeom); 9227c478bd9Sstevel@tonic-gate return (1); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* Read a line from the file */ 9267c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 9277c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 9287c478bd9Sstevel@tonic-gate continue; 9297c478bd9Sstevel@tonic-gate else { 9307c478bd9Sstevel@tonic-gate line[strlen(line)] = '\0'; 931bb16350dSlclee if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d", 9327c478bd9Sstevel@tonic-gate &disk_geom.dkg_pcyl, 9337c478bd9Sstevel@tonic-gate &disk_geom.dkg_ncyl, 9347c478bd9Sstevel@tonic-gate &disk_geom.dkg_acyl, 9357c478bd9Sstevel@tonic-gate &disk_geom.dkg_bcyl, 9367c478bd9Sstevel@tonic-gate &disk_geom.dkg_nhead, 9377c478bd9Sstevel@tonic-gate &disk_geom.dkg_nsect, 9387c478bd9Sstevel@tonic-gate §siz) != 7) { 9397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9407c478bd9Sstevel@tonic-gate "Syntax error:\n \"%s\".\n", 9417c478bd9Sstevel@tonic-gate line); 9427c478bd9Sstevel@tonic-gate return (1); 9437c478bd9Sstevel@tonic-gate } 9447c478bd9Sstevel@tonic-gate break; 9457c478bd9Sstevel@tonic-gate } /* else */ 9467c478bd9Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate if (!io_image) { 9497c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) { 9507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9517c478bd9Sstevel@tonic-gate "fdisk: Cannot set label geometry.\n"); 9527c478bd9Sstevel@tonic-gate return (1); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate } else { 9557c478bd9Sstevel@tonic-gate Numcyl = hba_Numcyl = disk_geom.dkg_ncyl; 9567c478bd9Sstevel@tonic-gate heads = hba_heads = disk_geom.dkg_nhead; 9577c478bd9Sstevel@tonic-gate sectors = hba_sectors = disk_geom.dkg_nsect; 9587c478bd9Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 9597c478bd9Sstevel@tonic-gate } 9607c478bd9Sstevel@tonic-gate 961bb16350dSlclee (void) fclose(fp); 9627c478bd9Sstevel@tonic-gate return (0); 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate /* 9667c478bd9Sstevel@tonic-gate * dev_mboot_read 9677c478bd9Sstevel@tonic-gate * Read the master boot sector from the device. 9687c478bd9Sstevel@tonic-gate */ 969bb16350dSlclee static void 970bb16350dSlclee dev_mboot_read(void) 9717c478bd9Sstevel@tonic-gate { 9727c478bd9Sstevel@tonic-gate if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) { 9737c478bd9Sstevel@tonic-gate perror("Error in ioctl DKIOCGMBOOT"); 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate if (errno == 0) 9767c478bd9Sstevel@tonic-gate return; 9777c478bd9Sstevel@tonic-gate if (lseek(Dev, 0, SEEK_SET) == -1) { 978bb16350dSlclee (void) fprintf(stderr, 9797c478bd9Sstevel@tonic-gate "fdisk: Error seeking to partition table on %s.\n", 9807c478bd9Sstevel@tonic-gate Dfltdev); 9817c478bd9Sstevel@tonic-gate if (!io_image) 9827c478bd9Sstevel@tonic-gate exit(1); 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 985bb16350dSlclee (void) fprintf(stderr, 9867c478bd9Sstevel@tonic-gate "fdisk: Error reading partition table from %s.\n", 9877c478bd9Sstevel@tonic-gate Dfltdev); 9887c478bd9Sstevel@tonic-gate if (!io_image) 9897c478bd9Sstevel@tonic-gate exit(1); 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate /* 9947c478bd9Sstevel@tonic-gate * dev_mboot_write 9957c478bd9Sstevel@tonic-gate * Write the master boot sector to the device. 9967c478bd9Sstevel@tonic-gate */ 997bb16350dSlclee static void 9989d5d1945Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz) 9997c478bd9Sstevel@tonic-gate { 10007c478bd9Sstevel@tonic-gate int new_pt, old_pt, error; 1001a4a653edSbharding int clr_efi = -1; 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate if (io_readonly) 1004bb16350dSlclee return; 10057c478bd9Sstevel@tonic-gate 10067c478bd9Sstevel@tonic-gate if (io_debug) { 1007bb16350dSlclee (void) fprintf(stderr, "About to write fdisk table:\n"); 10087c478bd9Sstevel@tonic-gate print_Table(); 10097c478bd9Sstevel@tonic-gate if (io_ifdisk) { 1010bb16350dSlclee (void) fprintf(stderr, "Press Enter to continue.\n"); 1011933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 10127c478bd9Sstevel@tonic-gate } 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate 1015a4a653edSbharding /* 1016a4a653edSbharding * If the new table has any Solaris partitions and the old 1017a4a653edSbharding * table does not have an entry that describes it 1018a4a653edSbharding * exactly then clear the old vtoc (if any). 1019a4a653edSbharding */ 10207c478bd9Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 10217c478bd9Sstevel@tonic-gate 1022a4a653edSbharding /* We only care about potential Solaris parts. */ 10237c478bd9Sstevel@tonic-gate if (Table[new_pt].systid != SUNIXOS && 10247c478bd9Sstevel@tonic-gate Table[new_pt].systid != SUNIXOS2) 10257c478bd9Sstevel@tonic-gate continue; 10267c478bd9Sstevel@tonic-gate 1027a4a653edSbharding /* Does the old table have an exact entry for the new entry? */ 1028a4a653edSbharding for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 1029a4a653edSbharding 1030a4a653edSbharding /* We only care about old Solaris partitions. */ 1031a4a653edSbharding if ((Old_Table[old_pt].systid == SUNIXOS) || 1032a4a653edSbharding (Old_Table[old_pt].systid == SUNIXOS2)) { 1033a4a653edSbharding 1034a4a653edSbharding /* Is this old one the same as a new one? */ 1035a4a653edSbharding if ((Old_Table[old_pt].relsect == 1036a4a653edSbharding Table[new_pt].relsect) && 1037a4a653edSbharding (Old_Table[old_pt].numsect == 1038a4a653edSbharding Table[new_pt].numsect)) 1039a4a653edSbharding break; /* Yes */ 1040a4a653edSbharding } 1041a4a653edSbharding } 1042a4a653edSbharding 1043a4a653edSbharding /* Did a solaris partition change location or size? */ 1044a4a653edSbharding if (old_pt >= FD_NUMPART) { 1045a4a653edSbharding /* Yes clear old vtoc */ 1046a4a653edSbharding if (io_debug) { 1047a4a653edSbharding (void) fprintf(stderr, 1048a4a653edSbharding "Clearing VTOC labels from NEW" 1049a4a653edSbharding " table\n"); 1050a4a653edSbharding } 1051a4a653edSbharding clear_vtoc(NEW, new_pt); 1052a4a653edSbharding } 1053a4a653edSbharding } 1054a4a653edSbharding 1055a4a653edSbharding 1056a4a653edSbharding /* see if the old table had EFI */ 1057a4a653edSbharding for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 1058a4a653edSbharding if (Old_Table[old_pt].systid == EFI_PMBR) { 1059a4a653edSbharding clr_efi = old_pt; 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate } 10627c478bd9Sstevel@tonic-gate 10637c478bd9Sstevel@tonic-gate /* look to see if a EFI partition changed in relsect/numsect */ 10647c478bd9Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 10657c478bd9Sstevel@tonic-gate if (Table[new_pt].systid != EFI_PMBR) 10667c478bd9Sstevel@tonic-gate continue; 10677c478bd9Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 10681237e847Slclee if ((Old_Table[old_pt].systid == 10691237e847Slclee Table[new_pt].systid) && 10701237e847Slclee (Old_Table[old_pt].relsect == 10711237e847Slclee Table[new_pt].relsect) && 10721237e847Slclee (Old_Table[old_pt].numsect == 10731237e847Slclee Table[new_pt].numsect)) 10747c478bd9Sstevel@tonic-gate break; 10757c478bd9Sstevel@tonic-gate } 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate /* 10787c478bd9Sstevel@tonic-gate * if EFI partition changed, set the flag to clear 10797c478bd9Sstevel@tonic-gate * the EFI GPT 10807c478bd9Sstevel@tonic-gate */ 10817c478bd9Sstevel@tonic-gate if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) { 10827c478bd9Sstevel@tonic-gate clr_efi = 0; 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate break; 10857c478bd9Sstevel@tonic-gate } 10867c478bd9Sstevel@tonic-gate 10877c478bd9Sstevel@tonic-gate /* clear labels if necessary */ 10887c478bd9Sstevel@tonic-gate if (clr_efi >= 0) { 10897c478bd9Sstevel@tonic-gate if (io_debug) { 1090bb16350dSlclee (void) fprintf(stderr, "Clearing EFI labels\n"); 10917c478bd9Sstevel@tonic-gate } 10927c478bd9Sstevel@tonic-gate if ((error = clear_efi()) != 0) { 10937c478bd9Sstevel@tonic-gate if (io_debug) { 1094bb16350dSlclee (void) fprintf(stderr, 1095bb16350dSlclee "\tError %d clearing EFI labels" 10967c478bd9Sstevel@tonic-gate " (probably no EFI labels exist)\n", 10977c478bd9Sstevel@tonic-gate error); 10987c478bd9Sstevel@tonic-gate } 10997c478bd9Sstevel@tonic-gate } 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate 11027c478bd9Sstevel@tonic-gate if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) { 1103bb16350dSlclee (void) fprintf(stderr, 11047c478bd9Sstevel@tonic-gate "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n", 11057c478bd9Sstevel@tonic-gate Dfltdev); 11067c478bd9Sstevel@tonic-gate } 11077c478bd9Sstevel@tonic-gate if (errno == 0) 11087c478bd9Sstevel@tonic-gate return; 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate /* write to disk drive */ 11117c478bd9Sstevel@tonic-gate if (lseek(Dev, sect, SEEK_SET) == -1) { 1112bb16350dSlclee (void) fprintf(stderr, 11137c478bd9Sstevel@tonic-gate "fdisk: Error seeking to master boot record on %s.\n", 11147c478bd9Sstevel@tonic-gate Dfltdev); 11157c478bd9Sstevel@tonic-gate exit(1); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate if (write(Dev, buff, bootsiz) != bootsiz) { 1118bb16350dSlclee (void) fprintf(stderr, 11197c478bd9Sstevel@tonic-gate "fdisk: Error writing master boot record to %s.\n", 11207c478bd9Sstevel@tonic-gate Dfltdev); 11217c478bd9Sstevel@tonic-gate exit(1); 11227c478bd9Sstevel@tonic-gate } 11237c478bd9Sstevel@tonic-gate } 11247c478bd9Sstevel@tonic-gate 11257c478bd9Sstevel@tonic-gate /* 11267c478bd9Sstevel@tonic-gate * mboot_read 11277c478bd9Sstevel@tonic-gate * Read the prototype boot records from the files. 11287c478bd9Sstevel@tonic-gate */ 1129bb16350dSlclee static void 1130bb16350dSlclee mboot_read(void) 11317c478bd9Sstevel@tonic-gate { 11327c478bd9Sstevel@tonic-gate int mDev, i; 11337c478bd9Sstevel@tonic-gate struct ipart *part; 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate #if defined(i386) || defined(sparc) 11367c478bd9Sstevel@tonic-gate /* 11377c478bd9Sstevel@tonic-gate * If the master boot file hasn't been specified, use the 11387c478bd9Sstevel@tonic-gate * implementation architecture name to generate the default one. 11397c478bd9Sstevel@tonic-gate */ 11407c478bd9Sstevel@tonic-gate if (io_mboot == (char *)0) { 11417c478bd9Sstevel@tonic-gate /* 11427c478bd9Sstevel@tonic-gate * Bug ID 1249035: 11437c478bd9Sstevel@tonic-gate * The mboot file must be delivered on all platforms 11447c478bd9Sstevel@tonic-gate * and installed in a non-platform-dependent 11457c478bd9Sstevel@tonic-gate * directory; i.e., /usr/lib/fs/ufs. 11467c478bd9Sstevel@tonic-gate */ 11477c478bd9Sstevel@tonic-gate io_mboot = "/usr/lib/fs/ufs/mboot"; 11487c478bd9Sstevel@tonic-gate } 11497c478bd9Sstevel@tonic-gate 11507c478bd9Sstevel@tonic-gate /* First read in the master boot record */ 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate /* Open the master boot proto file */ 11537c478bd9Sstevel@tonic-gate if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) { 1154bb16350dSlclee (void) fprintf(stderr, 11557c478bd9Sstevel@tonic-gate "fdisk: Cannot open master boot file %s.\n", 11567c478bd9Sstevel@tonic-gate io_mboot); 11577c478bd9Sstevel@tonic-gate exit(1); 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate /* Read the master boot program */ 11617c478bd9Sstevel@tonic-gate if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof 11627c478bd9Sstevel@tonic-gate (struct mboot)) { 1163bb16350dSlclee (void) fprintf(stderr, 11647c478bd9Sstevel@tonic-gate "fdisk: Cannot read master boot file %s.\n", 11657c478bd9Sstevel@tonic-gate io_mboot); 11667c478bd9Sstevel@tonic-gate exit(1); 11677c478bd9Sstevel@tonic-gate } 11687c478bd9Sstevel@tonic-gate 11697c478bd9Sstevel@tonic-gate /* Is this really a master boot record? */ 1170f85c7842SSuhasini Peddada if (les(BootCod.signature) != MBB_MAGIC) { 1171bb16350dSlclee (void) fprintf(stderr, 11727c478bd9Sstevel@tonic-gate "fdisk: Invalid master boot file %s.\n", io_mboot); 1173bb16350dSlclee (void) fprintf(stderr, 1174bb16350dSlclee "Bad magic number: is %x, but should be %x.\n", 1175f85c7842SSuhasini Peddada les(BootCod.signature), MBB_MAGIC); 11767c478bd9Sstevel@tonic-gate exit(1); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 1179bb16350dSlclee (void) close(mDev); 11807c478bd9Sstevel@tonic-gate #else 11817c478bd9Sstevel@tonic-gate #error fdisk needs to be ported to new architecture 11827c478bd9Sstevel@tonic-gate #endif 11837c478bd9Sstevel@tonic-gate 11847c478bd9Sstevel@tonic-gate /* Zero out the partitions part of this record */ 11857c478bd9Sstevel@tonic-gate part = (struct ipart *)BootCod.parts; 11867c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++, part++) { 1187bb16350dSlclee (void) memset(part, 0, sizeof (struct ipart)); 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate 11927c478bd9Sstevel@tonic-gate /* 11937c478bd9Sstevel@tonic-gate * fill_patt 11947c478bd9Sstevel@tonic-gate * Fill the disk with user/sector number pattern. 11957c478bd9Sstevel@tonic-gate */ 1196bb16350dSlclee static void 1197bb16350dSlclee fill_patt(void) 11987c478bd9Sstevel@tonic-gate { 1199bb16350dSlclee int *buff_ptr, i; 12009d5d1945Sbharding off_t *off_ptr; 12017c478bd9Sstevel@tonic-gate int io_fpatt = 0; 12027c478bd9Sstevel@tonic-gate int io_ipatt = 0; 12037c478bd9Sstevel@tonic-gate 12047c478bd9Sstevel@tonic-gate if (strncmp(io_fatt, "#", 1) != 0) { 12057c478bd9Sstevel@tonic-gate io_fpatt++; 12067c478bd9Sstevel@tonic-gate io_ipatt = strtoul(io_fatt, 0, 0); 12077c478bd9Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 12087c478bd9Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 12097c478bd9Sstevel@tonic-gate *buff_ptr = io_ipatt; 12107c478bd9Sstevel@tonic-gate } 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate /* 12137c478bd9Sstevel@tonic-gate * Fill disk with pattern based on block number. 12147c478bd9Sstevel@tonic-gate * Write to the disk at absolute relative block io_offset 12157c478bd9Sstevel@tonic-gate * for io_size blocks. 12167c478bd9Sstevel@tonic-gate */ 12177c478bd9Sstevel@tonic-gate while (io_size--) { 12189d5d1945Sbharding off_ptr = (off_t *)Bootsect; 12197c478bd9Sstevel@tonic-gate if (!io_fpatt) { 12209d5d1945Sbharding for (i = 0; i < sectsiz; 12219d5d1945Sbharding i += sizeof (off_t), off_ptr++) 12229d5d1945Sbharding *off_ptr = io_offset; 12237c478bd9Sstevel@tonic-gate } 12247c478bd9Sstevel@tonic-gate /* Write the data to disk */ 12259d5d1945Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 12269d5d1945Sbharding SEEK_SET) == -1) { 1227bb16350dSlclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12287c478bd9Sstevel@tonic-gate Dfltdev); 12297c478bd9Sstevel@tonic-gate exit(1); 12307c478bd9Sstevel@tonic-gate } 12317c478bd9Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1232bb16350dSlclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 12337c478bd9Sstevel@tonic-gate Dfltdev); 12347c478bd9Sstevel@tonic-gate exit(1); 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate } /* while (--io_size); */ 12377c478bd9Sstevel@tonic-gate } 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate /* 12407c478bd9Sstevel@tonic-gate * abs_read 12417c478bd9Sstevel@tonic-gate * Read from the disk at absolute relative block io_offset for 12427c478bd9Sstevel@tonic-gate * io_size blocks. Write the data to standard ouput (-r). 12437c478bd9Sstevel@tonic-gate */ 1244bb16350dSlclee static void 1245bb16350dSlclee abs_read(void) 1246bb16350dSlclee { 12477c478bd9Sstevel@tonic-gate int c; 12487c478bd9Sstevel@tonic-gate 12497c478bd9Sstevel@tonic-gate while (io_size--) { 12509d5d1945Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 12519d5d1945Sbharding SEEK_SET) == -1) { 1252bb16350dSlclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12537c478bd9Sstevel@tonic-gate Dfltdev); 12547c478bd9Sstevel@tonic-gate exit(1); 12557c478bd9Sstevel@tonic-gate } 12567c478bd9Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 1257bb16350dSlclee (void) fprintf(stderr, "fdisk: Error reading %s.\n", 12587c478bd9Sstevel@tonic-gate Dfltdev); 12597c478bd9Sstevel@tonic-gate exit(1); 12607c478bd9Sstevel@tonic-gate } 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate /* Write to standard ouptut */ 1263bb16350dSlclee if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12647c478bd9Sstevel@tonic-gate if (c >= 0) { 12657c478bd9Sstevel@tonic-gate if (io_debug) 1266bb16350dSlclee (void) fprintf(stderr, 12677c478bd9Sstevel@tonic-gate "fdisk: Output warning: %d of %d" 12687c478bd9Sstevel@tonic-gate " characters written.\n", 12697c478bd9Sstevel@tonic-gate c, sectsiz); 12707c478bd9Sstevel@tonic-gate exit(2); 12717c478bd9Sstevel@tonic-gate } else { 12727c478bd9Sstevel@tonic-gate perror("write error on output file."); 12737c478bd9Sstevel@tonic-gate exit(2); 12747c478bd9Sstevel@tonic-gate } 12757c478bd9Sstevel@tonic-gate } /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */ 12767c478bd9Sstevel@tonic-gate /* != sectsiz) */ 12777c478bd9Sstevel@tonic-gate } /* while (--io_size); */ 12787c478bd9Sstevel@tonic-gate exit(0); 12797c478bd9Sstevel@tonic-gate } 12807c478bd9Sstevel@tonic-gate 12817c478bd9Sstevel@tonic-gate /* 12827c478bd9Sstevel@tonic-gate * abs_write 12837c478bd9Sstevel@tonic-gate * Read the data from standard input. Write to the disk at 12847c478bd9Sstevel@tonic-gate * absolute relative block io_offset for io_size blocks (-w). 12857c478bd9Sstevel@tonic-gate */ 1286bb16350dSlclee static void 1287bb16350dSlclee abs_write(void) 12887c478bd9Sstevel@tonic-gate { 12897c478bd9Sstevel@tonic-gate int c, i; 12907c478bd9Sstevel@tonic-gate 12917c478bd9Sstevel@tonic-gate while (io_size--) { 12927c478bd9Sstevel@tonic-gate int part_exit = 0; 12937c478bd9Sstevel@tonic-gate /* Read from standard input */ 12947c478bd9Sstevel@tonic-gate if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12957c478bd9Sstevel@tonic-gate if (c >= 0) { 12967c478bd9Sstevel@tonic-gate if (io_debug) 1297bb16350dSlclee (void) fprintf(stderr, 12987c478bd9Sstevel@tonic-gate "fdisk: WARNING: Incomplete read (%d of" 12997c478bd9Sstevel@tonic-gate " %d characters read) on input file.\n", 13007c478bd9Sstevel@tonic-gate c, sectsiz); 13017c478bd9Sstevel@tonic-gate /* Fill pattern to mark partial sector in buf */ 13027c478bd9Sstevel@tonic-gate for (i = c; i < sectsiz; ) { 13037c478bd9Sstevel@tonic-gate Bootsect[i++] = 0x41; 13047c478bd9Sstevel@tonic-gate Bootsect[i++] = 0x62; 13057c478bd9Sstevel@tonic-gate Bootsect[i++] = 0x65; 13067c478bd9Sstevel@tonic-gate Bootsect[i++] = 0; 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate part_exit++; 13097c478bd9Sstevel@tonic-gate } else { 13107c478bd9Sstevel@tonic-gate perror("read error on input file."); 13117c478bd9Sstevel@tonic-gate exit(2); 13127c478bd9Sstevel@tonic-gate } 13137c478bd9Sstevel@tonic-gate 13147c478bd9Sstevel@tonic-gate } 13157c478bd9Sstevel@tonic-gate /* Write to disk drive */ 13169d5d1945Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 13179d5d1945Sbharding SEEK_SET) == -1) { 1318bb16350dSlclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 13197c478bd9Sstevel@tonic-gate Dfltdev); 13207c478bd9Sstevel@tonic-gate exit(1); 13217c478bd9Sstevel@tonic-gate } 13227c478bd9Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1323bb16350dSlclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 13247c478bd9Sstevel@tonic-gate Dfltdev); 13257c478bd9Sstevel@tonic-gate exit(1); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate if (part_exit) 13287c478bd9Sstevel@tonic-gate exit(0); 13297c478bd9Sstevel@tonic-gate } /* while (--io_size); */ 13307c478bd9Sstevel@tonic-gate exit(1); 13317c478bd9Sstevel@tonic-gate } 13327c478bd9Sstevel@tonic-gate 1333bb16350dSlclee 13347c478bd9Sstevel@tonic-gate /* 13357c478bd9Sstevel@tonic-gate * load 13367c478bd9Sstevel@tonic-gate * Load will either read the fdisk table from a file or add or 13377c478bd9Sstevel@tonic-gate * delete an entry (-A, -D, -F). 13387c478bd9Sstevel@tonic-gate */ 13397c478bd9Sstevel@tonic-gate 1340bb16350dSlclee static void 1341bb16350dSlclee load(int funct, char *file) 13427c478bd9Sstevel@tonic-gate { 13437c478bd9Sstevel@tonic-gate int id; 13447c478bd9Sstevel@tonic-gate int act; 13457c478bd9Sstevel@tonic-gate int bhead; 13467c478bd9Sstevel@tonic-gate int bsect; 13477c478bd9Sstevel@tonic-gate int bcyl; 13487c478bd9Sstevel@tonic-gate int ehead; 13497c478bd9Sstevel@tonic-gate int esect; 13507c478bd9Sstevel@tonic-gate int ecyl; 1351342440ecSPrasad Singamsetty uint32_t rsect; 1352342440ecSPrasad Singamsetty uint32_t numsect; 13537c478bd9Sstevel@tonic-gate char line[256]; 13547c478bd9Sstevel@tonic-gate int i = 0; 13557c478bd9Sstevel@tonic-gate int j; 13567c478bd9Sstevel@tonic-gate FILE *fp; 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate switch (funct) { 13597c478bd9Sstevel@tonic-gate 13607c478bd9Sstevel@tonic-gate case LOADFILE: 13617c478bd9Sstevel@tonic-gate 13627c478bd9Sstevel@tonic-gate /* 13637c478bd9Sstevel@tonic-gate * Zero out the table before loading it, which will 13647c478bd9Sstevel@tonic-gate * force it to be updated on disk later (-F 13657c478bd9Sstevel@tonic-gate * fdisk_file). 13667c478bd9Sstevel@tonic-gate */ 13677c478bd9Sstevel@tonic-gate nulltbl(); 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate /* Open the prototype file */ 13707c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) { 13717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13727c478bd9Sstevel@tonic-gate "fdisk: Cannot open prototype partition file %s.\n", 13737c478bd9Sstevel@tonic-gate file); 13747c478bd9Sstevel@tonic-gate exit(1); 13757c478bd9Sstevel@tonic-gate } 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate /* Read a line from the file */ 13787c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 13797c478bd9Sstevel@tonic-gate if (pars_fdisk(line, &id, &act, &bhead, &bsect, 13807c478bd9Sstevel@tonic-gate &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) { 13817c478bd9Sstevel@tonic-gate continue; 13827c478bd9Sstevel@tonic-gate } 13837c478bd9Sstevel@tonic-gate 13847c478bd9Sstevel@tonic-gate /* 13857c478bd9Sstevel@tonic-gate * Validate the partition. It cannot start at sector 13867c478bd9Sstevel@tonic-gate * 0 unless it is UNUSED or already exists 13877c478bd9Sstevel@tonic-gate */ 13887c478bd9Sstevel@tonic-gate if (validate_part(id, rsect, numsect) < 0) { 13897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13907c478bd9Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13917c478bd9Sstevel@tonic-gate line); 13927c478bd9Sstevel@tonic-gate exit(1); 13937c478bd9Sstevel@tonic-gate } 13942f8ec719SBarry Harding 13952f8ec719SBarry Harding if (entry_from_old_table(id, act, bhead, bsect, 13962f8ec719SBarry Harding bcyl, ehead, esect, ecyl, rsect, numsect)) { 13972f8ec719SBarry Harding /* 13982f8ec719SBarry Harding * If we got here it means we copied an 13992f8ec719SBarry Harding * unmodified entry. So there is no need 14002f8ec719SBarry Harding * to insert it in the table or do any 14012f8ec719SBarry Harding * checks against disk size. 14022f8ec719SBarry Harding * 14032f8ec719SBarry Harding * This is a work around on the following 14042f8ec719SBarry Harding * situation (for IDE disks, at least): 14052f8ec719SBarry Harding * Different operation systems calculate 14062f8ec719SBarry Harding * disk size different ways, of which there 14072f8ec719SBarry Harding * are two main ways. 14082f8ec719SBarry Harding * 14092f8ec719SBarry Harding * The first, rounds the disk size to modulo 14102f8ec719SBarry Harding * cylinder size (virtual made-up cylinder 14112f8ec719SBarry Harding * usually based on maximum number of heads 14122f8ec719SBarry Harding * and sectors in partition table fields). 14132f8ec719SBarry Harding * Our OS's (for IDE) and most other "Unix" 14142f8ec719SBarry Harding * type OS's do this. 14152f8ec719SBarry Harding * 14162f8ec719SBarry Harding * The second, uses every single block 14172f8ec719SBarry Harding * on the disk (to maximize available space). 14182f8ec719SBarry Harding * Since disk manufactures do not know about 14192f8ec719SBarry Harding * "virtual cylinders", there are some number 14202f8ec719SBarry Harding * of blocks that make up a partial cylinder 14212f8ec719SBarry Harding * at the end of the disk. 14222f8ec719SBarry Harding * 14232f8ec719SBarry Harding * The difference between these two methods 14242f8ec719SBarry Harding * is where the problem is. When one 14252f8ec719SBarry Harding * tries to install Solaris/OpenSolaris on 14262f8ec719SBarry Harding * a disk that has another OS using that 14272f8ec719SBarry Harding * "partial cylinder", install fails. It fails 14282f8ec719SBarry Harding * since fdisk thinks its asked to create a 14292f8ec719SBarry Harding * partition with the -F option that contains 14302f8ec719SBarry Harding * a partition that runs off the end of the 14312f8ec719SBarry Harding * disk. 14322f8ec719SBarry Harding */ 14332f8ec719SBarry Harding continue; 14342f8ec719SBarry Harding } 14352f8ec719SBarry Harding 14367c478bd9Sstevel@tonic-gate /* 14377c478bd9Sstevel@tonic-gate * Find an unused entry to use and put the entry 14387c478bd9Sstevel@tonic-gate * in table 14397c478bd9Sstevel@tonic-gate */ 14407c478bd9Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, 14417c478bd9Sstevel@tonic-gate esect, ecyl, rsect, numsect) < 0) { 14427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14437c478bd9Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 14447c478bd9Sstevel@tonic-gate line); 14457c478bd9Sstevel@tonic-gate exit(1); 14467c478bd9Sstevel@tonic-gate } 14477c478bd9Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 14487c478bd9Sstevel@tonic-gate 14497c478bd9Sstevel@tonic-gate if (verify_tbl() < 0) { 1450bb16350dSlclee (void) fprintf(stderr, 14517c478bd9Sstevel@tonic-gate "fdisk: Cannot create partition table\n"); 14527c478bd9Sstevel@tonic-gate exit(1); 14537c478bd9Sstevel@tonic-gate } 14547c478bd9Sstevel@tonic-gate 1455bb16350dSlclee (void) fclose(fp); 14567c478bd9Sstevel@tonic-gate return; 14577c478bd9Sstevel@tonic-gate 14587c478bd9Sstevel@tonic-gate case LOADDEL: 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate /* Parse the user-supplied deletion line (-D) */ 1461bb16350dSlclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, 1462bb16350dSlclee &ehead, &esect, &ecyl, &rsect, &numsect)) { 1463bb16350dSlclee (void) fprintf(stderr, 1464bb16350dSlclee "fdisk: Syntax error \"%s\"\n", file); 1465bb16350dSlclee exit(1); 1466bb16350dSlclee } 14677c478bd9Sstevel@tonic-gate 14687c478bd9Sstevel@tonic-gate /* Find the exact entry in the table */ 14697c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 14707c478bd9Sstevel@tonic-gate if (Table[i].systid == id && 14717c478bd9Sstevel@tonic-gate Table[i].bootid == act && 14727c478bd9Sstevel@tonic-gate Table[i].beghead == bhead && 14737c478bd9Sstevel@tonic-gate Table[i].begsect == ((bsect & 0x3f) | 1474bb16350dSlclee (uchar_t)((bcyl>>2) & 0xc0)) && 1475bb16350dSlclee Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 14767c478bd9Sstevel@tonic-gate Table[i].endhead == ehead && 14777c478bd9Sstevel@tonic-gate Table[i].endsect == ((esect & 0x3f) | 1478bb16350dSlclee (uchar_t)((ecyl>>2) & 0xc0)) && 1479bb16350dSlclee Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 1480f85c7842SSuhasini Peddada Table[i].relsect == lel(rsect) && 1481f85c7842SSuhasini Peddada Table[i].numsect == lel(numsect)) { 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate /* 14847c478bd9Sstevel@tonic-gate * Found the entry. Now move rest of 14857c478bd9Sstevel@tonic-gate * entries up toward the top of the 14867c478bd9Sstevel@tonic-gate * table, leaving available entries at 14877c478bd9Sstevel@tonic-gate * the end of the fdisk table. 14887c478bd9Sstevel@tonic-gate */ 14897c478bd9Sstevel@tonic-gate for (j = i; j < FD_NUMPART - 1; j++) { 14907c478bd9Sstevel@tonic-gate Table[j].systid = Table[j + 1].systid; 14917c478bd9Sstevel@tonic-gate Table[j].bootid = Table[j + 1].bootid; 14927c478bd9Sstevel@tonic-gate Table[j].beghead = Table[j + 1].beghead; 14937c478bd9Sstevel@tonic-gate Table[j].begsect = Table[j + 1].begsect; 14947c478bd9Sstevel@tonic-gate Table[j].begcyl = Table[j + 1].begcyl; 14957c478bd9Sstevel@tonic-gate Table[j].endhead = Table[j + 1].endhead; 14967c478bd9Sstevel@tonic-gate Table[j].endsect = Table[j + 1].endsect; 14977c478bd9Sstevel@tonic-gate Table[j].endcyl = Table[j + 1].endcyl; 14987c478bd9Sstevel@tonic-gate Table[j].relsect = Table[j + 1].relsect; 14997c478bd9Sstevel@tonic-gate Table[j].numsect = Table[j + 1].numsect; 15007c478bd9Sstevel@tonic-gate } 15017c478bd9Sstevel@tonic-gate 15027c478bd9Sstevel@tonic-gate /* 15037c478bd9Sstevel@tonic-gate * Mark the last entry as unused in case 15047c478bd9Sstevel@tonic-gate * all table entries were in use prior 15057c478bd9Sstevel@tonic-gate * to the deletion. 15067c478bd9Sstevel@tonic-gate */ 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate Table[FD_NUMPART - 1].systid = UNUSED; 15097c478bd9Sstevel@tonic-gate Table[FD_NUMPART - 1].bootid = 0; 15107c478bd9Sstevel@tonic-gate return; 15117c478bd9Sstevel@tonic-gate } 15127c478bd9Sstevel@tonic-gate } 1513bb16350dSlclee (void) fprintf(stderr, 15147c478bd9Sstevel@tonic-gate "fdisk: Entry does not match any existing partition:\n" 15157c478bd9Sstevel@tonic-gate " \"%s\"\n", 15167c478bd9Sstevel@tonic-gate file); 15177c478bd9Sstevel@tonic-gate exit(1); 1518f85c7842SSuhasini Peddada /* FALLTHRU */ 15197c478bd9Sstevel@tonic-gate 15207c478bd9Sstevel@tonic-gate case LOADADD: 15217c478bd9Sstevel@tonic-gate 15227c478bd9Sstevel@tonic-gate /* Parse the user-supplied addition line (-A) */ 1523bb16350dSlclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead, 1524bb16350dSlclee &esect, &ecyl, &rsect, &numsect)) { 1525bb16350dSlclee (void) fprintf(stderr, 1526bb16350dSlclee "fdisk: Syntax error \"%s\"\n", file); 1527bb16350dSlclee exit(1); 1528bb16350dSlclee } 15297c478bd9Sstevel@tonic-gate 15307c478bd9Sstevel@tonic-gate /* Validate the partition. It cannot start at sector 0 */ 15317c478bd9Sstevel@tonic-gate if (rsect == 0) { 15327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15337c478bd9Sstevel@tonic-gate "fdisk: New partition cannot start at sector 0:\n" 15347c478bd9Sstevel@tonic-gate " \"%s\".\n", 15357c478bd9Sstevel@tonic-gate file); 15367c478bd9Sstevel@tonic-gate exit(1); 15377c478bd9Sstevel@tonic-gate } 15387c478bd9Sstevel@tonic-gate 15397c478bd9Sstevel@tonic-gate /* 15407c478bd9Sstevel@tonic-gate * if the user wishes to add an EFI partition, we need 15417c478bd9Sstevel@tonic-gate * more extensive validation. rsect should be 1, and 15427c478bd9Sstevel@tonic-gate * numsect should equal the entire disk capacity - 1 15437c478bd9Sstevel@tonic-gate */ 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate if (id == EFI_PMBR) { 15467c478bd9Sstevel@tonic-gate if (rsect != 1) { 15477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15487c478bd9Sstevel@tonic-gate "fdisk: EFI partitions must start at sector" 15497c478bd9Sstevel@tonic-gate " 1 (input rsect = %d)\n", rsect); 15507c478bd9Sstevel@tonic-gate exit(1); 15517c478bd9Sstevel@tonic-gate } 15527c478bd9Sstevel@tonic-gate 1553342440ecSPrasad Singamsetty 1554342440ecSPrasad Singamsetty if (dev_capacity > DK_MAX_2TB) { 1555342440ecSPrasad Singamsetty if (numsect != DK_MAX_2TB) { 1556342440ecSPrasad Singamsetty (void) fprintf(stderr, 1557342440ecSPrasad Singamsetty "fdisk: EFI partitions must " 1558342440ecSPrasad Singamsetty "encompass the entire maximum 2 TB " 1559342440ecSPrasad Singamsetty "(input numsect: %u - max: %llu)\n", 1560342440ecSPrasad Singamsetty numsect, (diskaddr_t)DK_MAX_2TB); 1561342440ecSPrasad Singamsetty exit(1); 1562342440ecSPrasad Singamsetty } 1563342440ecSPrasad Singamsetty } else if (numsect != dev_capacity - 1) { 15647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15657c478bd9Sstevel@tonic-gate "fdisk: EFI partitions must encompass the " 1566bb16350dSlclee "entire disk\n" 1567342440ecSPrasad Singamsetty "(input numsect: %u - avail: %llu)\n", 1568bb16350dSlclee numsect, 15691237e847Slclee dev_capacity - 1); 15707c478bd9Sstevel@tonic-gate exit(1); 15717c478bd9Sstevel@tonic-gate } 15727c478bd9Sstevel@tonic-gate } 15737c478bd9Sstevel@tonic-gate 15747c478bd9Sstevel@tonic-gate /* Find unused entry for use and put entry in table */ 15757c478bd9Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect, 15767c478bd9Sstevel@tonic-gate ecyl, rsect, numsect) < 0) { 15777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 15787c478bd9Sstevel@tonic-gate "fdisk: Invalid entry could not be inserted:\n" 15797c478bd9Sstevel@tonic-gate " \"%s\"\n", 15807c478bd9Sstevel@tonic-gate file); 15817c478bd9Sstevel@tonic-gate exit(1); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate /* Make sure new entry does not overlap existing entry */ 15857c478bd9Sstevel@tonic-gate if (verify_tbl() < 0) { 1586bb16350dSlclee (void) fprintf(stderr, 1587bb16350dSlclee "fdisk: Cannot create partition \"%s\"\n", file); 15887c478bd9Sstevel@tonic-gate exit(1); 15897c478bd9Sstevel@tonic-gate } 15907c478bd9Sstevel@tonic-gate } /* switch funct */ 15917c478bd9Sstevel@tonic-gate } 15927c478bd9Sstevel@tonic-gate 15937c478bd9Sstevel@tonic-gate /* 15947c478bd9Sstevel@tonic-gate * Set_Table_CHS_Values 15957c478bd9Sstevel@tonic-gate * 15967c478bd9Sstevel@tonic-gate * This will calculate the CHS values for beginning and ending CHS 15977c478bd9Sstevel@tonic-gate * for a single partition table entry (ti) based on the relsect 15987c478bd9Sstevel@tonic-gate * and numsect values contained in the partion table entry. 15997c478bd9Sstevel@tonic-gate * 16007c478bd9Sstevel@tonic-gate * hba_heads and hba_sectors contain the number of heads and sectors. 16017c478bd9Sstevel@tonic-gate * 16027c478bd9Sstevel@tonic-gate * If the number of cylinders exceeds the MAX_CYL, 16037c478bd9Sstevel@tonic-gate * then maximum values will be placed in the corresponding chs entry. 16047c478bd9Sstevel@tonic-gate */ 16057c478bd9Sstevel@tonic-gate static void 16067c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(int ti) 16077c478bd9Sstevel@tonic-gate { 16087c478bd9Sstevel@tonic-gate uint32_t lba, cy, hd, sc; 16097c478bd9Sstevel@tonic-gate 16107c478bd9Sstevel@tonic-gate lba = (uint32_t)Table[ti].relsect; 16117c478bd9Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 16127c478bd9Sstevel@tonic-gate /* 16137c478bd9Sstevel@tonic-gate * the lba address cannot be expressed in CHS value 16147c478bd9Sstevel@tonic-gate * so store the maximum CHS field values in the CHS fields. 16157c478bd9Sstevel@tonic-gate */ 16167c478bd9Sstevel@tonic-gate cy = MAX_CYL + 1; 16177c478bd9Sstevel@tonic-gate hd = MAX_HEAD; 16187c478bd9Sstevel@tonic-gate sc = MAX_SECT; 16197c478bd9Sstevel@tonic-gate } else { 16207c478bd9Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 16217c478bd9Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 16227c478bd9Sstevel@tonic-gate sc = lba % hba_sectors + 1; 16237c478bd9Sstevel@tonic-gate } 16247c478bd9Sstevel@tonic-gate Table[ti].begcyl = cy & 0xff; 1625bb16350dSlclee Table[ti].beghead = (uchar_t)hd; 1626bb16350dSlclee Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate /* 16297c478bd9Sstevel@tonic-gate * This code is identical to the code above 16307c478bd9Sstevel@tonic-gate * except that it works on ending CHS values 16317c478bd9Sstevel@tonic-gate */ 16327c478bd9Sstevel@tonic-gate lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1); 16337c478bd9Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 16347c478bd9Sstevel@tonic-gate cy = MAX_CYL + 1; 16357c478bd9Sstevel@tonic-gate hd = MAX_HEAD; 16367c478bd9Sstevel@tonic-gate sc = MAX_SECT; 16377c478bd9Sstevel@tonic-gate } else { 16387c478bd9Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 16397c478bd9Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 16407c478bd9Sstevel@tonic-gate sc = lba % hba_sectors + 1; 16417c478bd9Sstevel@tonic-gate } 16427c478bd9Sstevel@tonic-gate Table[ti].endcyl = cy & 0xff; 1643bb16350dSlclee Table[ti].endhead = (uchar_t)hd; 1644bb16350dSlclee Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 16457c478bd9Sstevel@tonic-gate } 16467c478bd9Sstevel@tonic-gate 16477c478bd9Sstevel@tonic-gate /* 16487c478bd9Sstevel@tonic-gate * insert_tbl 16497c478bd9Sstevel@tonic-gate * Insert entry into fdisk table. Check all user-supplied values 16507c478bd9Sstevel@tonic-gate * for the entry, but not the validity relative to other table 16517c478bd9Sstevel@tonic-gate * entries! 16527c478bd9Sstevel@tonic-gate */ 1653bb16350dSlclee static int 1654bb16350dSlclee insert_tbl( 1655bb16350dSlclee int id, int act, 1656bb16350dSlclee int bhead, int bsect, int bcyl, 1657bb16350dSlclee int ehead, int esect, int ecyl, 1658342440ecSPrasad Singamsetty uint32_t rsect, uint32_t numsect) 16597c478bd9Sstevel@tonic-gate { 16607c478bd9Sstevel@tonic-gate int i; 16617c478bd9Sstevel@tonic-gate 16627c478bd9Sstevel@tonic-gate /* validate partition size */ 1663342440ecSPrasad Singamsetty if (((diskaddr_t)rsect + numsect) > dev_capacity) { 1664bb16350dSlclee (void) fprintf(stderr, 16657c478bd9Sstevel@tonic-gate "fdisk: Partition table exceeds the size of the disk.\n"); 16667c478bd9Sstevel@tonic-gate return (-1); 16677c478bd9Sstevel@tonic-gate } 16687c478bd9Sstevel@tonic-gate 1669342440ecSPrasad Singamsetty 16707c478bd9Sstevel@tonic-gate /* find UNUSED partition table entry */ 16717c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 16727c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 16737c478bd9Sstevel@tonic-gate break; 16747c478bd9Sstevel@tonic-gate } 16757c478bd9Sstevel@tonic-gate } 16767c478bd9Sstevel@tonic-gate if (i >= FD_NUMPART) { 1677bb16350dSlclee (void) fprintf(stderr, "fdisk: Partition table is full.\n"); 16787c478bd9Sstevel@tonic-gate return (-1); 16797c478bd9Sstevel@tonic-gate } 16807c478bd9Sstevel@tonic-gate 16817c478bd9Sstevel@tonic-gate 1682bb16350dSlclee Table[i].systid = (uchar_t)id; 1683bb16350dSlclee Table[i].bootid = (uchar_t)act; 1684f85c7842SSuhasini Peddada Table[i].numsect = lel(numsect); 1685f85c7842SSuhasini Peddada Table[i].relsect = lel(rsect); 16867c478bd9Sstevel@tonic-gate 16877c478bd9Sstevel@tonic-gate /* 16887c478bd9Sstevel@tonic-gate * If we have been called with a valid geometry, use it 16897c478bd9Sstevel@tonic-gate * valid means non-zero values that fit in the BIOS fields 16907c478bd9Sstevel@tonic-gate */ 16917c478bd9Sstevel@tonic-gate if (0 < bsect && bsect <= MAX_SECT && 16927c478bd9Sstevel@tonic-gate 0 <= bhead && bhead <= MAX_HEAD && 16937c478bd9Sstevel@tonic-gate 0 < esect && esect <= MAX_SECT && 16947c478bd9Sstevel@tonic-gate 0 <= ehead && ehead <= MAX_HEAD) { 16957c478bd9Sstevel@tonic-gate if (bcyl > MAX_CYL) 16967c478bd9Sstevel@tonic-gate bcyl = MAX_CYL + 1; 16977c478bd9Sstevel@tonic-gate if (ecyl > MAX_CYL) 16987c478bd9Sstevel@tonic-gate ecyl = MAX_CYL + 1; 16997c478bd9Sstevel@tonic-gate Table[i].begcyl = bcyl & 0xff; 17007c478bd9Sstevel@tonic-gate Table[i].endcyl = ecyl & 0xff; 1701bb16350dSlclee Table[i].beghead = (uchar_t)bhead; 1702bb16350dSlclee Table[i].endhead = (uchar_t)ehead; 1703bb16350dSlclee Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect); 17047c478bd9Sstevel@tonic-gate Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect; 17057c478bd9Sstevel@tonic-gate } else { 17067c478bd9Sstevel@tonic-gate 17077c478bd9Sstevel@tonic-gate /* 17087c478bd9Sstevel@tonic-gate * The specified values are invalid, 17097c478bd9Sstevel@tonic-gate * so calculate the values based on hba_heads, hba_sectors 17107c478bd9Sstevel@tonic-gate */ 17117c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(i); 17127c478bd9Sstevel@tonic-gate } 17137c478bd9Sstevel@tonic-gate 17147c478bd9Sstevel@tonic-gate /* 17157c478bd9Sstevel@tonic-gate * return partition index 17167c478bd9Sstevel@tonic-gate */ 17177c478bd9Sstevel@tonic-gate return (i); 17187c478bd9Sstevel@tonic-gate } 17197c478bd9Sstevel@tonic-gate 17207c478bd9Sstevel@tonic-gate /* 17212f8ec719SBarry Harding * entry_from_old_table 17222f8ec719SBarry Harding * If the specified entry is in the old table and is not a Solaris entry 17232f8ec719SBarry Harding * then insert same entry into new fdisk table. If we do this then 17242f8ec719SBarry Harding * all checks are skipped for that entry! 17252f8ec719SBarry Harding */ 17262f8ec719SBarry Harding static int 17272f8ec719SBarry Harding entry_from_old_table( 17282f8ec719SBarry Harding int id, int act, 17292f8ec719SBarry Harding int bhead, int bsect, int bcyl, 17302f8ec719SBarry Harding int ehead, int esect, int ecyl, 17312f8ec719SBarry Harding uint32_t rsect, uint32_t numsect) 17322f8ec719SBarry Harding { 17332f8ec719SBarry Harding uint32_t i, j; 17342f8ec719SBarry Harding 17352f8ec719SBarry Harding if (id == SUNIXOS || id == SUNIXOS2) 17362f8ec719SBarry Harding return (0); 17372f8ec719SBarry Harding for (i = 0; i < FD_NUMPART - 1; i++) { 17382f8ec719SBarry Harding if (Old_Table[i].systid == id && 17392f8ec719SBarry Harding Old_Table[i].bootid == act && 17402f8ec719SBarry Harding Old_Table[i].beghead == bhead && 17412f8ec719SBarry Harding Old_Table[i].begsect == ((bsect & 0x3f) | 17422f8ec719SBarry Harding (uchar_t)((bcyl>>2) & 0xc0)) && 17432f8ec719SBarry Harding Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 17442f8ec719SBarry Harding Old_Table[i].endhead == ehead && 17452f8ec719SBarry Harding Old_Table[i].endsect == ((esect & 0x3f) | 17462f8ec719SBarry Harding (uchar_t)((ecyl>>2) & 0xc0)) && 17472f8ec719SBarry Harding Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 17482f8ec719SBarry Harding Old_Table[i].relsect == lel(rsect) && 17492f8ec719SBarry Harding Old_Table[i].numsect == lel(numsect)) { 17502f8ec719SBarry Harding /* find UNUSED partition table entry */ 17512f8ec719SBarry Harding for (j = 0; j < FD_NUMPART; j++) { 17522f8ec719SBarry Harding if (Table[j].systid == UNUSED) { 17532f8ec719SBarry Harding (void) memcpy(&Table[j], &Old_Table[i], 17542f8ec719SBarry Harding sizeof (Table[0])); 17552f8ec719SBarry Harding skip_verify[j] = 1; 17562f8ec719SBarry Harding return (1); 17572f8ec719SBarry Harding 17582f8ec719SBarry Harding } 17592f8ec719SBarry Harding } 17602f8ec719SBarry Harding return (0); 17612f8ec719SBarry Harding } 17622f8ec719SBarry Harding 17632f8ec719SBarry Harding } 17642f8ec719SBarry Harding return (0); 17652f8ec719SBarry Harding } 17662f8ec719SBarry Harding 17672f8ec719SBarry Harding /* 17687c478bd9Sstevel@tonic-gate * verify_tbl 17697c478bd9Sstevel@tonic-gate * Verify that no partition entries overlap or exceed the size of 17707c478bd9Sstevel@tonic-gate * the disk. 17717c478bd9Sstevel@tonic-gate */ 1772bb16350dSlclee static int 1773bb16350dSlclee verify_tbl(void) 17747c478bd9Sstevel@tonic-gate { 1775342440ecSPrasad Singamsetty uint32_t i, j, rsect, numsect; 17767c478bd9Sstevel@tonic-gate int noMoreParts = 0; 17777c478bd9Sstevel@tonic-gate int numParts = 0; 17787c478bd9Sstevel@tonic-gate 17797c478bd9Sstevel@tonic-gate /* Make sure new entry does not overlap an existing entry */ 17807c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART - 1; i++) { 17817c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 17827c478bd9Sstevel@tonic-gate numParts++; 17837c478bd9Sstevel@tonic-gate /* 17847c478bd9Sstevel@tonic-gate * No valid partitions allowed after an UNUSED or 17857c478bd9Sstevel@tonic-gate * EFI_PMBR part 17867c478bd9Sstevel@tonic-gate */ 17877c478bd9Sstevel@tonic-gate if (noMoreParts) { 17887c478bd9Sstevel@tonic-gate return (-1); 17897c478bd9Sstevel@tonic-gate } 17907c478bd9Sstevel@tonic-gate 17917c478bd9Sstevel@tonic-gate /* 17927c478bd9Sstevel@tonic-gate * EFI_PMBR partitions must be the only partition 17937c478bd9Sstevel@tonic-gate * and must be Table entry 0 17947c478bd9Sstevel@tonic-gate */ 17957c478bd9Sstevel@tonic-gate if (Table[i].systid == EFI_PMBR) { 17967c478bd9Sstevel@tonic-gate if (i == 0) { 17977c478bd9Sstevel@tonic-gate noMoreParts = 1; 17987c478bd9Sstevel@tonic-gate } else { 17997c478bd9Sstevel@tonic-gate return (-1); 18007c478bd9Sstevel@tonic-gate } 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate if (Table[i].relsect != 1) { 1803bb16350dSlclee (void) fprintf(stderr, "ERROR: " 18047c478bd9Sstevel@tonic-gate "Invalid starting sector " 18057c478bd9Sstevel@tonic-gate "for EFI_PMBR partition:\n" 18067c478bd9Sstevel@tonic-gate "relsect %d " 18077c478bd9Sstevel@tonic-gate "(should be 1)\n", 18087c478bd9Sstevel@tonic-gate Table[i].relsect); 18097c478bd9Sstevel@tonic-gate 18107c478bd9Sstevel@tonic-gate return (-1); 18117c478bd9Sstevel@tonic-gate } 18127c478bd9Sstevel@tonic-gate 18131237e847Slclee if (Table[i].numsect != dev_capacity - 1) { 1814bb16350dSlclee (void) fprintf(stderr, "ERROR: " 18157c478bd9Sstevel@tonic-gate "EFI_PMBR partition must " 18167c478bd9Sstevel@tonic-gate "encompass the entire " 1817bb16350dSlclee "disk.\n numsect %d - " 18181237e847Slclee "actual %llu\n", 18197c478bd9Sstevel@tonic-gate Table[i].numsect, 18201237e847Slclee dev_capacity - 1); 18217c478bd9Sstevel@tonic-gate 18227c478bd9Sstevel@tonic-gate return (-1); 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate /* make sure the partition isn't larger than the disk */ 1827f85c7842SSuhasini Peddada rsect = lel(Table[i].relsect); 1828f85c7842SSuhasini Peddada numsect = lel(Table[i].numsect); 1829342440ecSPrasad Singamsetty 1830342440ecSPrasad Singamsetty if ((((diskaddr_t)rsect + numsect) > dev_capacity) || 1831342440ecSPrasad Singamsetty (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) { 18322f8ec719SBarry Harding if (!skip_verify[i]) 18337c478bd9Sstevel@tonic-gate return (-1); 18347c478bd9Sstevel@tonic-gate } 18357c478bd9Sstevel@tonic-gate 18367c478bd9Sstevel@tonic-gate for (j = i + 1; j < FD_NUMPART; j++) { 18377c478bd9Sstevel@tonic-gate if (Table[j].systid != UNUSED) { 1838342440ecSPrasad Singamsetty uint32_t t_relsect = 1839f85c7842SSuhasini Peddada lel(Table[j].relsect); 1840342440ecSPrasad Singamsetty uint32_t t_numsect = 1841f85c7842SSuhasini Peddada lel(Table[j].numsect); 18427c478bd9Sstevel@tonic-gate 18437c478bd9Sstevel@tonic-gate if (noMoreParts) { 1844bb16350dSlclee (void) fprintf(stderr, 18457c478bd9Sstevel@tonic-gate "Cannot add partition to " 18467c478bd9Sstevel@tonic-gate "table; no more partitions " 18477c478bd9Sstevel@tonic-gate "allowed\n"); 18487c478bd9Sstevel@tonic-gate 18497c478bd9Sstevel@tonic-gate if (io_debug) { 1850bb16350dSlclee (void) fprintf(stderr, 18517c478bd9Sstevel@tonic-gate "DEBUG: Current " 18527c478bd9Sstevel@tonic-gate "partition:\t" 18537c478bd9Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1854bb16350dSlclee "%d:%d:%d:%d:%d\n" 18557c478bd9Sstevel@tonic-gate " Next " 18567c478bd9Sstevel@tonic-gate "partition:\t\t" 18577c478bd9Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1858bb16350dSlclee "%d:%d:%d:%d:%d\n", 18597c478bd9Sstevel@tonic-gate Table[i].systid, 18607c478bd9Sstevel@tonic-gate Table[i].bootid, 18617c478bd9Sstevel@tonic-gate Table[i].begcyl, 18627c478bd9Sstevel@tonic-gate Table[i].beghead, 18637c478bd9Sstevel@tonic-gate Table[i].begsect, 18647c478bd9Sstevel@tonic-gate Table[i].endcyl, 18657c478bd9Sstevel@tonic-gate Table[i].endhead, 18667c478bd9Sstevel@tonic-gate Table[i].endsect, 18677c478bd9Sstevel@tonic-gate Table[i].relsect, 18687c478bd9Sstevel@tonic-gate Table[i].numsect, 18697c478bd9Sstevel@tonic-gate Table[j].systid, 18707c478bd9Sstevel@tonic-gate Table[j].bootid, 18717c478bd9Sstevel@tonic-gate Table[j].begcyl, 18727c478bd9Sstevel@tonic-gate Table[j].beghead, 18737c478bd9Sstevel@tonic-gate Table[j].begsect, 18747c478bd9Sstevel@tonic-gate Table[j].endcyl, 18757c478bd9Sstevel@tonic-gate Table[j].endhead, 18767c478bd9Sstevel@tonic-gate Table[j].endsect, 18777c478bd9Sstevel@tonic-gate Table[j].relsect, 18787c478bd9Sstevel@tonic-gate Table[j].numsect); 18797c478bd9Sstevel@tonic-gate } 18807c478bd9Sstevel@tonic-gate 18817c478bd9Sstevel@tonic-gate return (-1); 18827c478bd9Sstevel@tonic-gate } 18837c478bd9Sstevel@tonic-gate if ((rsect >= 18847c478bd9Sstevel@tonic-gate (t_relsect + t_numsect)) || 18857c478bd9Sstevel@tonic-gate ((rsect + numsect) <= t_relsect)) { 18867c478bd9Sstevel@tonic-gate continue; 18877c478bd9Sstevel@tonic-gate } else { 1888bb16350dSlclee (void) fprintf(stderr, "ERROR: " 18897c478bd9Sstevel@tonic-gate "current partition overlaps" 18907c478bd9Sstevel@tonic-gate " following partition\n"); 18917c478bd9Sstevel@tonic-gate 18927c478bd9Sstevel@tonic-gate return (-1); 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate } 18957c478bd9Sstevel@tonic-gate } 18967c478bd9Sstevel@tonic-gate } else { 18977c478bd9Sstevel@tonic-gate noMoreParts = 1; 18987c478bd9Sstevel@tonic-gate } 18997c478bd9Sstevel@tonic-gate } 19007c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 19012f8ec719SBarry Harding if (noMoreParts) 19022f8ec719SBarry Harding return (-1); 19032f8ec719SBarry Harding if (!skip_verify[i] && 19042f8ec719SBarry Harding ((((diskaddr_t)lel(Table[i].relsect) + 1905f85c7842SSuhasini Peddada lel(Table[i].numsect)) > dev_capacity) || 1906f85c7842SSuhasini Peddada (((diskaddr_t)lel(Table[i].relsect) + 19072f8ec719SBarry Harding lel(Table[i].numsect)) > DK_MAX_2TB))) { 19087c478bd9Sstevel@tonic-gate return (-1); 19097c478bd9Sstevel@tonic-gate } 19107c478bd9Sstevel@tonic-gate } 19117c478bd9Sstevel@tonic-gate 19127c478bd9Sstevel@tonic-gate return (numParts); 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate 19157c478bd9Sstevel@tonic-gate /* 19167c478bd9Sstevel@tonic-gate * pars_fdisk 19177c478bd9Sstevel@tonic-gate * Parse user-supplied data to set up fdisk partitions 19187c478bd9Sstevel@tonic-gate * (-A, -D, -F). 19197c478bd9Sstevel@tonic-gate */ 1920bb16350dSlclee static int 1921bb16350dSlclee pars_fdisk( 1922bb16350dSlclee char *line, 1923bb16350dSlclee int *id, int *act, 1924bb16350dSlclee int *bhead, int *bsect, int *bcyl, 1925bb16350dSlclee int *ehead, int *esect, int *ecyl, 1926342440ecSPrasad Singamsetty uint32_t *rsect, uint32_t *numsect) 19277c478bd9Sstevel@tonic-gate { 19287c478bd9Sstevel@tonic-gate int i; 19297c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 19307c478bd9Sstevel@tonic-gate return (1); 19317c478bd9Sstevel@tonic-gate line[strlen(line)] = '\0'; 19327c478bd9Sstevel@tonic-gate for (i = 0; i < strlen(line); i++) { 19337c478bd9Sstevel@tonic-gate if (line[i] == '\0') { 19347c478bd9Sstevel@tonic-gate break; 19357c478bd9Sstevel@tonic-gate } else if (line[i] == ':') { 19367c478bd9Sstevel@tonic-gate line[i] = ' '; 19377c478bd9Sstevel@tonic-gate } 19387c478bd9Sstevel@tonic-gate } 1939342440ecSPrasad Singamsetty if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u", 19407c478bd9Sstevel@tonic-gate id, act, bhead, bsect, bcyl, ehead, esect, ecyl, 19417c478bd9Sstevel@tonic-gate rsect, numsect) != 10) { 19427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error:\n \"%s\".\n", line); 19437c478bd9Sstevel@tonic-gate exit(1); 19447c478bd9Sstevel@tonic-gate } 19457c478bd9Sstevel@tonic-gate return (0); 19467c478bd9Sstevel@tonic-gate } 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate /* 19497c478bd9Sstevel@tonic-gate * validate_part 19507c478bd9Sstevel@tonic-gate * Validate that a new partition does not start at sector 0. Only UNUSED 19517c478bd9Sstevel@tonic-gate * partitions and previously existing partitions are allowed to start at 0. 19527c478bd9Sstevel@tonic-gate */ 1953bb16350dSlclee static int 1954342440ecSPrasad Singamsetty validate_part(int id, uint32_t rsect, uint32_t numsect) 19557c478bd9Sstevel@tonic-gate { 19567c478bd9Sstevel@tonic-gate int i; 19577c478bd9Sstevel@tonic-gate if ((id != UNUSED) && (rsect == 0)) { 19587c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 19597c478bd9Sstevel@tonic-gate if ((Old_Table[i].systid == id) && 1960f85c7842SSuhasini Peddada (Old_Table[i].relsect == lel(rsect)) && 1961f85c7842SSuhasini Peddada (Old_Table[i].numsect == lel(numsect))) 19621237e847Slclee return (0); 19637c478bd9Sstevel@tonic-gate } 1964bb16350dSlclee (void) fprintf(stderr, 1965bb16350dSlclee "New partition cannot start at sector 0\n"); 19667c478bd9Sstevel@tonic-gate return (-1); 19677c478bd9Sstevel@tonic-gate } 19687c478bd9Sstevel@tonic-gate return (0); 19697c478bd9Sstevel@tonic-gate } 19707c478bd9Sstevel@tonic-gate 19717c478bd9Sstevel@tonic-gate /* 19727c478bd9Sstevel@tonic-gate * stage0 19737c478bd9Sstevel@tonic-gate * Print out interactive menu and process user input. 19747c478bd9Sstevel@tonic-gate */ 1975bb16350dSlclee static void 1976bb16350dSlclee stage0(void) 19777c478bd9Sstevel@tonic-gate { 1978bb16350dSlclee dispmenu(); 1979bb16350dSlclee for (;;) { 1980bb16350dSlclee (void) printf(Q_LINE); 1981bb16350dSlclee (void) printf("Enter Selection: "); 1982933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 19837c478bd9Sstevel@tonic-gate rm_blanks(s); 1984933c1499SBarry Harding while (!((s[0] > '0') && (s[0] < '7') && 1985933c1499SBarry Harding ((s[1] == '\0') || (s[1] == '\n')))) { 1986bb16350dSlclee (void) printf(E_LINE); /* Clear any previous error */ 1987bb16350dSlclee (void) printf( 1988bb16350dSlclee "Enter a one-digit number between 1 and 6."); 1989bb16350dSlclee (void) printf(Q_LINE); 1990bb16350dSlclee (void) printf("Enter Selection: "); 1991933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 19927c478bd9Sstevel@tonic-gate rm_blanks(s); 19937c478bd9Sstevel@tonic-gate } 1994bb16350dSlclee (void) printf(E_LINE); 19957c478bd9Sstevel@tonic-gate switch (s[0]) { 19967c478bd9Sstevel@tonic-gate case '1': 19977c478bd9Sstevel@tonic-gate if (pcreate() == -1) 19987c478bd9Sstevel@tonic-gate return; 19997c478bd9Sstevel@tonic-gate break; 20007c478bd9Sstevel@tonic-gate case '2': 20017c478bd9Sstevel@tonic-gate if (pchange() == -1) 20027c478bd9Sstevel@tonic-gate return; 20037c478bd9Sstevel@tonic-gate break; 20047c478bd9Sstevel@tonic-gate case '3': 20057c478bd9Sstevel@tonic-gate if (pdelete() == -1) 20067c478bd9Sstevel@tonic-gate return; 20077c478bd9Sstevel@tonic-gate break; 20087c478bd9Sstevel@tonic-gate case '4': 20097c478bd9Sstevel@tonic-gate if (ppartid() == -1) 20107c478bd9Sstevel@tonic-gate return; 20117c478bd9Sstevel@tonic-gate break; 20127c478bd9Sstevel@tonic-gate case '5': 20137c478bd9Sstevel@tonic-gate /* update disk partition table, if changed */ 20147c478bd9Sstevel@tonic-gate if (TableChanged() == 1) { 20157c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 20167c478bd9Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 20177c478bd9Sstevel@tonic-gate } 20187c478bd9Sstevel@tonic-gate /* 20197c478bd9Sstevel@tonic-gate * If the VTOC table is wrong fix it 20207c478bd9Sstevel@tonic-gate * (truncate only) 20217c478bd9Sstevel@tonic-gate */ 20227c478bd9Sstevel@tonic-gate if (io_adjt) { 20237c478bd9Sstevel@tonic-gate fix_slice(); 20247c478bd9Sstevel@tonic-gate } 2025bb16350dSlclee (void) close(Dev); 20267c478bd9Sstevel@tonic-gate exit(0); 2027bb16350dSlclee /* FALLTHRU */ 20287c478bd9Sstevel@tonic-gate case '6': 20297c478bd9Sstevel@tonic-gate /* 20307c478bd9Sstevel@tonic-gate * If the VTOC table is wrong fix it 20317c478bd9Sstevel@tonic-gate * (truncate only) 20327c478bd9Sstevel@tonic-gate */ 20337c478bd9Sstevel@tonic-gate if (io_adjt) { 20347c478bd9Sstevel@tonic-gate fix_slice(); 20357c478bd9Sstevel@tonic-gate } 2036bb16350dSlclee (void) close(Dev); 20377c478bd9Sstevel@tonic-gate exit(0); 2038bb16350dSlclee /* FALLTHRU */ 20397c478bd9Sstevel@tonic-gate default: 20407c478bd9Sstevel@tonic-gate break; 20417c478bd9Sstevel@tonic-gate } 20427c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 20437c478bd9Sstevel@tonic-gate disptbl(); 2044bb16350dSlclee dispmenu(); 20457c478bd9Sstevel@tonic-gate } 20467c478bd9Sstevel@tonic-gate } 20477c478bd9Sstevel@tonic-gate 20487c478bd9Sstevel@tonic-gate /* 20497c478bd9Sstevel@tonic-gate * pcreate 20507c478bd9Sstevel@tonic-gate * Create partition entry in the table (interactive mode). 20517c478bd9Sstevel@tonic-gate */ 2052bb16350dSlclee static int 2053bb16350dSlclee pcreate(void) 20547c478bd9Sstevel@tonic-gate { 2055bb16350dSlclee uchar_t tsystid = 'z'; 20567c478bd9Sstevel@tonic-gate int i, j; 2057342440ecSPrasad Singamsetty uint32_t numsect; 20587c478bd9Sstevel@tonic-gate int retCode = 0; 20597c478bd9Sstevel@tonic-gate 20607c478bd9Sstevel@tonic-gate i = 0; 2061bb16350dSlclee for (;;) { 20627c478bd9Sstevel@tonic-gate if (i == FD_NUMPART) { 2063bb16350dSlclee (void) printf(E_LINE); 2064bb16350dSlclee (void) printf( 2065bb16350dSlclee "The partition table is full!\n" 2066bb16350dSlclee "You must delete a partition before creating" 20677c478bd9Sstevel@tonic-gate " a new one.\n"); 20687c478bd9Sstevel@tonic-gate return (-1); 20697c478bd9Sstevel@tonic-gate } 20707c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 20717c478bd9Sstevel@tonic-gate break; 20727c478bd9Sstevel@tonic-gate } 20737c478bd9Sstevel@tonic-gate i++; 20747c478bd9Sstevel@tonic-gate } 20757c478bd9Sstevel@tonic-gate 2076342440ecSPrasad Singamsetty numsect = 0; 20777c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 20787c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 2079f85c7842SSuhasini Peddada numsect += lel(Table[i].numsect); 20807c478bd9Sstevel@tonic-gate } 2081342440ecSPrasad Singamsetty if (numsect >= chs_capacity) { 2082bb16350dSlclee (void) printf(E_LINE); 2083bb16350dSlclee (void) printf("There is no more room on the disk for" 20847c478bd9Sstevel@tonic-gate " another partition.\n"); 2085bb16350dSlclee (void) printf( 2086bb16350dSlclee "You must delete a partition before creating" 20877c478bd9Sstevel@tonic-gate " a new one.\n"); 20887c478bd9Sstevel@tonic-gate return (-1); 20897c478bd9Sstevel@tonic-gate } 20907c478bd9Sstevel@tonic-gate } 20917c478bd9Sstevel@tonic-gate while (tsystid == 'z') { 2092342440ecSPrasad Singamsetty 2093342440ecSPrasad Singamsetty /* 2094342440ecSPrasad Singamsetty * The question here is expanding to more than what is 2095342440ecSPrasad Singamsetty * allocated for question lines (Q_LINE) which garbles 2096342440ecSPrasad Singamsetty * at least warning line. Clearing warning line as workaround 2097342440ecSPrasad Singamsetty * for now. 2098342440ecSPrasad Singamsetty */ 2099342440ecSPrasad Singamsetty 2100342440ecSPrasad Singamsetty (void) printf(W_LINE); 2101bb16350dSlclee (void) printf(Q_LINE); 2102bb16350dSlclee (void) printf( 2103bb16350dSlclee "Select the partition type to create:\n" 2104bb16350dSlclee " 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n" 2105bb16350dSlclee " 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n" 2106bb16350dSlclee " 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n" 2107bb16350dSlclee " D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? "); 2108933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 21097c478bd9Sstevel@tonic-gate rm_blanks(s); 2110933c1499SBarry Harding if ((s[1] != '\0') && (s[1] != '\n')) { 2111bb16350dSlclee (void) printf(E_LINE); 2112bb16350dSlclee (void) printf("Invalid selection, try again."); 21137c478bd9Sstevel@tonic-gate continue; 21147c478bd9Sstevel@tonic-gate } 21157c478bd9Sstevel@tonic-gate switch (s[0]) { 21167c478bd9Sstevel@tonic-gate case '0': /* exit */ 2117bb16350dSlclee (void) printf(E_LINE); 21187c478bd9Sstevel@tonic-gate return (-1); 21197c478bd9Sstevel@tonic-gate case '1': /* Solaris partition */ 21207c478bd9Sstevel@tonic-gate tsystid = SUNIXOS2; 21217c478bd9Sstevel@tonic-gate break; 21227c478bd9Sstevel@tonic-gate case '2': /* UNIX partition */ 21237c478bd9Sstevel@tonic-gate tsystid = UNIXOS; 21247c478bd9Sstevel@tonic-gate break; 21257c478bd9Sstevel@tonic-gate case '3': /* PCIXOS partition */ 21267c478bd9Sstevel@tonic-gate tsystid = PCIXOS; 21277c478bd9Sstevel@tonic-gate break; 21287c478bd9Sstevel@tonic-gate case '4': /* OTHEROS System partition */ 21297c478bd9Sstevel@tonic-gate tsystid = OTHEROS; 21307c478bd9Sstevel@tonic-gate break; 21317c478bd9Sstevel@tonic-gate case '5': 21327c478bd9Sstevel@tonic-gate tsystid = DOSOS12; /* DOS 12 bit fat */ 21337c478bd9Sstevel@tonic-gate break; 21347c478bd9Sstevel@tonic-gate case '6': 21357c478bd9Sstevel@tonic-gate tsystid = DOSOS16; /* DOS 16 bit fat */ 21367c478bd9Sstevel@tonic-gate break; 21377c478bd9Sstevel@tonic-gate case '7': 21387c478bd9Sstevel@tonic-gate tsystid = EXTDOS; 21397c478bd9Sstevel@tonic-gate break; 21407c478bd9Sstevel@tonic-gate case '8': 21417c478bd9Sstevel@tonic-gate tsystid = DOSHUGE; 21427c478bd9Sstevel@tonic-gate break; 21437c478bd9Sstevel@tonic-gate case '9': 21447c478bd9Sstevel@tonic-gate tsystid = FDISK_FAT95; /* FAT16, need extended int13 */ 21457c478bd9Sstevel@tonic-gate break; 21467c478bd9Sstevel@tonic-gate case 'a': /* x86 Boot partition */ 21477c478bd9Sstevel@tonic-gate case 'A': 21487c478bd9Sstevel@tonic-gate tsystid = X86BOOT; 21497c478bd9Sstevel@tonic-gate break; 21507c478bd9Sstevel@tonic-gate case 'b': /* Diagnostic boot partition */ 21517c478bd9Sstevel@tonic-gate case 'B': 21527c478bd9Sstevel@tonic-gate tsystid = DIAGPART; 21537c478bd9Sstevel@tonic-gate break; 21547c478bd9Sstevel@tonic-gate case 'c': /* FAT32 */ 21557c478bd9Sstevel@tonic-gate case 'C': 21567c478bd9Sstevel@tonic-gate tsystid = FDISK_WINDOWS; 21577c478bd9Sstevel@tonic-gate break; 21587c478bd9Sstevel@tonic-gate case 'd': /* FAT32 and need extended int13 */ 21597c478bd9Sstevel@tonic-gate case 'D': 21607c478bd9Sstevel@tonic-gate tsystid = FDISK_EXT_WIN; 21617c478bd9Sstevel@tonic-gate break; 21627c478bd9Sstevel@tonic-gate case 'e': /* Extended partition, need extended int13 */ 21637c478bd9Sstevel@tonic-gate case 'E': 21647c478bd9Sstevel@tonic-gate tsystid = FDISK_EXTLBA; 21657c478bd9Sstevel@tonic-gate break; 21667c478bd9Sstevel@tonic-gate case 'f': 21677c478bd9Sstevel@tonic-gate case 'F': 21687c478bd9Sstevel@tonic-gate tsystid = EFI_PMBR; 21697c478bd9Sstevel@tonic-gate break; 21707c478bd9Sstevel@tonic-gate default: 2171bb16350dSlclee (void) printf(E_LINE); 2172bb16350dSlclee (void) printf("Invalid selection, try again."); 21737c478bd9Sstevel@tonic-gate continue; 21747c478bd9Sstevel@tonic-gate } 21757c478bd9Sstevel@tonic-gate } 21767c478bd9Sstevel@tonic-gate 2177bb16350dSlclee (void) printf(E_LINE); 21787c478bd9Sstevel@tonic-gate 21797c478bd9Sstevel@tonic-gate if (tsystid != EFI_PMBR) { 2180342440ecSPrasad Singamsetty (void) printf(W_LINE); 2181342440ecSPrasad Singamsetty if ((dev_capacity > DK_MAX_2TB)) 2182342440ecSPrasad Singamsetty (void) printf("WARNING: Disk is larger than 2 TB. " 2183342440ecSPrasad Singamsetty "Upper limit is 2 TB for non-EFI partition ID\n"); 2184342440ecSPrasad Singamsetty 21857c478bd9Sstevel@tonic-gate /* create the new partition */ 21867c478bd9Sstevel@tonic-gate i = specify(tsystid); 21877c478bd9Sstevel@tonic-gate 21887c478bd9Sstevel@tonic-gate if (i != -1) { 21897c478bd9Sstevel@tonic-gate /* see if it should be the active partition */ 2190bb16350dSlclee (void) printf(E_LINE); 2191bb16350dSlclee (void) printf(Q_LINE); 21927c478bd9Sstevel@tonic-gate 2193bb16350dSlclee (void) printf( 2194bb16350dSlclee "Should this become the active partition? If " 2195bb16350dSlclee "yes, it will be activated\n" 2196bb16350dSlclee "each time the computer is reset or turned on.\n" 2197bb16350dSlclee "Please type \"y\" or \"n\". "); 21987c478bd9Sstevel@tonic-gate 21997c478bd9Sstevel@tonic-gate if (yesno()) { 2200bb16350dSlclee (void) printf(E_LINE); 22017c478bd9Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 22027c478bd9Sstevel@tonic-gate if (j == i) { 22037c478bd9Sstevel@tonic-gate Table[j].bootid = ACTIVE; 2204bb16350dSlclee (void) printf(E_LINE); 2205bb16350dSlclee (void) printf( 2206bb16350dSlclee "Partition %d is now " 22077c478bd9Sstevel@tonic-gate "the active partition.", 22087c478bd9Sstevel@tonic-gate j + 1); 22097c478bd9Sstevel@tonic-gate } else { 22107c478bd9Sstevel@tonic-gate Table[j].bootid = 0; 22117c478bd9Sstevel@tonic-gate } 22127c478bd9Sstevel@tonic-gate } 22137c478bd9Sstevel@tonic-gate } else { 22147c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 22157c478bd9Sstevel@tonic-gate } 22167c478bd9Sstevel@tonic-gate 22177c478bd9Sstevel@tonic-gate /* set up the return code */ 22187c478bd9Sstevel@tonic-gate i = 1; 22197c478bd9Sstevel@tonic-gate } 22207c478bd9Sstevel@tonic-gate } else { 22217c478bd9Sstevel@tonic-gate /* 22227c478bd9Sstevel@tonic-gate * partitions of type EFI_PMBR must be the only partitions in 22237c478bd9Sstevel@tonic-gate * the table 22247c478bd9Sstevel@tonic-gate * 22257c478bd9Sstevel@tonic-gate * First, make sure there were no errors the table is 22267c478bd9Sstevel@tonic-gate * empty 22277c478bd9Sstevel@tonic-gate */ 22287c478bd9Sstevel@tonic-gate retCode = verify_tbl(); 22297c478bd9Sstevel@tonic-gate 22307c478bd9Sstevel@tonic-gate if (retCode < 0) { 2231bb16350dSlclee (void) fprintf(stderr, 22327c478bd9Sstevel@tonic-gate "fdisk: Cannot create EFI partition table; \n" 22337c478bd9Sstevel@tonic-gate "current partition table is invalid.\n"); 22347c478bd9Sstevel@tonic-gate return (-1); 22357c478bd9Sstevel@tonic-gate } else if (retCode > 0) { 2236bb16350dSlclee (void) printf( 2237bb16350dSlclee "An EFI partition must be the only partition on " 22387c478bd9Sstevel@tonic-gate "disk. You may manually delete existing\n" 2239bb16350dSlclee "partitions, or fdisk can do it.\n" 2240bb16350dSlclee "Do you want fdisk to destroy existing " 2241bb16350dSlclee "partitions?\n" 2242bb16350dSlclee "Please type \"y\" or \"n\". "); 22437c478bd9Sstevel@tonic-gate 22447c478bd9Sstevel@tonic-gate if (yesno()) { 22457c478bd9Sstevel@tonic-gate nulltbl(); 22467c478bd9Sstevel@tonic-gate } else { 22477c478bd9Sstevel@tonic-gate return (-1); 22487c478bd9Sstevel@tonic-gate } 22497c478bd9Sstevel@tonic-gate } 22507c478bd9Sstevel@tonic-gate 22517c478bd9Sstevel@tonic-gate /* create the table entry - i should be 0 */ 2252342440ecSPrasad Singamsetty i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1, 2253342440ecSPrasad Singamsetty (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB: 2254342440ecSPrasad Singamsetty (dev_capacity - 1)); 22557c478bd9Sstevel@tonic-gate 22567c478bd9Sstevel@tonic-gate if (i != 0) { 2257bb16350dSlclee (void) printf("Error creating EFI partition!!!\n"); 22587c478bd9Sstevel@tonic-gate i = -1; 22597c478bd9Sstevel@tonic-gate } else { 22607c478bd9Sstevel@tonic-gate 22617c478bd9Sstevel@tonic-gate /* EFI partitions are currently never active */ 22627c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 22637c478bd9Sstevel@tonic-gate 22647c478bd9Sstevel@tonic-gate /* set up the return code */ 22657c478bd9Sstevel@tonic-gate i = 1; 22667c478bd9Sstevel@tonic-gate } 22677c478bd9Sstevel@tonic-gate } 22687c478bd9Sstevel@tonic-gate 22697c478bd9Sstevel@tonic-gate return (i); 22707c478bd9Sstevel@tonic-gate } 22717c478bd9Sstevel@tonic-gate 22727c478bd9Sstevel@tonic-gate /* 22737c478bd9Sstevel@tonic-gate * specify 22747c478bd9Sstevel@tonic-gate * Query the user to specify the size of the new partition in 22757c478bd9Sstevel@tonic-gate * terms of percentage of the disk or by specifying the starting 22767c478bd9Sstevel@tonic-gate * cylinder and length in cylinders. 22777c478bd9Sstevel@tonic-gate */ 2278bb16350dSlclee static int 2279bb16350dSlclee specify(uchar_t tsystid) 22807c478bd9Sstevel@tonic-gate { 22811237e847Slclee int i, j, percent = -1; 2282342440ecSPrasad Singamsetty int cyl, cylen; 2283342440ecSPrasad Singamsetty diskaddr_t first_free, size_free; 2284342440ecSPrasad Singamsetty diskaddr_t max_free; 22851237e847Slclee int cyl_size; 22867c478bd9Sstevel@tonic-gate struct ipart *partition[FD_NUMPART]; 22877c478bd9Sstevel@tonic-gate 22881237e847Slclee cyl_size = heads * sectors; 22891237e847Slclee 22901237e847Slclee /* 22911237e847Slclee * make a local copy of the partition table 22921237e847Slclee * and sort it into relsect order 22931237e847Slclee */ 22941237e847Slclee for (i = 0; i < FD_NUMPART; i++) 22951237e847Slclee partition[i] = &Table[i]; 22961237e847Slclee 22971237e847Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 22981237e847Slclee if (partition[i]->systid == UNUSED) 22991237e847Slclee break; 23001237e847Slclee for (j = i + 1; j < FD_NUMPART; j++) { 23011237e847Slclee if (partition[j]->systid == UNUSED) 23021237e847Slclee break; 2303f85c7842SSuhasini Peddada if (lel(partition[j]->relsect) < 2304f85c7842SSuhasini Peddada lel(partition[i]->relsect)) { 23051237e847Slclee struct ipart *temp = partition[i]; 23061237e847Slclee partition[i] = partition[j]; 23071237e847Slclee partition[j] = temp; 23081237e847Slclee } 23091237e847Slclee } 23101237e847Slclee } 23111237e847Slclee 2312342440ecSPrasad Singamsetty 2313bb16350dSlclee (void) printf(Q_LINE); 2314bb16350dSlclee (void) printf( 2315bb16350dSlclee "Specify the percentage of disk to use for this partition\n" 2316bb16350dSlclee "(or type \"c\" to specify the size in cylinders). "); 2317933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 23187c478bd9Sstevel@tonic-gate rm_blanks(s); 23197c478bd9Sstevel@tonic-gate if (s[0] != 'c') { /* Specify size in percentage of disk */ 23207c478bd9Sstevel@tonic-gate i = 0; 2321933c1499SBarry Harding while ((s[i] != '\0') && (s[i] != '\n')) { 23227c478bd9Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 2323bb16350dSlclee (void) printf(E_LINE); 23241237e847Slclee (void) printf("Invalid percentage value " 23251237e847Slclee "specified; retry the operation."); 23267c478bd9Sstevel@tonic-gate return (-1); 23277c478bd9Sstevel@tonic-gate } 23287c478bd9Sstevel@tonic-gate i++; 23297c478bd9Sstevel@tonic-gate if (i > 3) { 2330bb16350dSlclee (void) printf(E_LINE); 23311237e847Slclee (void) printf("Invalid percentage value " 23321237e847Slclee "specified; retry the operation."); 23337c478bd9Sstevel@tonic-gate return (-1); 23347c478bd9Sstevel@tonic-gate } 23357c478bd9Sstevel@tonic-gate } 23367c478bd9Sstevel@tonic-gate if ((percent = atoi(s)) > 100) { 2337bb16350dSlclee (void) printf(E_LINE); 2338bb16350dSlclee (void) printf( 2339bb16350dSlclee "Percentage value is too large. The value must be" 23407c478bd9Sstevel@tonic-gate " between 1 and 100;\nretry the operation.\n"); 23417c478bd9Sstevel@tonic-gate return (-1); 23427c478bd9Sstevel@tonic-gate } 23437c478bd9Sstevel@tonic-gate if (percent < 1) { 2344bb16350dSlclee (void) printf(E_LINE); 2345bb16350dSlclee (void) printf( 2346bb16350dSlclee "Percentage value is too small. The value must be" 23477c478bd9Sstevel@tonic-gate " between 1 and 100;\nretry the operation.\n"); 23487c478bd9Sstevel@tonic-gate return (-1); 23497c478bd9Sstevel@tonic-gate } 23507c478bd9Sstevel@tonic-gate 23511237e847Slclee if (percent == 100) 2352342440ecSPrasad Singamsetty cylen = Numcyl_usable - 1; 23531237e847Slclee else 2354342440ecSPrasad Singamsetty cylen = (Numcyl_usable * percent) / 100; 23551237e847Slclee 23561237e847Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 23571237e847Slclee if ((tsystid == DOSOS12) && 23581237e847Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 23597c478bd9Sstevel@tonic-gate int n; 2360342440ecSPrasad Singamsetty n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable; 2361bb16350dSlclee (void) printf(E_LINE); 23621237e847Slclee (void) printf("Maximum size for a DOS partition " 23631237e847Slclee "is %d%%; retry the operation.", 23647c478bd9Sstevel@tonic-gate n <= 100 ? n : 100); 23657c478bd9Sstevel@tonic-gate return (-1); 23667c478bd9Sstevel@tonic-gate } 23677c478bd9Sstevel@tonic-gate 23687c478bd9Sstevel@tonic-gate 23691237e847Slclee max_free = 0; 23707c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 23717c478bd9Sstevel@tonic-gate 23721237e847Slclee /* 23731237e847Slclee * check for free space before partition i 23741237e847Slclee * where i varies from 0 to 3 23751237e847Slclee * 23761237e847Slclee * freespace after partition 3 is unusable 23771237e847Slclee * because there are no free partitions 23781237e847Slclee * 23791237e847Slclee * freespace begins at the end of previous partition 23801237e847Slclee * or cylinder 1 23811237e847Slclee */ 23821237e847Slclee if (i) { 23831237e847Slclee /* Not an empty table */ 2384f85c7842SSuhasini Peddada first_free = lel(partition[i - 1]->relsect) + 2385f85c7842SSuhasini Peddada lel(partition[i - 1]->numsect); 23867c478bd9Sstevel@tonic-gate } else { 23871237e847Slclee first_free = cyl_size; 23887c478bd9Sstevel@tonic-gate } 23897c478bd9Sstevel@tonic-gate 23901237e847Slclee /* 23911237e847Slclee * freespace ends before the current partition 23921237e847Slclee * or the end of the disk (chs end) 23931237e847Slclee */ 23947c478bd9Sstevel@tonic-gate if (partition[i]->systid == UNUSED) { 23951237e847Slclee size_free = chs_capacity - first_free; 23967c478bd9Sstevel@tonic-gate } else { 239733c00887SShidokht Yadegari /* 239833c00887SShidokht Yadegari * Partition might start before cylinder 1. 239933c00887SShidokht Yadegari * Make sure free space is not negative. 240033c00887SShidokht Yadegari */ 24011237e847Slclee size_free = 2402f85c7842SSuhasini Peddada (lel(partition[i]->relsect > first_free)) ? 2403f85c7842SSuhasini Peddada (lel(partition[i]->relsect) - first_free) : 2404f85c7842SSuhasini Peddada 0; 24057c478bd9Sstevel@tonic-gate } 24067c478bd9Sstevel@tonic-gate 24071237e847Slclee /* save largest free space */ 24081237e847Slclee if (max_free < size_free) 24091237e847Slclee max_free = size_free; 24101237e847Slclee 2411342440ecSPrasad Singamsetty if (((uint64_t)cylen * cyl_size) <= size_free) { 24127c478bd9Sstevel@tonic-gate /* We found a place to use */ 24137c478bd9Sstevel@tonic-gate break; 24147c478bd9Sstevel@tonic-gate } 24151237e847Slclee if (partition[i]->systid == UNUSED) { 2416bb16350dSlclee (void) printf(E_LINE); 24171237e847Slclee max_free /= (cyl_size); 24181237e847Slclee (void) fprintf(stderr, "fdisk: " 2419342440ecSPrasad Singamsetty "Maximum percentage available is %lld\n", 2420342440ecSPrasad Singamsetty 100 * max_free / Numcyl_usable); 24217c478bd9Sstevel@tonic-gate return (-1); 24227c478bd9Sstevel@tonic-gate } 24237c478bd9Sstevel@tonic-gate } 24247c478bd9Sstevel@tonic-gate 2425bb16350dSlclee (void) printf(E_LINE); 24261237e847Slclee if (i >= FD_NUMPART) { 24271237e847Slclee (void) fprintf(stderr, 24281237e847Slclee "fdisk: Partition table is full.\n"); 24291237e847Slclee return (-1); 24301237e847Slclee } 24311237e847Slclee 24321237e847Slclee if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 24331237e847Slclee first_free, cylen * cyl_size)) >= 0) { 24341237e847Slclee return (i); 24351237e847Slclee } 24361237e847Slclee return (-1); 24371237e847Slclee } else { 24381237e847Slclee 24391237e847Slclee /* Specifying size in cylinders */ 24401237e847Slclee (void) printf(E_LINE); 2441bb16350dSlclee (void) printf(Q_LINE); 2442bb16350dSlclee (void) printf("Enter starting cylinder number: "); 24437c478bd9Sstevel@tonic-gate if ((cyl = getcyl()) == -1) { 2444bb16350dSlclee (void) printf(E_LINE); 2445bb16350dSlclee (void) printf("Invalid number; retry the operation."); 24467c478bd9Sstevel@tonic-gate return (-1); 24477c478bd9Sstevel@tonic-gate } 24487c478bd9Sstevel@tonic-gate if (cyl == 0) { 2449bb16350dSlclee (void) printf(E_LINE); 24501237e847Slclee (void) printf( 24511237e847Slclee "New partition cannot start at cylinder 0.\n"); 24527c478bd9Sstevel@tonic-gate return (-1); 24537c478bd9Sstevel@tonic-gate } 2454342440ecSPrasad Singamsetty 2455342440ecSPrasad Singamsetty 2456342440ecSPrasad Singamsetty if (cyl >= Numcyl_usable) { 2457bb16350dSlclee (void) printf(E_LINE); 2458bb16350dSlclee (void) printf( 24591237e847Slclee "Cylinder %d is out of bounds, " 24601237e847Slclee "the maximum is %d.\n", 2461342440ecSPrasad Singamsetty cyl, Numcyl_usable - 1); 24627c478bd9Sstevel@tonic-gate return (-1); 24637c478bd9Sstevel@tonic-gate } 2464342440ecSPrasad Singamsetty 2465bb16350dSlclee (void) printf(Q_LINE); 2466bb16350dSlclee (void) printf("Enter partition size in cylinders: "); 24677c478bd9Sstevel@tonic-gate if ((cylen = getcyl()) == -1) { 2468bb16350dSlclee (void) printf(E_LINE); 2469bb16350dSlclee (void) printf("Invalid number, retry the operation."); 24707c478bd9Sstevel@tonic-gate return (-1); 24717c478bd9Sstevel@tonic-gate } 24727c478bd9Sstevel@tonic-gate 24731237e847Slclee for (i = 0; i < FD_NUMPART; i++) { 24741237e847Slclee uint32_t t_relsect, t_numsect; 24751237e847Slclee 24761237e847Slclee if (partition[i]->systid == UNUSED) 24771237e847Slclee break; 2478f85c7842SSuhasini Peddada t_relsect = lel(partition[i]->relsect); 2479f85c7842SSuhasini Peddada t_numsect = lel(partition[i]->numsect); 24801237e847Slclee 24811237e847Slclee if (cyl * cyl_size >= t_relsect && 24821237e847Slclee cyl * cyl_size < t_relsect + t_numsect) { 2483bb16350dSlclee (void) printf(E_LINE); 2484bb16350dSlclee (void) printf( 24851237e847Slclee "Cylinder %d is already allocated" 24867c478bd9Sstevel@tonic-gate "\nretry the operation.", 24871237e847Slclee cyl); 24887c478bd9Sstevel@tonic-gate return (-1); 24897c478bd9Sstevel@tonic-gate } 24907c478bd9Sstevel@tonic-gate 24911237e847Slclee if (cyl * cyl_size < t_relsect && 24921237e847Slclee (cyl + cylen - 1) * cyl_size > t_relsect) { 24931237e847Slclee (void) printf(E_LINE); 24941237e847Slclee (void) printf( 24951237e847Slclee "Maximum size for partition is %u cylinders" 24961237e847Slclee "\nretry the operation.", 24971237e847Slclee (t_relsect - cyl * cyl_size) / cyl_size); 24981237e847Slclee return (-1); 24991237e847Slclee } 25001237e847Slclee } 25011237e847Slclee 2502342440ecSPrasad Singamsetty /* Verify partition doesn't exceed disk size or 2 TB */ 2503342440ecSPrasad Singamsetty if (cyl + cylen > Numcyl_usable) { 25041237e847Slclee (void) printf(E_LINE); 2505342440ecSPrasad Singamsetty if (Numcyl > Numcyl_usable) { 25061237e847Slclee (void) printf( 2507342440ecSPrasad Singamsetty "Maximum size for partition is %d " 2508342440ecSPrasad Singamsetty "cylinders; \nretry the operation.", 2509342440ecSPrasad Singamsetty Numcyl_usable - cyl); 2510342440ecSPrasad Singamsetty } else { 2511342440ecSPrasad Singamsetty (void) printf( 2512342440ecSPrasad Singamsetty "Maximum size for partition is %d " 2513342440ecSPrasad Singamsetty "cylinders; \nretry the operation.", 2514342440ecSPrasad Singamsetty Numcyl_usable - cyl); 2515342440ecSPrasad Singamsetty } 25161237e847Slclee return (-1); 25171237e847Slclee } 25181237e847Slclee 25191237e847Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 25201237e847Slclee if ((tsystid == DOSOS12) && 25211237e847Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 25221237e847Slclee (void) printf(E_LINE); 25231237e847Slclee (void) printf( 25241237e847Slclee "Maximum size for a %s partition is %ld cylinders;" 25251237e847Slclee "\nretry the operation.", 25261237e847Slclee Dstr, MAXDOS / (int)(cyl_size)); 25271237e847Slclee return (-1); 25281237e847Slclee } 25291237e847Slclee 25301237e847Slclee (void) printf(E_LINE); 25311237e847Slclee i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 25321237e847Slclee cyl * cyl_size, cylen * cyl_size); 25331237e847Slclee if (i < 0) 25341237e847Slclee return (-1); 25357c478bd9Sstevel@tonic-gate 25367c478bd9Sstevel@tonic-gate if (verify_tbl() < 0) { 2537bb16350dSlclee (void) printf(E_LINE); 2538bb16350dSlclee (void) printf("fdisk: Cannot create partition table\n"); 25397c478bd9Sstevel@tonic-gate return (-1); 25407c478bd9Sstevel@tonic-gate } 25417c478bd9Sstevel@tonic-gate 25427c478bd9Sstevel@tonic-gate return (i); 25437c478bd9Sstevel@tonic-gate } 25447c478bd9Sstevel@tonic-gate } 25457c478bd9Sstevel@tonic-gate 25467c478bd9Sstevel@tonic-gate /* 25477c478bd9Sstevel@tonic-gate * dispmenu 25487c478bd9Sstevel@tonic-gate * Display command menu (interactive mode). 25497c478bd9Sstevel@tonic-gate */ 2550bb16350dSlclee static void 2551bb16350dSlclee dispmenu(void) 25527c478bd9Sstevel@tonic-gate { 2553bb16350dSlclee (void) printf(M_LINE); 2554bb16350dSlclee (void) printf( 2555bb16350dSlclee "SELECT ONE OF THE FOLLOWING:\n" 2556bb16350dSlclee " 1. Create a partition\n" 2557bb16350dSlclee " 2. Specify the active partition\n" 2558bb16350dSlclee " 3. Delete a partition\n" 2559bb16350dSlclee " 4. Change between Solaris and Solaris2 Partition IDs\n" 2560bb16350dSlclee " 5. Exit (update disk configuration and exit)\n" 2561bb16350dSlclee " 6. Cancel (exit without updating disk configuration)\n"); 25627c478bd9Sstevel@tonic-gate } 25637c478bd9Sstevel@tonic-gate 25647c478bd9Sstevel@tonic-gate /* 25657c478bd9Sstevel@tonic-gate * pchange 25667c478bd9Sstevel@tonic-gate * Change the ACTIVE designation of a partition. 25677c478bd9Sstevel@tonic-gate */ 2568bb16350dSlclee static int 2569bb16350dSlclee pchange(void) 25707c478bd9Sstevel@tonic-gate { 25717c478bd9Sstevel@tonic-gate char s[80]; 25727c478bd9Sstevel@tonic-gate int i, j; 25737c478bd9Sstevel@tonic-gate 2574bb16350dSlclee for (;;) { 2575bb16350dSlclee (void) printf(Q_LINE); 25767c478bd9Sstevel@tonic-gate { 2577bb16350dSlclee (void) printf( 2578bb16350dSlclee "Specify the partition number to boot from" 25797c478bd9Sstevel@tonic-gate " (or specify 0 for none): "); 25807c478bd9Sstevel@tonic-gate } 2581933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 25827c478bd9Sstevel@tonic-gate rm_blanks(s); 2583933c1499SBarry Harding if (((s[1] != '\0') && (s[1] != '\n')) || 2584933c1499SBarry Harding (s[0] < '0') || (s[0] > '4')) { 2585bb16350dSlclee (void) printf(E_LINE); 2586bb16350dSlclee (void) printf( 2587bb16350dSlclee "Invalid response, please specify a number" 25887c478bd9Sstevel@tonic-gate " between 0 and 4.\n"); 25897c478bd9Sstevel@tonic-gate } else { 25907c478bd9Sstevel@tonic-gate break; 25917c478bd9Sstevel@tonic-gate } 25927c478bd9Sstevel@tonic-gate } 25937c478bd9Sstevel@tonic-gate if (s[0] == '0') { /* No active partitions */ 25947c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 25957c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED && 25967c478bd9Sstevel@tonic-gate Table[i].bootid == ACTIVE) 25977c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 25987c478bd9Sstevel@tonic-gate } 2599bb16350dSlclee (void) printf(E_LINE); 2600bb16350dSlclee (void) printf( 2601bb16350dSlclee "No partition is currently marked as active."); 26027c478bd9Sstevel@tonic-gate return (0); 26037c478bd9Sstevel@tonic-gate } else { /* User has selected a partition to be active */ 26047c478bd9Sstevel@tonic-gate i = s[0] - '1'; 26057c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2606bb16350dSlclee (void) printf(E_LINE); 2607bb16350dSlclee (void) printf("Partition does not exist."); 26087c478bd9Sstevel@tonic-gate return (-1); 26097c478bd9Sstevel@tonic-gate } 26107c478bd9Sstevel@tonic-gate /* a DOS-DATA or EXT-DOS partition cannot be active */ 26117c478bd9Sstevel@tonic-gate else if ((Table[i].systid == DOSDATA) || 26127c478bd9Sstevel@tonic-gate (Table[i].systid == EXTDOS) || 26137c478bd9Sstevel@tonic-gate (Table[i].systid == FDISK_EXTLBA)) { 2614bb16350dSlclee (void) printf(E_LINE); 2615bb16350dSlclee (void) printf( 2616bb16350dSlclee "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions " 26177c478bd9Sstevel@tonic-gate "cannot be made active.\n"); 2618bb16350dSlclee (void) printf("Select another partition."); 26197c478bd9Sstevel@tonic-gate return (-1); 26207c478bd9Sstevel@tonic-gate } 26217c478bd9Sstevel@tonic-gate Table[i].bootid = ACTIVE; 26227c478bd9Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 26237c478bd9Sstevel@tonic-gate if (j != i) 26247c478bd9Sstevel@tonic-gate Table[j].bootid = 0; 26257c478bd9Sstevel@tonic-gate } 26267c478bd9Sstevel@tonic-gate } 2627bb16350dSlclee (void) printf(E_LINE); 26287c478bd9Sstevel@tonic-gate { 2629bb16350dSlclee (void) printf( 2630bb16350dSlclee "Partition %d is now active. The system will start up" 26317c478bd9Sstevel@tonic-gate " from this\n", i + 1); 2632bb16350dSlclee (void) printf("partition after the next reboot."); 26337c478bd9Sstevel@tonic-gate } 26347c478bd9Sstevel@tonic-gate return (1); 26357c478bd9Sstevel@tonic-gate } 26367c478bd9Sstevel@tonic-gate 26377c478bd9Sstevel@tonic-gate /* 26387c478bd9Sstevel@tonic-gate * Change between SOLARIS and SOLARIS2 partition id 26397c478bd9Sstevel@tonic-gate */ 2640bb16350dSlclee static int 2641bb16350dSlclee ppartid(void) 26427c478bd9Sstevel@tonic-gate { 26437c478bd9Sstevel@tonic-gate char *p, s[80]; 26447c478bd9Sstevel@tonic-gate int i; 26457c478bd9Sstevel@tonic-gate 26467c478bd9Sstevel@tonic-gate for (;;) { 2647bb16350dSlclee (void) printf(Q_LINE); 2648bb16350dSlclee (void) printf("Specify the partition number to change" 26497c478bd9Sstevel@tonic-gate " (or enter 0 to exit): "); 2650bb16350dSlclee if (!fgets(s, sizeof (s), stdin)) 2651bb16350dSlclee return (1); 26527c478bd9Sstevel@tonic-gate i = strtol(s, &p, 10); 26537c478bd9Sstevel@tonic-gate 26547c478bd9Sstevel@tonic-gate if (*p != '\n' || i < 0 || i > FD_NUMPART) { 2655bb16350dSlclee (void) printf(E_LINE); 2656bb16350dSlclee (void) printf( 2657bb16350dSlclee "Invalid response, retry the operation.\n"); 26587c478bd9Sstevel@tonic-gate continue; 26597c478bd9Sstevel@tonic-gate } 26607c478bd9Sstevel@tonic-gate 26617c478bd9Sstevel@tonic-gate if (i == 0) { 26627c478bd9Sstevel@tonic-gate /* exit delete command */ 2663bb16350dSlclee (void) printf(E_LINE); /* clear error message */ 26647c478bd9Sstevel@tonic-gate return (1); 26657c478bd9Sstevel@tonic-gate } 26667c478bd9Sstevel@tonic-gate 26677c478bd9Sstevel@tonic-gate i -= 1; 26687c478bd9Sstevel@tonic-gate if (Table[i].systid == SUNIXOS) { 26697c478bd9Sstevel@tonic-gate Table[i].systid = SUNIXOS2; 26707c478bd9Sstevel@tonic-gate } else if (Table[i].systid == SUNIXOS2) { 26717c478bd9Sstevel@tonic-gate Table[i].systid = SUNIXOS; 26727c478bd9Sstevel@tonic-gate } else { 2673bb16350dSlclee (void) printf(E_LINE); 2674bb16350dSlclee (void) printf( 2675bb16350dSlclee "Partition %d is not a Solaris partition.", 26767c478bd9Sstevel@tonic-gate i + 1); 26777c478bd9Sstevel@tonic-gate continue; 26787c478bd9Sstevel@tonic-gate } 26797c478bd9Sstevel@tonic-gate 2680bb16350dSlclee (void) printf(E_LINE); 2681bb16350dSlclee (void) printf("Partition %d has been changed.", i + 1); 26827c478bd9Sstevel@tonic-gate return (1); 26837c478bd9Sstevel@tonic-gate } 26847c478bd9Sstevel@tonic-gate } 26857c478bd9Sstevel@tonic-gate 26867c478bd9Sstevel@tonic-gate /* 26877c478bd9Sstevel@tonic-gate * pdelete 26887c478bd9Sstevel@tonic-gate * Remove partition entry from the table (interactive mode). 26897c478bd9Sstevel@tonic-gate */ 2690bb16350dSlclee static char 2691bb16350dSlclee pdelete(void) 26927c478bd9Sstevel@tonic-gate { 26937c478bd9Sstevel@tonic-gate char s[80]; 26947c478bd9Sstevel@tonic-gate int i, j; 26957c478bd9Sstevel@tonic-gate char pactive; 26967c478bd9Sstevel@tonic-gate 2697bb16350dSlclee DEL1: (void) printf(Q_LINE); 2698bb16350dSlclee (void) printf("Specify the partition number to delete" 26997c478bd9Sstevel@tonic-gate " (or enter 0 to exit): "); 2700933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 27017c478bd9Sstevel@tonic-gate rm_blanks(s); 27027c478bd9Sstevel@tonic-gate if ((s[0] == '0')) { /* exit delete command */ 2703bb16350dSlclee (void) printf(E_LINE); /* clear error message */ 27047c478bd9Sstevel@tonic-gate return (1); 27057c478bd9Sstevel@tonic-gate } 27067c478bd9Sstevel@tonic-gate /* Accept only a single digit between 1 and 4 */ 2707933c1499SBarry Harding if (((s[1] != '\0') && (s[1] != '\n')) || 2708933c1499SBarry Harding (i = atoi(s)) < 1 || i > FD_NUMPART) { 2709bb16350dSlclee (void) printf(E_LINE); 2710bb16350dSlclee (void) printf("Invalid response, retry the operation.\n"); 27117c478bd9Sstevel@tonic-gate goto DEL1; 27127c478bd9Sstevel@tonic-gate } else { /* Found a digit between 1 and 4 */ 27137c478bd9Sstevel@tonic-gate --i; /* Structure begins with element 0 */ 27147c478bd9Sstevel@tonic-gate } 27157c478bd9Sstevel@tonic-gate 27167c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2717bb16350dSlclee (void) printf(E_LINE); 2718bb16350dSlclee (void) printf("Partition %d does not exist.", i + 1); 27197c478bd9Sstevel@tonic-gate return (-1); 27207c478bd9Sstevel@tonic-gate } 27217c478bd9Sstevel@tonic-gate 2722bb16350dSlclee (void) printf(Q_LINE); 2723bb16350dSlclee (void) printf("Are you sure you want to delete partition %d?" 27247c478bd9Sstevel@tonic-gate " This will make all files and \n", i + 1); 2725bb16350dSlclee (void) printf("programs in this partition inaccessible (type" 27267c478bd9Sstevel@tonic-gate " \"y\" or \"n\"). "); 27277c478bd9Sstevel@tonic-gate 2728bb16350dSlclee (void) printf(E_LINE); 27297c478bd9Sstevel@tonic-gate if (! yesno()) { 27307c478bd9Sstevel@tonic-gate return (1); 27317c478bd9Sstevel@tonic-gate } 27327c478bd9Sstevel@tonic-gate 27337c478bd9Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) { 27347c478bd9Sstevel@tonic-gate pactive = 1; 27357c478bd9Sstevel@tonic-gate } else { 27367c478bd9Sstevel@tonic-gate pactive = 0; 27377c478bd9Sstevel@tonic-gate } 27387c478bd9Sstevel@tonic-gate 27397c478bd9Sstevel@tonic-gate for (j = i; j < FD_NUMPART - 1; j++) { 27407c478bd9Sstevel@tonic-gate Table[j] = Table[j + 1]; 27417c478bd9Sstevel@tonic-gate } 27427c478bd9Sstevel@tonic-gate 27437c478bd9Sstevel@tonic-gate Table[j].systid = UNUSED; 27447c478bd9Sstevel@tonic-gate Table[j].numsect = 0; 27457c478bd9Sstevel@tonic-gate Table[j].relsect = 0; 27467c478bd9Sstevel@tonic-gate Table[j].bootid = 0; 2747bb16350dSlclee (void) printf(E_LINE); 2748bb16350dSlclee (void) printf("Partition %d has been deleted.", i + 1); 27497c478bd9Sstevel@tonic-gate 27507c478bd9Sstevel@tonic-gate if (pactive) { 2751bb16350dSlclee (void) printf(" This was the active partition."); 27527c478bd9Sstevel@tonic-gate } 27537c478bd9Sstevel@tonic-gate 27547c478bd9Sstevel@tonic-gate return (1); 27557c478bd9Sstevel@tonic-gate } 27567c478bd9Sstevel@tonic-gate 27577c478bd9Sstevel@tonic-gate /* 27587c478bd9Sstevel@tonic-gate * rm_blanks 27597c478bd9Sstevel@tonic-gate * Remove blanks from strings of user responses. 27607c478bd9Sstevel@tonic-gate */ 2761bb16350dSlclee static void 2762bb16350dSlclee rm_blanks(char *s) 27637c478bd9Sstevel@tonic-gate { 27647c478bd9Sstevel@tonic-gate register int i, j; 27657c478bd9Sstevel@tonic-gate 27667c478bd9Sstevel@tonic-gate for (i = 0; i < CBUFLEN; i++) { 27677c478bd9Sstevel@tonic-gate if ((s[i] == ' ') || (s[i] == '\t')) 27687c478bd9Sstevel@tonic-gate continue; 27697c478bd9Sstevel@tonic-gate else 27707c478bd9Sstevel@tonic-gate /* Found first non-blank character of the string */ 27717c478bd9Sstevel@tonic-gate break; 27727c478bd9Sstevel@tonic-gate } 27737c478bd9Sstevel@tonic-gate for (j = 0; i < CBUFLEN; j++, i++) { 27747c478bd9Sstevel@tonic-gate if ((s[j] = s[i]) == '\0') { 27757c478bd9Sstevel@tonic-gate /* Reached end of string */ 27767c478bd9Sstevel@tonic-gate return; 27777c478bd9Sstevel@tonic-gate } 27787c478bd9Sstevel@tonic-gate } 27797c478bd9Sstevel@tonic-gate } 27807c478bd9Sstevel@tonic-gate 27817c478bd9Sstevel@tonic-gate /* 27827c478bd9Sstevel@tonic-gate * getcyl 27837c478bd9Sstevel@tonic-gate * Take the user-specified cylinder number and convert it from a 27847c478bd9Sstevel@tonic-gate * string to a decimal value. 27857c478bd9Sstevel@tonic-gate */ 2786bb16350dSlclee static int 2787bb16350dSlclee getcyl(void) 27887c478bd9Sstevel@tonic-gate { 27897c478bd9Sstevel@tonic-gate int slen, i, j; 27907c478bd9Sstevel@tonic-gate unsigned int cyl; 2791933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 27927c478bd9Sstevel@tonic-gate rm_blanks(s); 27937c478bd9Sstevel@tonic-gate slen = strlen(s); 2794933c1499SBarry Harding if (s[slen - 1] == '\n') 2795933c1499SBarry Harding slen--; 27967c478bd9Sstevel@tonic-gate j = 1; 27977c478bd9Sstevel@tonic-gate cyl = 0; 27987c478bd9Sstevel@tonic-gate for (i = slen - 1; i >= 0; i--) { 27997c478bd9Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 28007c478bd9Sstevel@tonic-gate return (-1); 28017c478bd9Sstevel@tonic-gate } 28027c478bd9Sstevel@tonic-gate cyl += (j * (s[i] - '0')); 28037c478bd9Sstevel@tonic-gate j *= 10; 28047c478bd9Sstevel@tonic-gate } 28057c478bd9Sstevel@tonic-gate return (cyl); 28067c478bd9Sstevel@tonic-gate } 28077c478bd9Sstevel@tonic-gate 28087c478bd9Sstevel@tonic-gate /* 28097c478bd9Sstevel@tonic-gate * disptbl 28107c478bd9Sstevel@tonic-gate * Display the current fdisk table; determine percentage 28117c478bd9Sstevel@tonic-gate * of the disk used for each partition. 28127c478bd9Sstevel@tonic-gate */ 2813bb16350dSlclee static void 2814bb16350dSlclee disptbl(void) 28157c478bd9Sstevel@tonic-gate { 28167c478bd9Sstevel@tonic-gate int i; 28177c478bd9Sstevel@tonic-gate unsigned int startcyl, endcyl, length, percent, remainder; 28187c478bd9Sstevel@tonic-gate char *stat, *type; 2819342440ecSPrasad Singamsetty int is_pmbr = 0; 28207c478bd9Sstevel@tonic-gate 28217c478bd9Sstevel@tonic-gate if ((heads == 0) || (sectors == 0)) { 2822bb16350dSlclee (void) printf("WARNING: critical disk geometry information" 28237c478bd9Sstevel@tonic-gate " missing!\n"); 2824bb16350dSlclee (void) printf("\theads = %d, sectors = %d\n", heads, sectors); 28257c478bd9Sstevel@tonic-gate exit(1); 28267c478bd9Sstevel@tonic-gate } 28277c478bd9Sstevel@tonic-gate 2828bb16350dSlclee (void) printf(HOME); 2829bb16350dSlclee (void) printf(T_LINE); 2830bb16350dSlclee (void) printf(" Total disk size is %d cylinders\n", Numcyl); 2831*65908c77Syu, larry liu - Sun Microsystems - Beijing China (void) printf(" Cylinder size is %d (%d byte) blocks\n\n", 2832*65908c77Syu, larry liu - Sun Microsystems - Beijing China heads * sectors, sectsiz); 2833bb16350dSlclee (void) printf( 2834bb16350dSlclee " Cylinders\n"); 2835bb16350dSlclee (void) printf( 2836bb16350dSlclee " Partition Status Type Start End Length" 28377c478bd9Sstevel@tonic-gate " %%\n"); 2838bb16350dSlclee (void) printf( 2839bb16350dSlclee " ========= ====== ============ ===== === ======" 28407c478bd9Sstevel@tonic-gate " ==="); 28417c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 28427c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2843bb16350dSlclee (void) printf("\n"); 2844bb16350dSlclee (void) printf(CLR_LIN); 28457c478bd9Sstevel@tonic-gate continue; 28467c478bd9Sstevel@tonic-gate } 28477c478bd9Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) 28487c478bd9Sstevel@tonic-gate stat = Actvstr; 28497c478bd9Sstevel@tonic-gate else 28507c478bd9Sstevel@tonic-gate stat = NAstr; 28517c478bd9Sstevel@tonic-gate switch (Table[i].systid) { 28527c478bd9Sstevel@tonic-gate case UNIXOS: 28537c478bd9Sstevel@tonic-gate type = Ustr; 28547c478bd9Sstevel@tonic-gate break; 28557c478bd9Sstevel@tonic-gate case SUNIXOS: 28567c478bd9Sstevel@tonic-gate type = SUstr; 28577c478bd9Sstevel@tonic-gate break; 28587c478bd9Sstevel@tonic-gate case SUNIXOS2: 28597c478bd9Sstevel@tonic-gate type = SU2str; 28607c478bd9Sstevel@tonic-gate break; 28617c478bd9Sstevel@tonic-gate case X86BOOT: 28627c478bd9Sstevel@tonic-gate type = X86str; 28637c478bd9Sstevel@tonic-gate break; 28647c478bd9Sstevel@tonic-gate case DOSOS12: 28657c478bd9Sstevel@tonic-gate type = Dstr; 28667c478bd9Sstevel@tonic-gate break; 28677c478bd9Sstevel@tonic-gate case DOSOS16: 28687c478bd9Sstevel@tonic-gate type = D16str; 28697c478bd9Sstevel@tonic-gate break; 28707c478bd9Sstevel@tonic-gate case EXTDOS: 28717c478bd9Sstevel@tonic-gate type = EDstr; 28727c478bd9Sstevel@tonic-gate break; 28737c478bd9Sstevel@tonic-gate case DOSDATA: 28747c478bd9Sstevel@tonic-gate type = DDstr; 28757c478bd9Sstevel@tonic-gate break; 28767c478bd9Sstevel@tonic-gate case DOSHUGE: 28777c478bd9Sstevel@tonic-gate type = DBstr; 28787c478bd9Sstevel@tonic-gate break; 28797c478bd9Sstevel@tonic-gate case PCIXOS: 28807c478bd9Sstevel@tonic-gate type = PCstr; 28817c478bd9Sstevel@tonic-gate break; 28827c478bd9Sstevel@tonic-gate case DIAGPART: 28837c478bd9Sstevel@tonic-gate type = DIAGstr; 28847c478bd9Sstevel@tonic-gate break; 28857c478bd9Sstevel@tonic-gate case FDISK_IFS: 28867c478bd9Sstevel@tonic-gate type = IFSstr; 28877c478bd9Sstevel@tonic-gate break; 28887c478bd9Sstevel@tonic-gate case FDISK_AIXBOOT: 28897c478bd9Sstevel@tonic-gate type = AIXstr; 28907c478bd9Sstevel@tonic-gate break; 28917c478bd9Sstevel@tonic-gate case FDISK_AIXDATA: 28927c478bd9Sstevel@tonic-gate type = AIXDstr; 28937c478bd9Sstevel@tonic-gate break; 28947c478bd9Sstevel@tonic-gate case FDISK_OS2BOOT: 28957c478bd9Sstevel@tonic-gate type = OS2str; 28967c478bd9Sstevel@tonic-gate break; 28977c478bd9Sstevel@tonic-gate case FDISK_WINDOWS: 28987c478bd9Sstevel@tonic-gate type = WINstr; 28997c478bd9Sstevel@tonic-gate break; 29007c478bd9Sstevel@tonic-gate case FDISK_EXT_WIN: 29017c478bd9Sstevel@tonic-gate type = EWINstr; 29027c478bd9Sstevel@tonic-gate break; 29037c478bd9Sstevel@tonic-gate case FDISK_FAT95: 29047c478bd9Sstevel@tonic-gate type = FAT95str; 29057c478bd9Sstevel@tonic-gate break; 29067c478bd9Sstevel@tonic-gate case FDISK_EXTLBA: 29077c478bd9Sstevel@tonic-gate type = EXTLstr; 29087c478bd9Sstevel@tonic-gate break; 29097c478bd9Sstevel@tonic-gate case FDISK_LINUX: 29107c478bd9Sstevel@tonic-gate type = LINUXstr; 29117c478bd9Sstevel@tonic-gate break; 29127c478bd9Sstevel@tonic-gate case FDISK_CPM: 29137c478bd9Sstevel@tonic-gate type = CPMstr; 29147c478bd9Sstevel@tonic-gate break; 29157c478bd9Sstevel@tonic-gate case FDISK_NOVELL3: 29167c478bd9Sstevel@tonic-gate type = NOVstr; 29177c478bd9Sstevel@tonic-gate break; 29187c478bd9Sstevel@tonic-gate case FDISK_QNX4: 29197c478bd9Sstevel@tonic-gate type = QNXstr; 29207c478bd9Sstevel@tonic-gate break; 29217c478bd9Sstevel@tonic-gate case FDISK_QNX42: 29227c478bd9Sstevel@tonic-gate type = QNX2str; 29237c478bd9Sstevel@tonic-gate break; 29247c478bd9Sstevel@tonic-gate case FDISK_QNX43: 29257c478bd9Sstevel@tonic-gate type = QNX3str; 29267c478bd9Sstevel@tonic-gate break; 29277c478bd9Sstevel@tonic-gate case FDISK_LINUXNAT: 29287c478bd9Sstevel@tonic-gate type = LINNATstr; 29297c478bd9Sstevel@tonic-gate break; 29307c478bd9Sstevel@tonic-gate case FDISK_NTFSVOL1: 29317c478bd9Sstevel@tonic-gate type = NTFSVOL1str; 29327c478bd9Sstevel@tonic-gate break; 29337c478bd9Sstevel@tonic-gate case FDISK_NTFSVOL2: 29347c478bd9Sstevel@tonic-gate type = NTFSVOL2str; 29357c478bd9Sstevel@tonic-gate break; 29367c478bd9Sstevel@tonic-gate case FDISK_BSD: 29377c478bd9Sstevel@tonic-gate type = BSDstr; 29387c478bd9Sstevel@tonic-gate break; 29397c478bd9Sstevel@tonic-gate case FDISK_NEXTSTEP: 29407c478bd9Sstevel@tonic-gate type = NEXTSTEPstr; 29417c478bd9Sstevel@tonic-gate break; 29427c478bd9Sstevel@tonic-gate case FDISK_BSDIFS: 29437c478bd9Sstevel@tonic-gate type = BSDIFSstr; 29447c478bd9Sstevel@tonic-gate break; 29457c478bd9Sstevel@tonic-gate case FDISK_BSDISWAP: 29467c478bd9Sstevel@tonic-gate type = BSDISWAPstr; 29477c478bd9Sstevel@tonic-gate break; 29487c478bd9Sstevel@tonic-gate case EFI_PMBR: 29497c478bd9Sstevel@tonic-gate type = EFIstr; 2950f85c7842SSuhasini Peddada if (lel(Table[i].numsect) == DK_MAX_2TB) 2951342440ecSPrasad Singamsetty is_pmbr = 1; 2952342440ecSPrasad Singamsetty 29537c478bd9Sstevel@tonic-gate break; 29547c478bd9Sstevel@tonic-gate default: 29557c478bd9Sstevel@tonic-gate type = Ostr; 29567c478bd9Sstevel@tonic-gate break; 29577c478bd9Sstevel@tonic-gate } 2958f85c7842SSuhasini Peddada startcyl = lel(Table[i].relsect) / 29592b26c6eeSbharding (unsigned long)(heads * sectors); 2960342440ecSPrasad Singamsetty 2961f85c7842SSuhasini Peddada if (lel(Table[i].numsect) == DK_MAX_2TB) { 2962342440ecSPrasad Singamsetty endcyl = Numcyl - 1; 2963342440ecSPrasad Singamsetty length = endcyl - startcyl + 1; 2964342440ecSPrasad Singamsetty } else { 2965f85c7842SSuhasini Peddada length = lel(Table[i].numsect) / 29662b26c6eeSbharding (unsigned long)(heads * sectors); 2967f85c7842SSuhasini Peddada if (lel(Table[i].numsect) % 2968342440ecSPrasad Singamsetty (unsigned long)(heads * sectors)) 29697c478bd9Sstevel@tonic-gate length++; 29707c478bd9Sstevel@tonic-gate endcyl = startcyl + length - 1; 2971342440ecSPrasad Singamsetty } 2972342440ecSPrasad Singamsetty 2973342440ecSPrasad Singamsetty percent = length * 100 / Numcyl_usable; 2974342440ecSPrasad Singamsetty if ((remainder = (length * 100 % Numcyl_usable)) != 0) { 2975342440ecSPrasad Singamsetty if ((remainder * 100 / Numcyl_usable) > 50) { 29767c478bd9Sstevel@tonic-gate /* round up */ 29777c478bd9Sstevel@tonic-gate percent++; 29787c478bd9Sstevel@tonic-gate } 29797c478bd9Sstevel@tonic-gate /* Else leave the percent as is since it's already */ 29807c478bd9Sstevel@tonic-gate /* rounded down */ 29817c478bd9Sstevel@tonic-gate } 29827c478bd9Sstevel@tonic-gate if (percent > 100) 29837c478bd9Sstevel@tonic-gate percent = 100; 2984bb16350dSlclee (void) printf( 2985bb16350dSlclee "\n %d %s %-12.12s %4d %4d %4d" 2986bb16350dSlclee " %3d", 2987bb16350dSlclee i + 1, stat, type, startcyl, endcyl, length, percent); 29887c478bd9Sstevel@tonic-gate } 2989342440ecSPrasad Singamsetty 29907c478bd9Sstevel@tonic-gate /* Print warning message if table is empty */ 29917c478bd9Sstevel@tonic-gate if (Table[0].systid == UNUSED) { 2992bb16350dSlclee (void) printf(W_LINE); 2993bb16350dSlclee (void) printf("WARNING: no partitions are defined!"); 29947c478bd9Sstevel@tonic-gate } else { 29957c478bd9Sstevel@tonic-gate /* Clear the warning line */ 2996bb16350dSlclee (void) printf(W_LINE); 2997342440ecSPrasad Singamsetty 2998342440ecSPrasad Singamsetty /* Print warning if disk > 2TB and is not EFI PMBR */ 2999342440ecSPrasad Singamsetty if (!is_pmbr && (dev_capacity > DK_MAX_2TB)) 3000342440ecSPrasad Singamsetty (void) printf("WARNING: Disk is larger than 2 TB. " 3001342440ecSPrasad Singamsetty "Upper limit is 2 TB for non-EFI partition ID\n"); 30027c478bd9Sstevel@tonic-gate } 30037c478bd9Sstevel@tonic-gate } 30047c478bd9Sstevel@tonic-gate 30057c478bd9Sstevel@tonic-gate /* 30067c478bd9Sstevel@tonic-gate * print_Table 30077c478bd9Sstevel@tonic-gate * Write the detailed fdisk table to standard error for 30087c478bd9Sstevel@tonic-gate * the selected disk device. 30097c478bd9Sstevel@tonic-gate */ 3010bb16350dSlclee static void 3011bb16350dSlclee print_Table(void) 3012bb16350dSlclee { 30137c478bd9Sstevel@tonic-gate int i; 30147c478bd9Sstevel@tonic-gate 3015bb16350dSlclee (void) fprintf(stderr, 30167c478bd9Sstevel@tonic-gate " SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT" 30177c478bd9Sstevel@tonic-gate " NUMSECT\n"); 30187c478bd9Sstevel@tonic-gate 30197c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 3020bb16350dSlclee (void) fprintf(stderr, " %-5d ", Table[i].systid); 3021bb16350dSlclee (void) fprintf(stderr, "%-3d ", Table[i].bootid); 3022bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].beghead); 3023bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f); 30241237e847Slclee (void) fprintf(stderr, "%-8d ", 30251237e847Slclee (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl); 30267c478bd9Sstevel@tonic-gate 3027bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].endhead); 3028bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f); 30291237e847Slclee (void) fprintf(stderr, "%-8d ", 30301237e847Slclee (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl); 3031f85c7842SSuhasini Peddada (void) fprintf(stderr, "%-10u ", lel(Table[i].relsect)); 3032f85c7842SSuhasini Peddada (void) fprintf(stderr, "%-10u\n", lel(Table[i].numsect)); 30337c478bd9Sstevel@tonic-gate 30347c478bd9Sstevel@tonic-gate } 30357c478bd9Sstevel@tonic-gate } 30367c478bd9Sstevel@tonic-gate 30377c478bd9Sstevel@tonic-gate /* 30387c478bd9Sstevel@tonic-gate * copy_Table_to_Old_Table 30397c478bd9Sstevel@tonic-gate * Copy Table into Old_Table. The function only copies the systid, 30407c478bd9Sstevel@tonic-gate * numsect, relsect, and bootid values because they are the only 30417c478bd9Sstevel@tonic-gate * ones compared when determining if Table has changed. 30427c478bd9Sstevel@tonic-gate */ 3043bb16350dSlclee static void 3044bb16350dSlclee copy_Table_to_Old_Table(void) 30457c478bd9Sstevel@tonic-gate { 30467c478bd9Sstevel@tonic-gate int i; 30477c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 3048bb16350dSlclee (void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0])); 30497c478bd9Sstevel@tonic-gate } 30507c478bd9Sstevel@tonic-gate } 30517c478bd9Sstevel@tonic-gate 30527c478bd9Sstevel@tonic-gate /* 30537c478bd9Sstevel@tonic-gate * nulltbl 30547c478bd9Sstevel@tonic-gate * Zero out the systid, numsect, relsect, and bootid values in the 30557c478bd9Sstevel@tonic-gate * fdisk table. 30567c478bd9Sstevel@tonic-gate */ 3057bb16350dSlclee static void 3058bb16350dSlclee nulltbl(void) 30597c478bd9Sstevel@tonic-gate { 30607c478bd9Sstevel@tonic-gate int i; 30617c478bd9Sstevel@tonic-gate 30627c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 30637c478bd9Sstevel@tonic-gate Table[i].systid = UNUSED; 3064f85c7842SSuhasini Peddada Table[i].numsect = lel(UNUSED); 3065f85c7842SSuhasini Peddada Table[i].relsect = lel(UNUSED); 30667c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 30672f8ec719SBarry Harding skip_verify[i] = 0; 30687c478bd9Sstevel@tonic-gate } 30697c478bd9Sstevel@tonic-gate } 30707c478bd9Sstevel@tonic-gate 30717c478bd9Sstevel@tonic-gate /* 30727c478bd9Sstevel@tonic-gate * copy_Bootblk_to_Table 30737c478bd9Sstevel@tonic-gate * Copy the bytes from the boot record to an internal "Table". 30747c478bd9Sstevel@tonic-gate * All unused are padded with zeros starting at offset 446. 30757c478bd9Sstevel@tonic-gate */ 3076bb16350dSlclee static void 3077bb16350dSlclee copy_Bootblk_to_Table(void) 30787c478bd9Sstevel@tonic-gate { 30797c478bd9Sstevel@tonic-gate int i, j; 30807c478bd9Sstevel@tonic-gate char *bootptr; 30817c478bd9Sstevel@tonic-gate struct ipart iparts[FD_NUMPART]; 30827c478bd9Sstevel@tonic-gate 30837c478bd9Sstevel@tonic-gate /* Get an aligned copy of the partition tables */ 3084bb16350dSlclee (void) memcpy(iparts, Bootblk->parts, sizeof (iparts)); 30857c478bd9Sstevel@tonic-gate bootptr = (char *)iparts; /* Points to start of partition table */ 3086f85c7842SSuhasini Peddada if (les(Bootblk->signature) != MBB_MAGIC) { 30877c478bd9Sstevel@tonic-gate /* Signature is missing */ 30887c478bd9Sstevel@tonic-gate nulltbl(); 3089bb16350dSlclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 30907c478bd9Sstevel@tonic-gate return; 30917c478bd9Sstevel@tonic-gate } 30927c478bd9Sstevel@tonic-gate /* 30937c478bd9Sstevel@tonic-gate * When the DOS fdisk command deletes a partition, it is not 30947c478bd9Sstevel@tonic-gate * recognized by the old algorithm. The algorithm that 30957c478bd9Sstevel@tonic-gate * follows looks at each entry in the Bootrec and copies all 30967c478bd9Sstevel@tonic-gate * those that are valid. 30977c478bd9Sstevel@tonic-gate */ 30987c478bd9Sstevel@tonic-gate j = 0; 30997c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 31007c478bd9Sstevel@tonic-gate if (iparts[i].systid == 0) { 31017c478bd9Sstevel@tonic-gate /* Null entry */ 31027c478bd9Sstevel@tonic-gate bootptr += sizeof (struct ipart); 31037c478bd9Sstevel@tonic-gate } else { 3104bb16350dSlclee fill_ipart(bootptr, &Table[j]); 31057c478bd9Sstevel@tonic-gate j++; 31067c478bd9Sstevel@tonic-gate bootptr += sizeof (struct ipart); 31077c478bd9Sstevel@tonic-gate } 31087c478bd9Sstevel@tonic-gate } 31097c478bd9Sstevel@tonic-gate for (i = j; i < FD_NUMPART; i++) { 31107c478bd9Sstevel@tonic-gate Table[i].systid = UNUSED; 3111f85c7842SSuhasini Peddada Table[i].numsect = lel(UNUSED); 3112f85c7842SSuhasini Peddada Table[i].relsect = lel(UNUSED); 31137c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 31147c478bd9Sstevel@tonic-gate 31157c478bd9Sstevel@tonic-gate } 31167c478bd9Sstevel@tonic-gate /* For now, always replace the bootcode with ours */ 3117bb16350dSlclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 31187c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 31197c478bd9Sstevel@tonic-gate } 31207c478bd9Sstevel@tonic-gate 31217c478bd9Sstevel@tonic-gate /* 31227c478bd9Sstevel@tonic-gate * fill_ipart 31237c478bd9Sstevel@tonic-gate * Initialize ipart structure values. 31247c478bd9Sstevel@tonic-gate */ 3125bb16350dSlclee static void 31267c478bd9Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp) 31277c478bd9Sstevel@tonic-gate { 31287c478bd9Sstevel@tonic-gate #ifdef sparc 31297c478bd9Sstevel@tonic-gate /* Packing struct ipart for Sparc */ 31307c478bd9Sstevel@tonic-gate partp->bootid = getbyte(&bootptr); 31317c478bd9Sstevel@tonic-gate partp->beghead = getbyte(&bootptr); 31327c478bd9Sstevel@tonic-gate partp->begsect = getbyte(&bootptr); 31337c478bd9Sstevel@tonic-gate partp->begcyl = getbyte(&bootptr); 31347c478bd9Sstevel@tonic-gate partp->systid = getbyte(&bootptr); 31357c478bd9Sstevel@tonic-gate partp->endhead = getbyte(&bootptr); 31367c478bd9Sstevel@tonic-gate partp->endsect = getbyte(&bootptr); 31377c478bd9Sstevel@tonic-gate partp->endcyl = getbyte(&bootptr); 3138bb16350dSlclee partp->relsect = (int32_t)getlong(&bootptr); 3139bb16350dSlclee partp->numsect = (int32_t)getlong(&bootptr); 31407c478bd9Sstevel@tonic-gate #else 31417c478bd9Sstevel@tonic-gate *partp = *(struct ipart *)bootptr; 31427c478bd9Sstevel@tonic-gate #endif 31437c478bd9Sstevel@tonic-gate } 31447c478bd9Sstevel@tonic-gate 31457c478bd9Sstevel@tonic-gate /* 3146bb16350dSlclee * getbyte, getlong 31477c478bd9Sstevel@tonic-gate * Get a byte, a short, or a long (SPARC only). 31487c478bd9Sstevel@tonic-gate */ 31497c478bd9Sstevel@tonic-gate #ifdef sparc 3150bb16350dSlclee uchar_t 3151bb16350dSlclee getbyte(char **bp) 31527c478bd9Sstevel@tonic-gate { 3153bb16350dSlclee uchar_t b; 31547c478bd9Sstevel@tonic-gate 3155bb16350dSlclee b = (uchar_t)**bp; 31567c478bd9Sstevel@tonic-gate *bp = *bp + 1; 31577c478bd9Sstevel@tonic-gate return (b); 31587c478bd9Sstevel@tonic-gate } 31597c478bd9Sstevel@tonic-gate 3160bb16350dSlclee uint32_t 3161bb16350dSlclee getlong(char **bp) 31627c478bd9Sstevel@tonic-gate { 3163bb16350dSlclee int32_t b, bh, bl; 31647c478bd9Sstevel@tonic-gate 31657c478bd9Sstevel@tonic-gate bh = ((**bp) << 8) | *(*bp + 1); 31667c478bd9Sstevel@tonic-gate *bp += 2; 31677c478bd9Sstevel@tonic-gate bl = ((**bp) << 8) | *(*bp + 1); 31687c478bd9Sstevel@tonic-gate *bp += 2; 31697c478bd9Sstevel@tonic-gate 31707c478bd9Sstevel@tonic-gate b = (bh << 16) | bl; 3171bb16350dSlclee return ((uint32_t)b); 31727c478bd9Sstevel@tonic-gate } 31737c478bd9Sstevel@tonic-gate #endif 31747c478bd9Sstevel@tonic-gate 31757c478bd9Sstevel@tonic-gate /* 31767c478bd9Sstevel@tonic-gate * copy_Table_to_Bootblk 3177*65908c77Syu, larry liu - Sun Microsystems - Beijing China * Copy the table into the boot record. Note that the unused 31787c478bd9Sstevel@tonic-gate * entries will always be the last ones in the table and they are 31797c478bd9Sstevel@tonic-gate * marked with 100 in sysind. The the unused portion of the table 31807c478bd9Sstevel@tonic-gate * is padded with zeros in the bytes after the used entries. 31817c478bd9Sstevel@tonic-gate */ 3182bb16350dSlclee static void 3183bb16350dSlclee copy_Table_to_Bootblk(void) 31847c478bd9Sstevel@tonic-gate { 31857c478bd9Sstevel@tonic-gate struct ipart *boot_ptr, *tbl_ptr; 31867c478bd9Sstevel@tonic-gate 31877c478bd9Sstevel@tonic-gate boot_ptr = (struct ipart *)Bootblk->parts; 31887c478bd9Sstevel@tonic-gate tbl_ptr = (struct ipart *)&Table[0].bootid; 31897c478bd9Sstevel@tonic-gate for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid; 31907c478bd9Sstevel@tonic-gate tbl_ptr++, boot_ptr++) { 31917c478bd9Sstevel@tonic-gate if (tbl_ptr->systid == UNUSED) 3192bb16350dSlclee (void) memset(boot_ptr, 0, sizeof (struct ipart)); 31937c478bd9Sstevel@tonic-gate else 3194bb16350dSlclee (void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart)); 31957c478bd9Sstevel@tonic-gate } 3196f85c7842SSuhasini Peddada Bootblk->signature = les(MBB_MAGIC); 31977c478bd9Sstevel@tonic-gate } 31987c478bd9Sstevel@tonic-gate 31997c478bd9Sstevel@tonic-gate /* 32007c478bd9Sstevel@tonic-gate * TableChanged 32017c478bd9Sstevel@tonic-gate * Check for any changes in the partition table. 32027c478bd9Sstevel@tonic-gate */ 3203bb16350dSlclee static int 3204bb16350dSlclee TableChanged(void) 32057c478bd9Sstevel@tonic-gate { 32067c478bd9Sstevel@tonic-gate int i, changed; 32077c478bd9Sstevel@tonic-gate 32087c478bd9Sstevel@tonic-gate changed = 0; 32097c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 32107c478bd9Sstevel@tonic-gate if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) { 32117c478bd9Sstevel@tonic-gate /* Partition table changed, write back to disk */ 32127c478bd9Sstevel@tonic-gate changed = 1; 32137c478bd9Sstevel@tonic-gate } 32147c478bd9Sstevel@tonic-gate } 32157c478bd9Sstevel@tonic-gate 32167c478bd9Sstevel@tonic-gate return (changed); 32177c478bd9Sstevel@tonic-gate } 32187c478bd9Sstevel@tonic-gate 32197c478bd9Sstevel@tonic-gate /* 32207c478bd9Sstevel@tonic-gate * ffile_write 32217c478bd9Sstevel@tonic-gate * Display contents of partition table to standard output or 32227c478bd9Sstevel@tonic-gate * another file name without writing it to the disk (-W file). 32237c478bd9Sstevel@tonic-gate */ 3224bb16350dSlclee static void 3225bb16350dSlclee ffile_write(char *file) 32267c478bd9Sstevel@tonic-gate { 32277c478bd9Sstevel@tonic-gate register int i; 32287c478bd9Sstevel@tonic-gate FILE *fp; 32297c478bd9Sstevel@tonic-gate 32307c478bd9Sstevel@tonic-gate /* 32317c478bd9Sstevel@tonic-gate * If file isn't standard output, then it's a file name. 32327c478bd9Sstevel@tonic-gate * Open file and write it. 32337c478bd9Sstevel@tonic-gate */ 32347c478bd9Sstevel@tonic-gate if (file != (char *)stdout) { 32357c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "w")) == NULL) { 32361237e847Slclee (void) fprintf(stderr, 32371237e847Slclee "fdisk: Cannot open output file %s.\n", 32387c478bd9Sstevel@tonic-gate file); 32397c478bd9Sstevel@tonic-gate exit(1); 32407c478bd9Sstevel@tonic-gate } 32417c478bd9Sstevel@tonic-gate } 32427c478bd9Sstevel@tonic-gate else 32437c478bd9Sstevel@tonic-gate fp = stdout; 32447c478bd9Sstevel@tonic-gate 32457c478bd9Sstevel@tonic-gate /* 32467c478bd9Sstevel@tonic-gate * Write the fdisk table information 32477c478bd9Sstevel@tonic-gate */ 3248bb16350dSlclee (void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev); 3249bb16350dSlclee (void) fprintf(fp, "* Dimensions:\n"); 3250bb16350dSlclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 3251bb16350dSlclee (void) fprintf(fp, "* %4d sectors/track\n", sectors); 3252bb16350dSlclee (void) fprintf(fp, "* %4d tracks/cylinder\n", heads); 3253bb16350dSlclee (void) fprintf(fp, "* %4d cylinders\n", Numcyl); 3254bb16350dSlclee (void) fprintf(fp, "*\n"); 32557c478bd9Sstevel@tonic-gate /* Write virtual (HBA) geometry, if required */ 32567c478bd9Sstevel@tonic-gate if (v_flag) { 3257bb16350dSlclee (void) fprintf(fp, "* HBA Dimensions:\n"); 3258bb16350dSlclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 3259bb16350dSlclee (void) fprintf(fp, "* %4d sectors/track\n", hba_sectors); 3260bb16350dSlclee (void) fprintf(fp, "* %4d tracks/cylinder\n", hba_heads); 3261bb16350dSlclee (void) fprintf(fp, "* %4d cylinders\n", hba_Numcyl); 3262bb16350dSlclee (void) fprintf(fp, "*\n"); 32637c478bd9Sstevel@tonic-gate } 3264bb16350dSlclee (void) fprintf(fp, "* systid:\n"); 3265bb16350dSlclee (void) fprintf(fp, "* 1: DOSOS12\n"); 3266bb16350dSlclee (void) fprintf(fp, "* 2: PCIXOS\n"); 3267bb16350dSlclee (void) fprintf(fp, "* 4: DOSOS16\n"); 3268bb16350dSlclee (void) fprintf(fp, "* 5: EXTDOS\n"); 3269bb16350dSlclee (void) fprintf(fp, "* 6: DOSBIG\n"); 3270bb16350dSlclee (void) fprintf(fp, "* 7: FDISK_IFS\n"); 3271bb16350dSlclee (void) fprintf(fp, "* 8: FDISK_AIXBOOT\n"); 3272bb16350dSlclee (void) fprintf(fp, "* 9: FDISK_AIXDATA\n"); 3273bb16350dSlclee (void) fprintf(fp, "* 10: FDISK_0S2BOOT\n"); 3274bb16350dSlclee (void) fprintf(fp, "* 11: FDISK_WINDOWS\n"); 3275bb16350dSlclee (void) fprintf(fp, "* 12: FDISK_EXT_WIN\n"); 3276bb16350dSlclee (void) fprintf(fp, "* 14: FDISK_FAT95\n"); 3277bb16350dSlclee (void) fprintf(fp, "* 15: FDISK_EXTLBA\n"); 3278bb16350dSlclee (void) fprintf(fp, "* 18: DIAGPART\n"); 3279bb16350dSlclee (void) fprintf(fp, "* 65: FDISK_LINUX\n"); 3280bb16350dSlclee (void) fprintf(fp, "* 82: FDISK_CPM\n"); 3281bb16350dSlclee (void) fprintf(fp, "* 86: DOSDATA\n"); 3282bb16350dSlclee (void) fprintf(fp, "* 98: OTHEROS\n"); 3283bb16350dSlclee (void) fprintf(fp, "* 99: UNIXOS\n"); 3284bb16350dSlclee (void) fprintf(fp, "* 101: FDISK_NOVELL3\n"); 3285bb16350dSlclee (void) fprintf(fp, "* 119: FDISK_QNX4\n"); 3286bb16350dSlclee (void) fprintf(fp, "* 120: FDISK_QNX42\n"); 3287bb16350dSlclee (void) fprintf(fp, "* 121: FDISK_QNX43\n"); 3288bb16350dSlclee (void) fprintf(fp, "* 130: SUNIXOS\n"); 3289bb16350dSlclee (void) fprintf(fp, "* 131: FDISK_LINUXNAT\n"); 3290bb16350dSlclee (void) fprintf(fp, "* 134: FDISK_NTFSVOL1\n"); 3291bb16350dSlclee (void) fprintf(fp, "* 135: FDISK_NTFSVOL2\n"); 3292bb16350dSlclee (void) fprintf(fp, "* 165: FDISK_BSD\n"); 3293bb16350dSlclee (void) fprintf(fp, "* 167: FDISK_NEXTSTEP\n"); 3294bb16350dSlclee (void) fprintf(fp, "* 183: FDISK_BSDIFS\n"); 3295bb16350dSlclee (void) fprintf(fp, "* 184: FDISK_BSDISWAP\n"); 3296bb16350dSlclee (void) fprintf(fp, "* 190: X86BOOT\n"); 3297bb16350dSlclee (void) fprintf(fp, "* 191: SUNIXOS2\n"); 3298bb16350dSlclee (void) fprintf(fp, "* 238: EFI_PMBR\n"); 3299bb16350dSlclee (void) fprintf(fp, "* 239: EFI_FS\n"); 3300bb16350dSlclee (void) fprintf(fp, "*\n"); 3301bb16350dSlclee (void) fprintf(fp, 33027c478bd9Sstevel@tonic-gate "\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl" 33037c478bd9Sstevel@tonic-gate " Rsect Numsect\n"); 3304342440ecSPrasad Singamsetty 33057c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 3306f85c7842SSuhasini Peddada if (Table[i].systid != UNUSED) 3307bb16350dSlclee (void) fprintf(fp, 3308342440ecSPrasad Singamsetty " %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u" 3309342440ecSPrasad Singamsetty " %-10u\n", 33107c478bd9Sstevel@tonic-gate Table[i].systid, 33117c478bd9Sstevel@tonic-gate Table[i].bootid, 33127c478bd9Sstevel@tonic-gate Table[i].beghead, 33137c478bd9Sstevel@tonic-gate Table[i].begsect & 0x3f, 33147c478bd9Sstevel@tonic-gate ((Table[i].begcyl & 0xff) | ((Table[i].begsect & 33157c478bd9Sstevel@tonic-gate 0xc0) << 2)), 33167c478bd9Sstevel@tonic-gate Table[i].endhead, 33177c478bd9Sstevel@tonic-gate Table[i].endsect & 0x3f, 33187c478bd9Sstevel@tonic-gate ((Table[i].endcyl & 0xff) | ((Table[i].endsect & 33197c478bd9Sstevel@tonic-gate 0xc0) << 2)), 3320f85c7842SSuhasini Peddada lel(Table[i].relsect), 3321f85c7842SSuhasini Peddada lel(Table[i].numsect)); 33227c478bd9Sstevel@tonic-gate } 33237c478bd9Sstevel@tonic-gate if (fp != stdout) 3324bb16350dSlclee (void) fclose(fp); 33257c478bd9Sstevel@tonic-gate } 33267c478bd9Sstevel@tonic-gate 33277c478bd9Sstevel@tonic-gate /* 33287c478bd9Sstevel@tonic-gate * fix_slice 33297c478bd9Sstevel@tonic-gate * Read the VTOC table on the Solaris partition and check that no 33307c478bd9Sstevel@tonic-gate * slices exist that extend past the end of the Solaris partition. 33317c478bd9Sstevel@tonic-gate * If no Solaris partition exists, nothing is done. 33327c478bd9Sstevel@tonic-gate */ 3333bb16350dSlclee static void 3334bb16350dSlclee fix_slice(void) 33357c478bd9Sstevel@tonic-gate { 33367c478bd9Sstevel@tonic-gate int i; 3337342440ecSPrasad Singamsetty uint32_t numsect; 33387c478bd9Sstevel@tonic-gate 33397c478bd9Sstevel@tonic-gate if (io_image) { 3340bb16350dSlclee return; 33417c478bd9Sstevel@tonic-gate } 33427c478bd9Sstevel@tonic-gate 33437c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 33447c478bd9Sstevel@tonic-gate if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) { 33457c478bd9Sstevel@tonic-gate /* 33467c478bd9Sstevel@tonic-gate * Only the size matters (not starting point), since 33477c478bd9Sstevel@tonic-gate * VTOC entries are relative to the start of 33487c478bd9Sstevel@tonic-gate * the partition. 33497c478bd9Sstevel@tonic-gate */ 3350f85c7842SSuhasini Peddada numsect = lel(Table[i].numsect); 33517c478bd9Sstevel@tonic-gate break; 33527c478bd9Sstevel@tonic-gate } 33537c478bd9Sstevel@tonic-gate } 33547c478bd9Sstevel@tonic-gate 33557c478bd9Sstevel@tonic-gate if (i >= FD_NUMPART) { 33567c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 33577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 33587c478bd9Sstevel@tonic-gate "fdisk: No Solaris partition found - VTOC not" 33597c478bd9Sstevel@tonic-gate " checked.\n"); 33607c478bd9Sstevel@tonic-gate } 3361bb16350dSlclee return; 33627c478bd9Sstevel@tonic-gate } 33637c478bd9Sstevel@tonic-gate 3364bb16350dSlclee if (readvtoc() != VTOC_OK) { 33657c478bd9Sstevel@tonic-gate exit(1); /* Failed to read the VTOC */ 33661237e847Slclee } 33677c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 33687c478bd9Sstevel@tonic-gate /* Special case for slice two (entire disk) */ 33697c478bd9Sstevel@tonic-gate if (i == 2) { 33707c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_start != 0) { 33717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3372342440ecSPrasad Singamsetty "slice %d starts at %llu, is not at" 33737c478bd9Sstevel@tonic-gate " start of partition", 33747c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_start); 33757c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3376bb16350dSlclee (void) printf(" adjust ?:"); 33777c478bd9Sstevel@tonic-gate if (yesno()) 33787c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 33797c478bd9Sstevel@tonic-gate } else { 33807c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 33811237e847Slclee (void) fprintf(stderr, " adjusted!\n"); 33827c478bd9Sstevel@tonic-gate } 33837c478bd9Sstevel@tonic-gate 33847c478bd9Sstevel@tonic-gate } 33857c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_size != numsect) { 33867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3387342440ecSPrasad Singamsetty "slice %d size %llu does not cover" 33887c478bd9Sstevel@tonic-gate " complete partition", 33897c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_size); 33907c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3391bb16350dSlclee (void) printf(" adjust ?:"); 33927c478bd9Sstevel@tonic-gate if (yesno()) 33937c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 33947c478bd9Sstevel@tonic-gate numsect; 33957c478bd9Sstevel@tonic-gate } else { 33961237e847Slclee disk_vtoc.v_part[i].p_size = numsect; 33971237e847Slclee (void) fprintf(stderr, " adjusted!\n"); 33987c478bd9Sstevel@tonic-gate } 33997c478bd9Sstevel@tonic-gate } 34007c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_tag != V_BACKUP) { 34017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 34027c478bd9Sstevel@tonic-gate "slice %d tag was %d should be %d", 34037c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_tag, 34047c478bd9Sstevel@tonic-gate V_BACKUP); 34057c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3406bb16350dSlclee (void) printf(" fix ?:"); 34077c478bd9Sstevel@tonic-gate if (yesno()) 34087c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 34097c478bd9Sstevel@tonic-gate V_BACKUP; 34107c478bd9Sstevel@tonic-gate } else { 34117c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = V_BACKUP; 34127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " fixed!\n"); 34137c478bd9Sstevel@tonic-gate } 34147c478bd9Sstevel@tonic-gate } 34151237e847Slclee continue; 34161237e847Slclee } 34177c478bd9Sstevel@tonic-gate if (io_ADJT) { 34187c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_start > numsect || 34197c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start + 34207c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size > numsect) { 34217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3422342440ecSPrasad Singamsetty "slice %d (start %llu, end %llu)" 3423bb16350dSlclee " is larger than the partition", 34247c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_start, 34257c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start + 34267c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size); 34277c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3428bb16350dSlclee (void) printf(" remove ?:"); 34297c478bd9Sstevel@tonic-gate if (yesno()) { 34301237e847Slclee disk_vtoc.v_part[i].p_size = 0; 34311237e847Slclee disk_vtoc.v_part[i].p_start = 0; 34321237e847Slclee disk_vtoc.v_part[i].p_tag = 0; 34331237e847Slclee disk_vtoc.v_part[i].p_flag = 0; 34347c478bd9Sstevel@tonic-gate } 34357c478bd9Sstevel@tonic-gate } else { 34367c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 34377c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 34387c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 34397c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 34407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 34417c478bd9Sstevel@tonic-gate " removed!\n"); 34427c478bd9Sstevel@tonic-gate } 34437c478bd9Sstevel@tonic-gate } 34441237e847Slclee continue; 34451237e847Slclee } 34461237e847Slclee if (disk_vtoc.v_part[i].p_start > numsect) { 34477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3448342440ecSPrasad Singamsetty "slice %d (start %llu) is larger than the " 3449342440ecSPrasad Singamsetty "partition", i, disk_vtoc.v_part[i].p_start); 34507c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3451bb16350dSlclee (void) printf(" remove ?:"); 34527c478bd9Sstevel@tonic-gate if (yesno()) { 34537c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 34541237e847Slclee disk_vtoc.v_part[i].p_start = 0; 34557c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 34567c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 34577c478bd9Sstevel@tonic-gate } 34587c478bd9Sstevel@tonic-gate } else { 34597c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 34607c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 34617c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 34627c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 34637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 34647c478bd9Sstevel@tonic-gate " removed!\n"); 34657c478bd9Sstevel@tonic-gate } 34667c478bd9Sstevel@tonic-gate } else if (disk_vtoc.v_part[i].p_start 34671237e847Slclee + disk_vtoc.v_part[i].p_size > numsect) { 34687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3469342440ecSPrasad Singamsetty "slice %d (end %llu) is larger" 34707c478bd9Sstevel@tonic-gate " than the partition", 34717c478bd9Sstevel@tonic-gate i, 34727c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start + 34737c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size); 34747c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3475bb16350dSlclee (void) printf(" adjust ?:"); 34767c478bd9Sstevel@tonic-gate if (yesno()) { 34771237e847Slclee disk_vtoc.v_part[i].p_size = numsect; 34787c478bd9Sstevel@tonic-gate } 34797c478bd9Sstevel@tonic-gate } else { 34801237e847Slclee disk_vtoc.v_part[i].p_size = numsect; 34811237e847Slclee (void) fprintf(stderr, " adjusted!\n"); 34827c478bd9Sstevel@tonic-gate } 34837c478bd9Sstevel@tonic-gate } 34847c478bd9Sstevel@tonic-gate } 34857c478bd9Sstevel@tonic-gate #if 1 /* bh for now */ 34867c478bd9Sstevel@tonic-gate /* Make the VTOC look sane - ha ha */ 34877c478bd9Sstevel@tonic-gate disk_vtoc.v_version = V_VERSION; 34887c478bd9Sstevel@tonic-gate disk_vtoc.v_sanity = VTOC_SANE; 34897c478bd9Sstevel@tonic-gate disk_vtoc.v_nparts = V_NUMPAR; 34907c478bd9Sstevel@tonic-gate if (disk_vtoc.v_sectorsz == 0) 34917c478bd9Sstevel@tonic-gate disk_vtoc.v_sectorsz = NBPSCTR; 34927c478bd9Sstevel@tonic-gate #endif 34937c478bd9Sstevel@tonic-gate 34947c478bd9Sstevel@tonic-gate /* Write the VTOC back to the disk */ 34957c478bd9Sstevel@tonic-gate if (!io_readonly) 3496bb16350dSlclee (void) writevtoc(); 34977c478bd9Sstevel@tonic-gate } 34987c478bd9Sstevel@tonic-gate 34997c478bd9Sstevel@tonic-gate /* 35007c478bd9Sstevel@tonic-gate * yesno 35017c478bd9Sstevel@tonic-gate * Get yes or no answer. Return 1 for yes and 0 for no. 35027c478bd9Sstevel@tonic-gate */ 35037c478bd9Sstevel@tonic-gate 3504bb16350dSlclee static int 3505bb16350dSlclee yesno(void) 35067c478bd9Sstevel@tonic-gate { 35077c478bd9Sstevel@tonic-gate char s[80]; 35087c478bd9Sstevel@tonic-gate 35097c478bd9Sstevel@tonic-gate for (;;) { 3510933c1499SBarry Harding (void) fgets(s, sizeof (s), stdin); 35117c478bd9Sstevel@tonic-gate rm_blanks(s); 3512933c1499SBarry Harding if (((s[1] != '\0') && (s[1] != '\n')) || 3513933c1499SBarry Harding ((s[0] != 'y') && (s[0] != 'n'))) { 3514bb16350dSlclee (void) printf(E_LINE); 3515bb16350dSlclee (void) printf("Please answer with \"y\" or \"n\": "); 35167c478bd9Sstevel@tonic-gate continue; 35177c478bd9Sstevel@tonic-gate } 35187c478bd9Sstevel@tonic-gate if (s[0] == 'y') 35197c478bd9Sstevel@tonic-gate return (1); 35207c478bd9Sstevel@tonic-gate else 35217c478bd9Sstevel@tonic-gate return (0); 35227c478bd9Sstevel@tonic-gate } 35237c478bd9Sstevel@tonic-gate } 35247c478bd9Sstevel@tonic-gate 35257c478bd9Sstevel@tonic-gate /* 35267c478bd9Sstevel@tonic-gate * readvtoc 35277c478bd9Sstevel@tonic-gate * Read the VTOC from the Solaris partition of the device. 35287c478bd9Sstevel@tonic-gate */ 3529bb16350dSlclee static int 3530bb16350dSlclee readvtoc(void) 35317c478bd9Sstevel@tonic-gate { 35327c478bd9Sstevel@tonic-gate int i; 35337c478bd9Sstevel@tonic-gate int retval = VTOC_OK; 35347c478bd9Sstevel@tonic-gate 3535342440ecSPrasad Singamsetty if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) { 35367c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 35377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Invalid VTOC.\n"); 35387c478bd9Sstevel@tonic-gate vt_inval++; 35397c478bd9Sstevel@tonic-gate retval = VTOC_INVAL; 35407c478bd9Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 35417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 35427c478bd9Sstevel@tonic-gate "GPT\n"); 35437c478bd9Sstevel@tonic-gate retval = VTOC_NOTSUP; 35447c478bd9Sstevel@tonic-gate } else { 35457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot read VTOC.\n"); 35467c478bd9Sstevel@tonic-gate retval = VTOC_RWERR; 35477c478bd9Sstevel@tonic-gate } 35487c478bd9Sstevel@tonic-gate } 35497c478bd9Sstevel@tonic-gate return (retval); 35507c478bd9Sstevel@tonic-gate } 35517c478bd9Sstevel@tonic-gate 35527c478bd9Sstevel@tonic-gate /* 35537c478bd9Sstevel@tonic-gate * writevtoc 35547c478bd9Sstevel@tonic-gate * Write the VTOC to the Solaris partition on the device. 35557c478bd9Sstevel@tonic-gate */ 3556bb16350dSlclee static int 3557bb16350dSlclee writevtoc(void) 35587c478bd9Sstevel@tonic-gate { 35597c478bd9Sstevel@tonic-gate int i; 35607c478bd9Sstevel@tonic-gate int retval = 0; 35617c478bd9Sstevel@tonic-gate 3562342440ecSPrasad Singamsetty if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) { 35637c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 35647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 35657c478bd9Sstevel@tonic-gate "fdisk: Invalid entry exists in VTOC.\n"); 35667c478bd9Sstevel@tonic-gate retval = VTOC_INVAL; 35677c478bd9Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 35687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 35697c478bd9Sstevel@tonic-gate "GPT\n"); 35707c478bd9Sstevel@tonic-gate retval = VTOC_NOTSUP; 35717c478bd9Sstevel@tonic-gate } else { 35727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot write VTOC.\n"); 35737c478bd9Sstevel@tonic-gate retval = VTOC_RWERR; 35747c478bd9Sstevel@tonic-gate } 35757c478bd9Sstevel@tonic-gate } 35767c478bd9Sstevel@tonic-gate return (retval); 35777c478bd9Sstevel@tonic-gate } 35787c478bd9Sstevel@tonic-gate 35797c478bd9Sstevel@tonic-gate /* 35807c478bd9Sstevel@tonic-gate * efi_ioctl 35817c478bd9Sstevel@tonic-gate * issues DKIOCSETEFI IOCTL 35827c478bd9Sstevel@tonic-gate * (duplicate of private efi_ioctl() in rdwr_efi.c 35837c478bd9Sstevel@tonic-gate */ 35847c478bd9Sstevel@tonic-gate static int 35857c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 35867c478bd9Sstevel@tonic-gate { 35877c478bd9Sstevel@tonic-gate void *data = dk_ioc->dki_data; 35887c478bd9Sstevel@tonic-gate int error; 35897c478bd9Sstevel@tonic-gate 35907c478bd9Sstevel@tonic-gate dk_ioc->dki_data_64 = (uintptr_t)data; 35917c478bd9Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc); 35927c478bd9Sstevel@tonic-gate 35937c478bd9Sstevel@tonic-gate return (error); 35947c478bd9Sstevel@tonic-gate } 35957c478bd9Sstevel@tonic-gate 35967c478bd9Sstevel@tonic-gate /* 35977c478bd9Sstevel@tonic-gate * clear_efi 35987c478bd9Sstevel@tonic-gate * Clear EFI labels from the EFI_PMBR partition on the device 35997c478bd9Sstevel@tonic-gate * This function is modeled on the libefi(3LIB) call efi_write() 36007c478bd9Sstevel@tonic-gate */ 3601bb16350dSlclee static int 3602bb16350dSlclee clear_efi(void) 36037c478bd9Sstevel@tonic-gate { 36047c478bd9Sstevel@tonic-gate struct dk_gpt *efi_vtoc; 36057c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc; 36067c478bd9Sstevel@tonic-gate 36077c478bd9Sstevel@tonic-gate /* 36087c478bd9Sstevel@tonic-gate * see if we can read the EFI label 36097c478bd9Sstevel@tonic-gate */ 36107c478bd9Sstevel@tonic-gate if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) { 36117c478bd9Sstevel@tonic-gate return (VT_ERROR); 36127c478bd9Sstevel@tonic-gate } 36137c478bd9Sstevel@tonic-gate 36147c478bd9Sstevel@tonic-gate /* 36157c478bd9Sstevel@tonic-gate * set up the dk_ioc structure for writing 36167c478bd9Sstevel@tonic-gate */ 36177c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 1; 36187c478bd9Sstevel@tonic-gate dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize; 36197c478bd9Sstevel@tonic-gate 36207c478bd9Sstevel@tonic-gate if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) { 36217c478bd9Sstevel@tonic-gate return (VT_ERROR); 36227c478bd9Sstevel@tonic-gate } 36237c478bd9Sstevel@tonic-gate 36247c478bd9Sstevel@tonic-gate /* 36257c478bd9Sstevel@tonic-gate * clear the primary label 36267c478bd9Sstevel@tonic-gate */ 36277c478bd9Sstevel@tonic-gate if (io_debug) { 3628bb16350dSlclee (void) fprintf(stderr, 3629bb16350dSlclee "\tClearing primary EFI label at block %lld\n", 3630bb16350dSlclee dk_ioc.dki_lba); 36317c478bd9Sstevel@tonic-gate } 36327c478bd9Sstevel@tonic-gate 36337c478bd9Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 36347c478bd9Sstevel@tonic-gate free(dk_ioc.dki_data); 36357c478bd9Sstevel@tonic-gate switch (errno) { 36367c478bd9Sstevel@tonic-gate case EIO: 36377c478bd9Sstevel@tonic-gate return (VT_EIO); 36387c478bd9Sstevel@tonic-gate case EINVAL: 36397c478bd9Sstevel@tonic-gate return (VT_EINVAL); 36407c478bd9Sstevel@tonic-gate default: 36417c478bd9Sstevel@tonic-gate return (VT_ERROR); 36427c478bd9Sstevel@tonic-gate } 36437c478bd9Sstevel@tonic-gate } 36447c478bd9Sstevel@tonic-gate 36457c478bd9Sstevel@tonic-gate /* 36467c478bd9Sstevel@tonic-gate * clear the backup partition table 36477c478bd9Sstevel@tonic-gate */ 36487c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1; 36497c478bd9Sstevel@tonic-gate dk_ioc.dki_length -= efi_vtoc->efi_lbasize; 3650*65908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data + 3651*65908c77Syu, larry liu - Sun Microsystems - Beijing China efi_vtoc->efi_lbasize); 36527c478bd9Sstevel@tonic-gate if (io_debug) { 3653bb16350dSlclee (void) fprintf(stderr, 3654bb16350dSlclee "\tClearing backup partition table at block %lld\n", 3655bb16350dSlclee dk_ioc.dki_lba); 36567c478bd9Sstevel@tonic-gate } 36577c478bd9Sstevel@tonic-gate 36587c478bd9Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 36597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\tUnable to clear backup EFI label at " 36607c478bd9Sstevel@tonic-gate "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1, 36617c478bd9Sstevel@tonic-gate errno); 36627c478bd9Sstevel@tonic-gate } 36637c478bd9Sstevel@tonic-gate 36647c478bd9Sstevel@tonic-gate /* 36657c478bd9Sstevel@tonic-gate * clear the backup label 36667c478bd9Sstevel@tonic-gate */ 36677c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_lba; 36687c478bd9Sstevel@tonic-gate dk_ioc.dki_length = efi_vtoc->efi_lbasize; 3669*65908c77Syu, larry liu - Sun Microsystems - Beijing China dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data - 3670*65908c77Syu, larry liu - Sun Microsystems - Beijing China efi_vtoc->efi_lbasize); 36717c478bd9Sstevel@tonic-gate if (io_debug) { 3672bb16350dSlclee (void) fprintf(stderr, "\tClearing backup label at block " 3673bb16350dSlclee "%lld\n", dk_ioc.dki_lba); 36747c478bd9Sstevel@tonic-gate } 36757c478bd9Sstevel@tonic-gate 36767c478bd9Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 3677bb16350dSlclee (void) fprintf(stderr, 3678bb16350dSlclee "\tUnable to clear backup EFI label at " 3679bb16350dSlclee "block %llu; errno %d\n", 3680bb16350dSlclee efi_vtoc->efi_last_lba, 36817c478bd9Sstevel@tonic-gate errno); 36827c478bd9Sstevel@tonic-gate } 36837c478bd9Sstevel@tonic-gate 36847c478bd9Sstevel@tonic-gate free(dk_ioc.dki_data); 36857c478bd9Sstevel@tonic-gate efi_free(efi_vtoc); 36867c478bd9Sstevel@tonic-gate 36877c478bd9Sstevel@tonic-gate return (0); 36887c478bd9Sstevel@tonic-gate } 36897c478bd9Sstevel@tonic-gate 36907c478bd9Sstevel@tonic-gate /* 36917c478bd9Sstevel@tonic-gate * clear_vtoc 36927c478bd9Sstevel@tonic-gate * Clear the VTOC from the current or previous Solaris partition on the 36937c478bd9Sstevel@tonic-gate * device. 36947c478bd9Sstevel@tonic-gate */ 3695bb16350dSlclee static void 36967c478bd9Sstevel@tonic-gate clear_vtoc(int table, int part) 36977c478bd9Sstevel@tonic-gate { 36987c478bd9Sstevel@tonic-gate struct ipart *clr_table; 3699*65908c77Syu, larry liu - Sun Microsystems - Beijing China char *disk_label; 3700342440ecSPrasad Singamsetty uint32_t pcyl, ncyl, count; 3701342440ecSPrasad Singamsetty diskaddr_t backup_block, solaris_offset; 3702342440ecSPrasad Singamsetty ssize_t bytes; 37039d5d1945Sbharding off_t seek_byte; 37047c478bd9Sstevel@tonic-gate 37057c478bd9Sstevel@tonic-gate #ifdef DEBUG 3706*65908c77Syu, larry liu - Sun Microsystems - Beijing China char *read_label; 37077c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 37087c478bd9Sstevel@tonic-gate 37097c478bd9Sstevel@tonic-gate if (table == OLD) { 37107c478bd9Sstevel@tonic-gate clr_table = &Old_Table[part]; 37117c478bd9Sstevel@tonic-gate } else { 37127c478bd9Sstevel@tonic-gate clr_table = &Table[part]; 37137c478bd9Sstevel@tonic-gate } 37147c478bd9Sstevel@tonic-gate 3715*65908c77Syu, larry liu - Sun Microsystems - Beijing China disk_label = (char *)calloc(sectsiz, 1); 3716*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (disk_label == NULL) { 3717*65908c77Syu, larry liu - Sun Microsystems - Beijing China return; 3718*65908c77Syu, larry liu - Sun Microsystems - Beijing China } 37197c478bd9Sstevel@tonic-gate 3720f85c7842SSuhasini Peddada seek_byte = (off_t)(lel(clr_table->relsect) + VTOC_OFFSET) * sectsiz; 37217c478bd9Sstevel@tonic-gate 37227c478bd9Sstevel@tonic-gate if (io_debug) { 37239d5d1945Sbharding (void) fprintf(stderr, 37249d5d1945Sbharding "\tClearing primary VTOC at byte %llu (block %llu)\n", 37259d5d1945Sbharding (uint64_t)seek_byte, 3726f85c7842SSuhasini Peddada (uint64_t)(lel(clr_table->relsect) + VTOC_OFFSET)); 37277c478bd9Sstevel@tonic-gate } 37287c478bd9Sstevel@tonic-gate 37297c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3730bb16350dSlclee (void) fprintf(stderr, 37319d5d1945Sbharding "\tError seeking to primary label at byte %llu\n", 37329d5d1945Sbharding (uint64_t)seek_byte); 3733*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(disk_label); 3734bb16350dSlclee return; 37357c478bd9Sstevel@tonic-gate } 37367c478bd9Sstevel@tonic-gate 3737*65908c77Syu, larry liu - Sun Microsystems - Beijing China bytes = write(Dev, disk_label, sectsiz); 37387c478bd9Sstevel@tonic-gate 3739*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (bytes != sectsiz) { 3740bb16350dSlclee (void) fprintf(stderr, 3741342440ecSPrasad Singamsetty "\tWarning: only %d bytes written to clear primary" 3742342440ecSPrasad Singamsetty " VTOC!\n", bytes); 37437c478bd9Sstevel@tonic-gate } 37447c478bd9Sstevel@tonic-gate 37457c478bd9Sstevel@tonic-gate #ifdef DEBUG 37467c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3747bb16350dSlclee (void) fprintf(stderr, 37489d5d1945Sbharding "DEBUG: Error seeking to primary label at byte %llu\n", 37499d5d1945Sbharding (uint64_t)seek_byte); 3750*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(disk_label); 3751bb16350dSlclee return; 37527c478bd9Sstevel@tonic-gate } else { 37539d5d1945Sbharding (void) fprintf(stderr, 37549d5d1945Sbharding "DEBUG: Successful lseek() to byte %llu\n", 37559d5d1945Sbharding (uint64_t)seek_byte); 37567c478bd9Sstevel@tonic-gate } 37577c478bd9Sstevel@tonic-gate 3758*65908c77Syu, larry liu - Sun Microsystems - Beijing China read_label = (char *)calloc(sectsiz, 1); 3759*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (read_label == NULL) { 3760*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(disk_label); 3761*65908c77Syu, larry liu - Sun Microsystems - Beijing China return; 3762*65908c77Syu, larry liu - Sun Microsystems - Beijing China } 37637c478bd9Sstevel@tonic-gate 3764*65908c77Syu, larry liu - Sun Microsystems - Beijing China bytes = read(Dev, read_label, sectsiz); 3765*65908c77Syu, larry liu - Sun Microsystems - Beijing China 3766*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (bytes != sectsiz) { 3767bb16350dSlclee (void) fprintf(stderr, 3768bb16350dSlclee "DEBUG: Warning: only %d bytes read of label\n", 37697c478bd9Sstevel@tonic-gate bytes); 37707c478bd9Sstevel@tonic-gate } 37717c478bd9Sstevel@tonic-gate 3772*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (memcmp(disk_label, read_label, sectsiz) != 0) { 3773bb16350dSlclee (void) fprintf(stderr, 3774bb16350dSlclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 37757c478bd9Sstevel@tonic-gate } else { 3776bb16350dSlclee (void) fprintf(stderr, "DEBUG Good compare of disk_label and " 37777c478bd9Sstevel@tonic-gate "read_label\n"); 37787c478bd9Sstevel@tonic-gate } 37797c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 37807c478bd9Sstevel@tonic-gate 37817c478bd9Sstevel@tonic-gate /* Clear backup label */ 3782f85c7842SSuhasini Peddada pcyl = lel(clr_table->numsect) / (heads * sectors); 3783f85c7842SSuhasini Peddada solaris_offset = lel(clr_table->relsect); 37847c478bd9Sstevel@tonic-gate ncyl = pcyl - acyl; 37857c478bd9Sstevel@tonic-gate 37867c478bd9Sstevel@tonic-gate backup_block = ((ncyl + acyl - 1) * 37877c478bd9Sstevel@tonic-gate (heads * sectors)) + ((heads - 1) * sectors) + 1; 37887c478bd9Sstevel@tonic-gate 37897c478bd9Sstevel@tonic-gate for (count = 1; count < 6; count++) { 3790*65908c77Syu, larry liu - Sun Microsystems - Beijing China seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz; 37917c478bd9Sstevel@tonic-gate 37927c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3793bb16350dSlclee (void) fprintf(stderr, 37949d5d1945Sbharding "\tError seeking to backup label at byte %llu on " 37959d5d1945Sbharding "%s.\n", (uint64_t)seek_byte, Dfltdev); 3796*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(disk_label); 3797*65908c77Syu, larry liu - Sun Microsystems - Beijing China #ifdef DEBUG 3798*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(read_label); 3799*65908c77Syu, larry liu - Sun Microsystems - Beijing China #endif /* DEBUG */ 3800bb16350dSlclee return; 38017c478bd9Sstevel@tonic-gate } 38027c478bd9Sstevel@tonic-gate 38037c478bd9Sstevel@tonic-gate if (io_debug) { 3804bb16350dSlclee (void) fprintf(stderr, "\tClearing backup VTOC at" 38059d5d1945Sbharding " byte %llu (block %llu)\n", 38069d5d1945Sbharding (uint64_t)seek_byte, 38079d5d1945Sbharding (uint64_t)(solaris_offset + backup_block)); 38087c478bd9Sstevel@tonic-gate } 38097c478bd9Sstevel@tonic-gate 3810*65908c77Syu, larry liu - Sun Microsystems - Beijing China bytes = write(Dev, disk_label, sectsiz); 38117c478bd9Sstevel@tonic-gate 3812*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (bytes != sectsiz) { 3813bb16350dSlclee (void) fprintf(stderr, 3814bb16350dSlclee "\t\tWarning: only %d bytes written to " 38159d5d1945Sbharding "clear backup VTOC at block %llu!\n", bytes, 38169d5d1945Sbharding (uint64_t)(solaris_offset + backup_block)); 38177c478bd9Sstevel@tonic-gate } 38187c478bd9Sstevel@tonic-gate 38197c478bd9Sstevel@tonic-gate #ifdef DEBUG 38207c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3821bb16350dSlclee (void) fprintf(stderr, 38229d5d1945Sbharding "DEBUG: Error seeking to backup label at byte %llu\n", 38239d5d1945Sbharding (uint64_t)seek_byte); 3824*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(disk_label); 3825*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(read_label); 3826bb16350dSlclee return; 38277c478bd9Sstevel@tonic-gate } else { 38289d5d1945Sbharding (void) fprintf(stderr, 38299d5d1945Sbharding "DEBUG: Successful lseek() to byte %llu\n", 38309d5d1945Sbharding (uint64_t)seek_byte); 38317c478bd9Sstevel@tonic-gate } 38327c478bd9Sstevel@tonic-gate 3833*65908c77Syu, larry liu - Sun Microsystems - Beijing China bytes = read(Dev, read_label, sectsiz); 38347c478bd9Sstevel@tonic-gate 3835*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (bytes != sectsiz) { 3836bb16350dSlclee (void) fprintf(stderr, 3837bb16350dSlclee "DEBUG: Warning: only %d bytes read of backup label\n", 3838bb16350dSlclee bytes); 38397c478bd9Sstevel@tonic-gate } 38407c478bd9Sstevel@tonic-gate 3841*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (memcmp(disk_label, read_label, sectsiz) != 0) { 3842bb16350dSlclee (void) fprintf(stderr, 3843bb16350dSlclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 38447c478bd9Sstevel@tonic-gate } else { 3845bb16350dSlclee (void) fprintf(stderr, 3846bb16350dSlclee "DEBUG: Good compare of disk_label and backup " 38477c478bd9Sstevel@tonic-gate "read_label\n"); 38487c478bd9Sstevel@tonic-gate } 3849*65908c77Syu, larry liu - Sun Microsystems - Beijing China 38507c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 38517c478bd9Sstevel@tonic-gate 38527c478bd9Sstevel@tonic-gate backup_block += 2; 38537c478bd9Sstevel@tonic-gate } 3854*65908c77Syu, larry liu - Sun Microsystems - Beijing China 3855*65908c77Syu, larry liu - Sun Microsystems - Beijing China #ifdef DEBUG 3856*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(read_label); 3857*65908c77Syu, larry liu - Sun Microsystems - Beijing China #endif /* DEBUG */ 3858*65908c77Syu, larry liu - Sun Microsystems - Beijing China free(disk_label); 38597c478bd9Sstevel@tonic-gate } 38607c478bd9Sstevel@tonic-gate 38617c478bd9Sstevel@tonic-gate #define FDISK_STANDARD_LECTURE \ 38627c478bd9Sstevel@tonic-gate "Fdisk is normally used with the device that " \ 38637c478bd9Sstevel@tonic-gate "represents the entire fixed disk.\n" \ 38647c478bd9Sstevel@tonic-gate "(For example, /dev/rdsk/c0d0p0 on x86 or " \ 38657c478bd9Sstevel@tonic-gate "/dev/rdsk/c0t5d0s2 on sparc).\n" 38667c478bd9Sstevel@tonic-gate 38677c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NOT_SECTOR_ZERO \ 38687c478bd9Sstevel@tonic-gate "The device does not appear to include absolute\n" \ 38697c478bd9Sstevel@tonic-gate "sector 0 of the PHYSICAL disk " \ 38707c478bd9Sstevel@tonic-gate "(the normal location for an fdisk table).\n" 38717c478bd9Sstevel@tonic-gate 38727c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NOT_FULL \ 38737c478bd9Sstevel@tonic-gate "The device does not appear to encompass the entire PHYSICAL disk.\n" 38747c478bd9Sstevel@tonic-gate 38757c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NO_VTOC \ 38767c478bd9Sstevel@tonic-gate "Unable to find a volume table of contents.\n" \ 38777c478bd9Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 38787c478bd9Sstevel@tonic-gate 38797c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NO_GEOM \ 38807c478bd9Sstevel@tonic-gate "Unable to get geometry from device.\n" \ 38817c478bd9Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 38827c478bd9Sstevel@tonic-gate 38837c478bd9Sstevel@tonic-gate #define FDISK_SHALL_I_CONTINUE \ 38847c478bd9Sstevel@tonic-gate "Are you sure you want to continue? (y/n) " 38857c478bd9Sstevel@tonic-gate 38867c478bd9Sstevel@tonic-gate /* 38877c478bd9Sstevel@tonic-gate * lecture_and_query 38887c478bd9Sstevel@tonic-gate * Called when a sanity check fails. This routine gives a warning 38897c478bd9Sstevel@tonic-gate * specific to the check that fails, followed by a generic lecture 38907c478bd9Sstevel@tonic-gate * about the "right" device to supply as input. Then, if appropriate, 38917c478bd9Sstevel@tonic-gate * it will prompt the user on whether or not they want to continue. 38927c478bd9Sstevel@tonic-gate * Inappropriate times for prompting are when the user has selected 38937c478bd9Sstevel@tonic-gate * non-interactive mode or read-only mode. 38947c478bd9Sstevel@tonic-gate */ 3895bb16350dSlclee static int 38967c478bd9Sstevel@tonic-gate lecture_and_query(char *warning, char *devname) 38977c478bd9Sstevel@tonic-gate { 38987c478bd9Sstevel@tonic-gate if (io_nifdisk) 38997c478bd9Sstevel@tonic-gate return (0); 39007c478bd9Sstevel@tonic-gate 3901bb16350dSlclee (void) fprintf(stderr, "WARNING: Device %s: \n", devname); 3902bb16350dSlclee (void) fprintf(stderr, "%s", warning); 3903bb16350dSlclee (void) fprintf(stderr, FDISK_STANDARD_LECTURE); 3904bb16350dSlclee (void) fprintf(stderr, FDISK_SHALL_I_CONTINUE); 39057c478bd9Sstevel@tonic-gate 39067c478bd9Sstevel@tonic-gate return (yesno()); 39077c478bd9Sstevel@tonic-gate } 39087c478bd9Sstevel@tonic-gate 3909bb16350dSlclee static void 39107c478bd9Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd) 39117c478bd9Sstevel@tonic-gate { 3912342440ecSPrasad Singamsetty struct extvtoc v; 39137c478bd9Sstevel@tonic-gate struct dk_geom d; 39147c478bd9Sstevel@tonic-gate struct part_info pi; 3915342440ecSPrasad Singamsetty struct extpart_info extpi; 3916342440ecSPrasad Singamsetty diskaddr_t totsize; 39177c478bd9Sstevel@tonic-gate int idx = -1; 39187c478bd9Sstevel@tonic-gate 39197c478bd9Sstevel@tonic-gate /* 39207c478bd9Sstevel@tonic-gate * First try the PARTINFO ioctl. If it works, we will be able 39217c478bd9Sstevel@tonic-gate * to tell if they've specified the full disk partition by checking 39227c478bd9Sstevel@tonic-gate * to see if they've specified a partition that starts at sector 0. 39237c478bd9Sstevel@tonic-gate */ 3924342440ecSPrasad Singamsetty if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) { 3925342440ecSPrasad Singamsetty if (extpi.p_start != 0) { 3926342440ecSPrasad Singamsetty if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 3927342440ecSPrasad Singamsetty devname)) { 3928342440ecSPrasad Singamsetty (void) close(fd); 3929342440ecSPrasad Singamsetty exit(1); 3930342440ecSPrasad Singamsetty } 3931342440ecSPrasad Singamsetty } 3932342440ecSPrasad Singamsetty } else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) { 39337c478bd9Sstevel@tonic-gate if (pi.p_start != 0) { 39347c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 39357c478bd9Sstevel@tonic-gate devname)) { 39367c478bd9Sstevel@tonic-gate (void) close(fd); 39377c478bd9Sstevel@tonic-gate exit(1); 39387c478bd9Sstevel@tonic-gate } 39397c478bd9Sstevel@tonic-gate } 39407c478bd9Sstevel@tonic-gate } else { 3941342440ecSPrasad Singamsetty if ((idx = read_extvtoc(fd, &v)) < 0) { 39427c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_VTOC, 39437c478bd9Sstevel@tonic-gate devname)) { 39447c478bd9Sstevel@tonic-gate (void) close(fd); 39457c478bd9Sstevel@tonic-gate exit(1); 39467c478bd9Sstevel@tonic-gate } 39477c478bd9Sstevel@tonic-gate return; 39487c478bd9Sstevel@tonic-gate } 39497c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &d) == -1) { 39507c478bd9Sstevel@tonic-gate perror(devname); 39517c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_GEOM, 39527c478bd9Sstevel@tonic-gate devname)) { 39537c478bd9Sstevel@tonic-gate (void) close(fd); 39547c478bd9Sstevel@tonic-gate exit(1); 39557c478bd9Sstevel@tonic-gate } 39567c478bd9Sstevel@tonic-gate return; 39577c478bd9Sstevel@tonic-gate } 3958342440ecSPrasad Singamsetty totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect; 39597c478bd9Sstevel@tonic-gate if (v.v_part[idx].p_size != totsize) { 39607c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_FULL, 39617c478bd9Sstevel@tonic-gate devname)) { 39627c478bd9Sstevel@tonic-gate (void) close(fd); 39637c478bd9Sstevel@tonic-gate exit(1); 39647c478bd9Sstevel@tonic-gate } 39657c478bd9Sstevel@tonic-gate } 39667c478bd9Sstevel@tonic-gate } 39677c478bd9Sstevel@tonic-gate } 39687c478bd9Sstevel@tonic-gate 39697c478bd9Sstevel@tonic-gate 39707c478bd9Sstevel@tonic-gate /* 39717c478bd9Sstevel@tonic-gate * get_node 39727c478bd9Sstevel@tonic-gate * Called from main to construct the name of the device node to open. 39737c478bd9Sstevel@tonic-gate * Initially tries to stat the node exactly as provided, if that fails 39747c478bd9Sstevel@tonic-gate * we prepend the default path (/dev/rdsk/). 39757c478bd9Sstevel@tonic-gate */ 39767c478bd9Sstevel@tonic-gate static char * 39777c478bd9Sstevel@tonic-gate get_node(char *devname) 39787c478bd9Sstevel@tonic-gate { 39797c478bd9Sstevel@tonic-gate char *node; 39807c478bd9Sstevel@tonic-gate struct stat statbuf; 39817c478bd9Sstevel@tonic-gate size_t space; 39827c478bd9Sstevel@tonic-gate 39837c478bd9Sstevel@tonic-gate /* Don't do anything if we are skipping device checks */ 39847c478bd9Sstevel@tonic-gate if (io_image) 39857c478bd9Sstevel@tonic-gate return (devname); 39867c478bd9Sstevel@tonic-gate 39877c478bd9Sstevel@tonic-gate node = devname; 39887c478bd9Sstevel@tonic-gate 39897c478bd9Sstevel@tonic-gate /* Try the node as provided first */ 39907c478bd9Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 39917c478bd9Sstevel@tonic-gate /* 39927c478bd9Sstevel@tonic-gate * Copy the passed in string to a new buffer, prepend the 39937c478bd9Sstevel@tonic-gate * default path and try again. 39947c478bd9Sstevel@tonic-gate */ 39957c478bd9Sstevel@tonic-gate space = strlen(DEFAULT_PATH) + strlen(devname) + 1; 39967c478bd9Sstevel@tonic-gate 39977c478bd9Sstevel@tonic-gate if ((node = malloc(space)) == NULL) { 3998bb16350dSlclee (void) fprintf(stderr, "fdisk: Unable to obtain memory " 39997c478bd9Sstevel@tonic-gate "for device node.\n"); 40007c478bd9Sstevel@tonic-gate exit(1); 40017c478bd9Sstevel@tonic-gate } 40027c478bd9Sstevel@tonic-gate 40037c478bd9Sstevel@tonic-gate /* Copy over the default path and the provided node */ 40047c478bd9Sstevel@tonic-gate (void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH)); 40057c478bd9Sstevel@tonic-gate space -= strlen(DEFAULT_PATH); 40067c478bd9Sstevel@tonic-gate (void) strlcpy(node + strlen(DEFAULT_PATH), devname, space); 40077c478bd9Sstevel@tonic-gate 40087c478bd9Sstevel@tonic-gate /* Try to stat it again */ 40097c478bd9Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 40107c478bd9Sstevel@tonic-gate /* Failed all options, give up */ 4011bb16350dSlclee (void) fprintf(stderr, 4012bb16350dSlclee "fdisk: Cannot stat device %s.\n", 40137c478bd9Sstevel@tonic-gate devname); 40147c478bd9Sstevel@tonic-gate exit(1); 40157c478bd9Sstevel@tonic-gate } 40167c478bd9Sstevel@tonic-gate } 40177c478bd9Sstevel@tonic-gate 40187c478bd9Sstevel@tonic-gate /* Make sure the device specified is the raw device */ 40197c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 4020bb16350dSlclee (void) fprintf(stderr, 4021bb16350dSlclee "fdisk: %s must be a raw device.\n", node); 40227c478bd9Sstevel@tonic-gate exit(1); 40237c478bd9Sstevel@tonic-gate } 40247c478bd9Sstevel@tonic-gate 40257c478bd9Sstevel@tonic-gate return (node); 40267c478bd9Sstevel@tonic-gate } 4027