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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*bb16350dSlclee * Copyright 2005 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 #pragma ident "%Z%%M% %I% %E% SMI" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * PROGRAM: fdisk(1M) 387c478bd9Sstevel@tonic-gate * This program reads the partition table on the specified device and 397c478bd9Sstevel@tonic-gate * also reads the drive parameters. The user can perform various 407c478bd9Sstevel@tonic-gate * operations from a supplied menu or from the command line. Diagnostic 417c478bd9Sstevel@tonic-gate * options are also available. 427c478bd9Sstevel@tonic-gate */ 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <stdlib.h> 467c478bd9Sstevel@tonic-gate #include <string.h> 477c478bd9Sstevel@tonic-gate #include <unistd.h> 487c478bd9Sstevel@tonic-gate #include <errno.h> 497c478bd9Sstevel@tonic-gate #include <fcntl.h> 507c478bd9Sstevel@tonic-gate #include <ctype.h> 517c478bd9Sstevel@tonic-gate #include <sys/stat.h> 527c478bd9Sstevel@tonic-gate #include <sys/types.h> 537c478bd9Sstevel@tonic-gate #include <sys/param.h> 547c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 557c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h> 567c478bd9Sstevel@tonic-gate #include <sys/byteorder.h> 577c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 607c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 617c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #define CLR_SCR "[1;1H[0J" 647c478bd9Sstevel@tonic-gate #define CLR_LIN "[0K" 657c478bd9Sstevel@tonic-gate #define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \ 667c478bd9Sstevel@tonic-gate "[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H" 677c478bd9Sstevel@tonic-gate #define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K" 687c478bd9Sstevel@tonic-gate #define W_LINE "[12;1H[0K[11;1H[0K" 697c478bd9Sstevel@tonic-gate #define E_LINE "[24;1H[0K[23;1H[0K" 707c478bd9Sstevel@tonic-gate #define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \ 717c478bd9Sstevel@tonic-gate "[0K[18;1H[0K[19;1H[0K[13;1H" 727c478bd9Sstevel@tonic-gate #define T_LINE "[1;1H[0K" 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define DEFAULT_PATH "/dev/rdsk/" 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* XXX - should be in fdisk.h, used by sd as well */ 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * the MAX values are the maximum usable values for BIOS chs values 807c478bd9Sstevel@tonic-gate * The MAX_CYL value of 1022 is the maximum usable value 817c478bd9Sstevel@tonic-gate * the value of 1023 is a fence value, 827c478bd9Sstevel@tonic-gate * indicating no CHS geometry exists for the corresponding LBA value. 837c478bd9Sstevel@tonic-gate * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1) 847c478bd9Sstevel@tonic-gate * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT) 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate #define MAX_SECT (63) 877c478bd9Sstevel@tonic-gate #define MAX_CYL (1022) 887c478bd9Sstevel@tonic-gate #define MAX_HEAD (254) 897c478bd9Sstevel@tonic-gate 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 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Support for fdisk(1M) on the SPARC platform 1027c478bd9Sstevel@tonic-gate * In order to convert little endian values to big endian for SPARC, 1037c478bd9Sstevel@tonic-gate * byte/short and long values must be swapped. 1047c478bd9Sstevel@tonic-gate * These swapping macros will be used to access information in the 1057c478bd9Sstevel@tonic-gate * mboot and ipart structures. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #ifdef sparc 1097c478bd9Sstevel@tonic-gate #define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF)) 1107c478bd9Sstevel@tonic-gate #define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \ 1117c478bd9Sstevel@tonic-gate (les((unsigned)((val)&0xffff0000)>>16))) 1127c478bd9Sstevel@tonic-gate #else 1137c478bd9Sstevel@tonic-gate #define les(val) (val) 1147c478bd9Sstevel@tonic-gate #define lel(val) (val) 1157c478bd9Sstevel@tonic-gate #endif 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1187c478bd9Sstevel@tonic-gate #define VTOC_OFFSET 512 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 125*bb16350dSlclee 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 133*bb16350dSlclee 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 194*bb16350dSlclee static char Ostr[] = "Other OS"; 195*bb16350dSlclee static char Dstr[] = "DOS12"; 196*bb16350dSlclee static char D16str[] = "DOS16"; 197*bb16350dSlclee static char DDstr[] = "DOS-DATA"; 198*bb16350dSlclee static char EDstr[] = "EXT-DOS"; 199*bb16350dSlclee static char DBstr[] = "DOS-BIG"; 200*bb16350dSlclee static char PCstr[] = "PCIX"; 201*bb16350dSlclee static char Ustr[] = "UNIX System"; 202*bb16350dSlclee static char SUstr[] = "Solaris"; 203*bb16350dSlclee static char SU2str[] = "Solaris2"; 204*bb16350dSlclee static char X86str[] = "x86 Boot"; 205*bb16350dSlclee static char DIAGstr[] = "Diagnostic"; 206*bb16350dSlclee static char IFSstr[] = "IFS: NTFS"; 207*bb16350dSlclee static char AIXstr[] = "AIX Boot"; 208*bb16350dSlclee static char AIXDstr[] = "AIX Data"; 209*bb16350dSlclee static char OS2str[] = "OS/2 Boot"; 210*bb16350dSlclee static char WINstr[] = "Win95 FAT32"; 211*bb16350dSlclee static char EWINstr[] = "Ext Win95"; 212*bb16350dSlclee static char FAT95str[] = "FAT16 LBA"; 213*bb16350dSlclee static char EXTLstr[] = "EXT LBA"; 214*bb16350dSlclee static char LINUXstr[] = "Linux"; 215*bb16350dSlclee static char CPMstr[] = "CP/M"; 216*bb16350dSlclee static char NOVstr[] = "Netware 3.x+"; 217*bb16350dSlclee static char QNXstr[] = "QNX 4.x"; 218*bb16350dSlclee static char QNX2str[] = "QNX part 2"; 219*bb16350dSlclee static char QNX3str[] = "QNX part 3"; 220*bb16350dSlclee static char LINNATstr[] = "Linux native"; 221*bb16350dSlclee static char NTFSVOL1str[] = "NT volset 1"; 222*bb16350dSlclee static char NTFSVOL2str[] = "NT volset 2"; 223*bb16350dSlclee static char BSDstr[] = "BSD OS"; 224*bb16350dSlclee static char NEXTSTEPstr[] = "NeXTSTEP"; 225*bb16350dSlclee static char BSDIFSstr[] = "BSDI FS"; 226*bb16350dSlclee static char BSDISWAPstr[] = "BSDI swap"; 227*bb16350dSlclee static char Actvstr[] = "Active"; 228*bb16350dSlclee static char EFIstr[] = "EFI"; 229*bb16350dSlclee static char NAstr[] = " "; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* All the user options and flags */ 232*bb16350dSlclee static char *Dfltdev; /* name of fixed disk drive */ 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate /* Diagnostic options */ 235*bb16350dSlclee static int io_wrt = 0; /* write stdin to disk (-w) */ 236*bb16350dSlclee static int io_rd = 0; /* read disk and write stdout (-r) */ 237*bb16350dSlclee static char *io_fatt; /* user supplied pattern (-P pattern) */ 238*bb16350dSlclee static int io_patt = 0; /* write pattern to disk (-P pattern) */ 239*bb16350dSlclee static int io_lgeom = 0; /* get label geometry (-g) */ 240*bb16350dSlclee static int io_pgeom = 0; /* get drive physical geometry (-G) */ 241*bb16350dSlclee static char *io_sgeom = 0; /* set label geometry (-S geom_file) */ 242*bb16350dSlclee 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. */ 246*bb16350dSlclee static int io_offset = 0; /* offset sector (-o offset) */ 247*bb16350dSlclee static int io_size = 0; /* size in sectors (-s size) */ 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* Partition table flags */ 250*bb16350dSlclee static int v_flag = 0; /* virtual geometry-HBA flag (-v) */ 251*bb16350dSlclee static int stdo_flag = 0; /* stdout flag (-W -) */ 252*bb16350dSlclee static int io_fdisk = 0; /* do fdisk operation */ 253*bb16350dSlclee static int io_ifdisk = 0; /* interactive partition */ 254*bb16350dSlclee static int io_nifdisk = 0; /* non-interactive partition (-n) */ 2557c478bd9Sstevel@tonic-gate 256*bb16350dSlclee static int io_adjt = 0; /* check/adjust VTOC (truncate (-t)) */ 257*bb16350dSlclee static int io_ADJT = 0; /* check/adjust VTOC (delete (-T)) */ 258*bb16350dSlclee static char *io_ffdisk = 0; /* input fdisk file name (-F file) */ 259*bb16350dSlclee static char *io_Wfdisk = 0; /* output fdisk file name (-W file) */ 260*bb16350dSlclee static char *io_Afdisk = 0; /* add entry to partition table (-A) */ 261*bb16350dSlclee static char *io_Dfdisk = 0; /* delete entry from part. table (-D) */ 2627c478bd9Sstevel@tonic-gate 263*bb16350dSlclee static char *io_mboot = 0; /* master boot record (-b boot_file) */ 2647c478bd9Sstevel@tonic-gate 265*bb16350dSlclee static struct mboot BootCod; /* buffer for master boot record */ 2667c478bd9Sstevel@tonic-gate 267*bb16350dSlclee static int io_wholedisk = 0; /* use whole disk for Solaris (-B) */ 268*bb16350dSlclee static int io_EFIdisk = 0; /* use whole disk for EFI (-E) */ 269*bb16350dSlclee static int io_debug = 0; /* activate verbose mode (-d) */ 270*bb16350dSlclee static int io_image = 0; /* create image using geometry (-I) */ 2717c478bd9Sstevel@tonic-gate 272*bb16350dSlclee static struct mboot *Bootblk; /* pointer to cut/paste sector zero */ 273*bb16350dSlclee static char *Bootsect; /* pointer to sector zero buffer */ 274*bb16350dSlclee static char *Nullsect; 275*bb16350dSlclee static struct vtoc disk_vtoc; /* verify VTOC table */ 276*bb16350dSlclee static int vt_inval = 0; 277*bb16350dSlclee static int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */ 278*bb16350dSlclee static int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */ 2797c478bd9Sstevel@tonic-gate 280*bb16350dSlclee static struct ipart Table[FD_NUMPART]; 281*bb16350dSlclee static struct ipart Old_Table[FD_NUMPART]; 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate /* Disk geometry information */ 284*bb16350dSlclee static struct dk_geom disk_geom; 2857c478bd9Sstevel@tonic-gate 286*bb16350dSlclee static int Dev; /* fd for open device */ 2877c478bd9Sstevel@tonic-gate /* Physical geometry for the drive */ 288*bb16350dSlclee static int Numcyl; /* number of cylinders */ 289*bb16350dSlclee static int heads; /* number of heads */ 290*bb16350dSlclee static int sectors; /* number of sectors per track */ 291*bb16350dSlclee static int acyl; /* number of alternate sectors */ 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */ 294*bb16350dSlclee static int hba_Numcyl; /* number of cylinders */ 295*bb16350dSlclee static int hba_heads; /* number of heads */ 296*bb16350dSlclee static int hba_sectors; /* number of sectors per track */ 2977c478bd9Sstevel@tonic-gate 298*bb16350dSlclee static int sectsiz; /* sector size */ 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate /* Load functions for fdisk table modification */ 3017c478bd9Sstevel@tonic-gate #define LOADFILE 0 /* load fdisk from file */ 3027c478bd9Sstevel@tonic-gate #define LOADDEL 1 /* delete an fdisk entry */ 3037c478bd9Sstevel@tonic-gate #define LOADADD 2 /* add an fdisk entry */ 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate #define CBUFLEN 80 306*bb16350dSlclee static char s[CBUFLEN]; 3077c478bd9Sstevel@tonic-gate 308*bb16350dSlclee static void update_disk_and_exit(boolean_t table_changed); 309*bb16350dSlclee int main(int argc, char *argv[]); 310*bb16350dSlclee static int read_geom(char *sgeom); 311*bb16350dSlclee static void dev_mboot_read(void); 312*bb16350dSlclee static void dev_mboot_write(int sect, char *buff, int bootsiz); 313*bb16350dSlclee static void mboot_read(void); 314*bb16350dSlclee static void fill_patt(void); 315*bb16350dSlclee static void abs_read(void); 316*bb16350dSlclee static void abs_write(void); 317*bb16350dSlclee static void load(int funct, char *file); 3187c478bd9Sstevel@tonic-gate static void Set_Table_CHS_Values(int ti); 319*bb16350dSlclee static int insert_tbl(int id, int act, 320*bb16350dSlclee int bhead, int bsect, int bcyl, 321*bb16350dSlclee int ehead, int esect, int ecyl, 322*bb16350dSlclee int rsect, int numsect); 323*bb16350dSlclee static int verify_tbl(void); 324*bb16350dSlclee static int pars_fdisk(char *line, 325*bb16350dSlclee int *id, int *act, 326*bb16350dSlclee int *bhead, int *bsect, int *bcyl, 327*bb16350dSlclee int *ehead, int *esect, int *ecyl, 328*bb16350dSlclee int *rsect, int *numsect); 329*bb16350dSlclee static int validate_part(int id, int rsect, int numsect); 330*bb16350dSlclee static void stage0(void); 331*bb16350dSlclee static int pcreate(void); 332*bb16350dSlclee static int specify(uchar_t tsystid); 333*bb16350dSlclee static void dispmenu(void); 334*bb16350dSlclee static int pchange(void); 335*bb16350dSlclee static int ppartid(void); 336*bb16350dSlclee static char pdelete(void); 337*bb16350dSlclee static void rm_blanks(char *s); 338*bb16350dSlclee static int getcyl(void); 339*bb16350dSlclee static void disptbl(void); 340*bb16350dSlclee static void print_Table(void); 341*bb16350dSlclee static void copy_Table_to_Old_Table(void); 342*bb16350dSlclee static void nulltbl(void); 343*bb16350dSlclee static void copy_Bootblk_to_Table(void); 344*bb16350dSlclee static void fill_ipart(char *bootptr, struct ipart *partp); 345*bb16350dSlclee #ifdef sparc 346*bb16350dSlclee uchar_t getbyte(char **bp); 347*bb16350dSlclee uint32_t getlong(char **bp); 348*bb16350dSlclee #endif 349*bb16350dSlclee static void copy_Table_to_Bootblk(void); 350*bb16350dSlclee static int TableChanged(void); 351*bb16350dSlclee static void ffile_write(char *file); 352*bb16350dSlclee static void fix_slice(void); 353*bb16350dSlclee static int yesno(void); 354*bb16350dSlclee static int readvtoc(void); 355*bb16350dSlclee static int writevtoc(void); 356*bb16350dSlclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 357*bb16350dSlclee static int clear_efi(void); 358*bb16350dSlclee static void clear_vtoc(int table, int part); 359*bb16350dSlclee static int lecture_and_query(char *warning, char *devname); 360*bb16350dSlclee static void sanity_check_provided_device(char *devname, int fd); 361*bb16350dSlclee static char *get_node(char *devname); 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate static void 3647c478bd9Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed) 3657c478bd9Sstevel@tonic-gate { 3667c478bd9Sstevel@tonic-gate if (table_changed) { 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * Copy the new table back to the sector buffer 3697c478bd9Sstevel@tonic-gate * and write it to disk 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 3727c478bd9Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate /* If the VTOC table is wrong fix it (truncation only) */ 3767c478bd9Sstevel@tonic-gate if (io_adjt) 3777c478bd9Sstevel@tonic-gate fix_slice(); 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate exit(0); 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate /* 3857c478bd9Sstevel@tonic-gate * main 3867c478bd9Sstevel@tonic-gate * Process command-line options. 3877c478bd9Sstevel@tonic-gate */ 388*bb16350dSlclee int 3897c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3907c478bd9Sstevel@tonic-gate { 391*bb16350dSlclee int c, i; 3927c478bd9Sstevel@tonic-gate extern int optind; 3937c478bd9Sstevel@tonic-gate extern char *optarg; 3947c478bd9Sstevel@tonic-gate int errflg = 0; 3957c478bd9Sstevel@tonic-gate int diag_cnt = 0; 3967c478bd9Sstevel@tonic-gate int openmode; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate setbuf(stderr, 0); /* so all output gets out on exit */ 3997c478bd9Sstevel@tonic-gate setbuf(stdout, 0); 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate /* Process the options. */ 4027c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE")) 4037c478bd9Sstevel@tonic-gate != EOF) { 4047c478bd9Sstevel@tonic-gate switch (c) { 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate case 'o': 4077c478bd9Sstevel@tonic-gate io_offset = strtoul(optarg, 0, 0); 4087c478bd9Sstevel@tonic-gate continue; 4097c478bd9Sstevel@tonic-gate case 's': 4107c478bd9Sstevel@tonic-gate io_size = strtoul(optarg, 0, 0); 4117c478bd9Sstevel@tonic-gate continue; 4127c478bd9Sstevel@tonic-gate case 'P': 4137c478bd9Sstevel@tonic-gate diag_cnt++; 4147c478bd9Sstevel@tonic-gate io_patt++; 4157c478bd9Sstevel@tonic-gate io_fatt = optarg; 4167c478bd9Sstevel@tonic-gate continue; 4177c478bd9Sstevel@tonic-gate case 'w': 4187c478bd9Sstevel@tonic-gate diag_cnt++; 4197c478bd9Sstevel@tonic-gate io_wrt++; 4207c478bd9Sstevel@tonic-gate continue; 4217c478bd9Sstevel@tonic-gate case 'r': 4227c478bd9Sstevel@tonic-gate diag_cnt++; 4237c478bd9Sstevel@tonic-gate io_rd++; 4247c478bd9Sstevel@tonic-gate continue; 4257c478bd9Sstevel@tonic-gate case 'd': 4267c478bd9Sstevel@tonic-gate io_debug++; 4277c478bd9Sstevel@tonic-gate continue; 4287c478bd9Sstevel@tonic-gate case 'I': 4297c478bd9Sstevel@tonic-gate io_image++; 4307c478bd9Sstevel@tonic-gate continue; 4317c478bd9Sstevel@tonic-gate case 'R': 4327c478bd9Sstevel@tonic-gate io_readonly++; 4337c478bd9Sstevel@tonic-gate continue; 4347c478bd9Sstevel@tonic-gate case 'S': 4357c478bd9Sstevel@tonic-gate diag_cnt++; 4367c478bd9Sstevel@tonic-gate io_sgeom = optarg; 4377c478bd9Sstevel@tonic-gate continue; 4387c478bd9Sstevel@tonic-gate case 'T': 4397c478bd9Sstevel@tonic-gate io_ADJT++; 440*bb16350dSlclee /* FALLTHRU */ 4417c478bd9Sstevel@tonic-gate case 't': 4427c478bd9Sstevel@tonic-gate io_adjt++; 4437c478bd9Sstevel@tonic-gate continue; 4447c478bd9Sstevel@tonic-gate case 'B': 4457c478bd9Sstevel@tonic-gate io_wholedisk++; 4467c478bd9Sstevel@tonic-gate io_fdisk++; 4477c478bd9Sstevel@tonic-gate continue; 4487c478bd9Sstevel@tonic-gate case 'E': 4497c478bd9Sstevel@tonic-gate io_EFIdisk++; 4507c478bd9Sstevel@tonic-gate io_fdisk++; 4517c478bd9Sstevel@tonic-gate continue; 4527c478bd9Sstevel@tonic-gate case 'g': 4537c478bd9Sstevel@tonic-gate diag_cnt++; 4547c478bd9Sstevel@tonic-gate io_lgeom++; 4557c478bd9Sstevel@tonic-gate continue; 4567c478bd9Sstevel@tonic-gate case 'G': 4577c478bd9Sstevel@tonic-gate diag_cnt++; 4587c478bd9Sstevel@tonic-gate io_pgeom++; 4597c478bd9Sstevel@tonic-gate continue; 4607c478bd9Sstevel@tonic-gate case 'n': 4617c478bd9Sstevel@tonic-gate io_nifdisk++; 4627c478bd9Sstevel@tonic-gate io_fdisk++; 4637c478bd9Sstevel@tonic-gate continue; 4647c478bd9Sstevel@tonic-gate case 'F': 4657c478bd9Sstevel@tonic-gate io_fdisk++; 4667c478bd9Sstevel@tonic-gate io_ffdisk = optarg; 4677c478bd9Sstevel@tonic-gate continue; 4687c478bd9Sstevel@tonic-gate case 'b': 4697c478bd9Sstevel@tonic-gate io_mboot = optarg; 4707c478bd9Sstevel@tonic-gate continue; 4717c478bd9Sstevel@tonic-gate case 'W': 4727c478bd9Sstevel@tonic-gate /* 4737c478bd9Sstevel@tonic-gate * If '-' is the -W argument, then write 4747c478bd9Sstevel@tonic-gate * to standard output, otherwise write 4757c478bd9Sstevel@tonic-gate * to the specified file. 4767c478bd9Sstevel@tonic-gate */ 4777c478bd9Sstevel@tonic-gate if (strncmp(optarg, "-", 1) == 0) 4787c478bd9Sstevel@tonic-gate stdo_flag = 1; 4797c478bd9Sstevel@tonic-gate else 4807c478bd9Sstevel@tonic-gate io_Wfdisk = optarg; 4817c478bd9Sstevel@tonic-gate io_fdisk++; 4827c478bd9Sstevel@tonic-gate continue; 4837c478bd9Sstevel@tonic-gate case 'A': 4847c478bd9Sstevel@tonic-gate io_fdisk++; 4857c478bd9Sstevel@tonic-gate io_Afdisk = optarg; 4867c478bd9Sstevel@tonic-gate continue; 4877c478bd9Sstevel@tonic-gate case 'D': 4887c478bd9Sstevel@tonic-gate io_fdisk++; 4897c478bd9Sstevel@tonic-gate io_Dfdisk = optarg; 4907c478bd9Sstevel@tonic-gate continue; 4917c478bd9Sstevel@tonic-gate case 'h': 492*bb16350dSlclee (void) fprintf(stderr, "%s\n", Usage); 493*bb16350dSlclee (void) fprintf(stderr, "%s\n", Usage1); 4947c478bd9Sstevel@tonic-gate exit(0); 495*bb16350dSlclee /* FALLTHRU */ 4967c478bd9Sstevel@tonic-gate case 'v': 4977c478bd9Sstevel@tonic-gate v_flag = 1; 4987c478bd9Sstevel@tonic-gate continue; 4997c478bd9Sstevel@tonic-gate case '?': 5007c478bd9Sstevel@tonic-gate errflg++; 5017c478bd9Sstevel@tonic-gate break; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate break; 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate if (io_image && io_sgeom && diag_cnt == 1) { 5077c478bd9Sstevel@tonic-gate diag_cnt = 0; 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate /* User option checking */ 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate /* By default, run in interactive mode */ 5137c478bd9Sstevel@tonic-gate if (!io_fdisk && !diag_cnt && !io_nifdisk) { 5147c478bd9Sstevel@tonic-gate io_ifdisk++; 5157c478bd9Sstevel@tonic-gate io_fdisk++; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) { 5187c478bd9Sstevel@tonic-gate errflg++; 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate /* Was any error detected? */ 5227c478bd9Sstevel@tonic-gate if (errflg || argc == optind) { 523*bb16350dSlclee (void) fprintf(stderr, "%s\n", Usage); 524*bb16350dSlclee (void) fprintf(stderr, 5257c478bd9Sstevel@tonic-gate "\nDetailed help is available with the -h option.\n"); 5267c478bd9Sstevel@tonic-gate exit(2); 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* Figure out the correct device node to open */ 5317c478bd9Sstevel@tonic-gate Dfltdev = get_node(argv[optind]); 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if (io_readonly) 5347c478bd9Sstevel@tonic-gate openmode = O_RDONLY; 5357c478bd9Sstevel@tonic-gate else 5367c478bd9Sstevel@tonic-gate openmode = O_RDWR|O_CREAT; 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate if ((Dev = open(Dfltdev, openmode, 0666)) == -1) { 539*bb16350dSlclee (void) fprintf(stderr, 540*bb16350dSlclee "fdisk: Cannot open device %s.\n", 541*bb16350dSlclee Dfltdev); 5427c478bd9Sstevel@tonic-gate exit(1); 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate /* Get the disk geometry */ 5467c478bd9Sstevel@tonic-gate if (!io_image) { 5477c478bd9Sstevel@tonic-gate /* Get disk's HBA (virtual) geometry */ 5487c478bd9Sstevel@tonic-gate errno = 0; 5497c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) { 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * If ioctl isn't implemented on this platform, then 5537c478bd9Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v), 5547c478bd9Sstevel@tonic-gate * otherwise use the virtual geometry. 5557c478bd9Sstevel@tonic-gate */ 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (errno == ENOTTY) { 5587c478bd9Sstevel@tonic-gate v_flag = 0; 5597c478bd9Sstevel@tonic-gate no_virtgeom_ioctl = 1; 5607c478bd9Sstevel@tonic-gate } else if (errno == EINVAL) { 5617c478bd9Sstevel@tonic-gate /* 5627c478bd9Sstevel@tonic-gate * This means that the ioctl exists, but 5637c478bd9Sstevel@tonic-gate * is invalid for this disk, meaning the 5647c478bd9Sstevel@tonic-gate * disk doesn't have an HBA geometry 5657c478bd9Sstevel@tonic-gate * (like, say, it's larger than 8GB). 5667c478bd9Sstevel@tonic-gate */ 5677c478bd9Sstevel@tonic-gate v_flag = 0; 5687c478bd9Sstevel@tonic-gate hba_Numcyl = hba_heads = hba_sectors = 0; 5697c478bd9Sstevel@tonic-gate } else { 5707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5717c478bd9Sstevel@tonic-gate "%s: Cannot get virtual disk geometry.\n", 5727c478bd9Sstevel@tonic-gate argv[optind]); 5737c478bd9Sstevel@tonic-gate exit(1); 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate } else { 5767c478bd9Sstevel@tonic-gate /* save virtual geometry values obtained by ioctl */ 5777c478bd9Sstevel@tonic-gate hba_Numcyl = disk_geom.dkg_ncyl; 5787c478bd9Sstevel@tonic-gate hba_heads = disk_geom.dkg_nhead; 5797c478bd9Sstevel@tonic-gate hba_sectors = disk_geom.dkg_nsect; 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate errno = 0; 5837c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 5847c478bd9Sstevel@tonic-gate if (errno == ENOTTY) { 5857c478bd9Sstevel@tonic-gate no_physgeom_ioctl = 1; 5867c478bd9Sstevel@tonic-gate } else { 5877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5887c478bd9Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 5897c478bd9Sstevel@tonic-gate argv[optind]); 5907c478bd9Sstevel@tonic-gate exit(1); 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate /* 5957c478bd9Sstevel@tonic-gate * Call DKIOCGGEOM if the ioctls for physical and virtual 5967c478bd9Sstevel@tonic-gate * geometry fail. Get both from this generic call. 5977c478bd9Sstevel@tonic-gate */ 5987c478bd9Sstevel@tonic-gate if (no_virtgeom_ioctl && no_physgeom_ioctl) { 5997c478bd9Sstevel@tonic-gate errno = 0; 6007c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6027c478bd9Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6037c478bd9Sstevel@tonic-gate argv[optind]); 6047c478bd9Sstevel@tonic-gate exit(1); 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6097c478bd9Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6107c478bd9Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 6117c478bd9Sstevel@tonic-gate sectsiz = 512; 6127c478bd9Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * if hba geometry was not set by DKIOC_VIRTGEOM 6167c478bd9Sstevel@tonic-gate * or we got an invalid hba geometry 6177c478bd9Sstevel@tonic-gate * then set hba geometry based on max values 6187c478bd9Sstevel@tonic-gate */ 6197c478bd9Sstevel@tonic-gate if (no_virtgeom_ioctl || 620*bb16350dSlclee disk_geom.dkg_ncyl == 0 || 621*bb16350dSlclee disk_geom.dkg_nhead == 0 || 622*bb16350dSlclee disk_geom.dkg_nsect == 0 || 6237c478bd9Sstevel@tonic-gate disk_geom.dkg_ncyl > MAX_CYL || 6247c478bd9Sstevel@tonic-gate disk_geom.dkg_nhead > MAX_HEAD || 6257c478bd9Sstevel@tonic-gate disk_geom.dkg_nsect > MAX_SECT) { 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate /* 6287c478bd9Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v) 6297c478bd9Sstevel@tonic-gate */ 6307c478bd9Sstevel@tonic-gate v_flag = 0; 6317c478bd9Sstevel@tonic-gate hba_sectors = MAX_SECT; 6327c478bd9Sstevel@tonic-gate hba_heads = MAX_HEAD + 1; 6337c478bd9Sstevel@tonic-gate hba_Numcyl = (Numcyl * heads * sectors) / 6347c478bd9Sstevel@tonic-gate (hba_sectors * hba_heads); 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate if (io_debug) { 638*bb16350dSlclee (void) fprintf(stderr, "Physical Geometry:\n"); 639*bb16350dSlclee (void) fprintf(stderr, 6407c478bd9Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6417c478bd9Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6427c478bd9Sstevel@tonic-gate Numcyl, 6437c478bd9Sstevel@tonic-gate heads, 6447c478bd9Sstevel@tonic-gate sectors, 6457c478bd9Sstevel@tonic-gate sectsiz, 6467c478bd9Sstevel@tonic-gate Numcyl * heads * sectors, 6477c478bd9Sstevel@tonic-gate (Numcyl * heads * sectors * sectsiz) / 1048576); 648*bb16350dSlclee (void) fprintf(stderr, "Virtual (HBA) Geometry:\n"); 649*bb16350dSlclee (void) fprintf(stderr, 6507c478bd9Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6517c478bd9Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6527c478bd9Sstevel@tonic-gate hba_Numcyl, 6537c478bd9Sstevel@tonic-gate hba_heads, 6547c478bd9Sstevel@tonic-gate hba_sectors, 6557c478bd9Sstevel@tonic-gate sectsiz, 6567c478bd9Sstevel@tonic-gate hba_Numcyl * hba_heads * hba_sectors, 657*bb16350dSlclee (hba_Numcyl * hba_heads * hba_sectors * sectsiz) / 658*bb16350dSlclee 1048576); 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate /* If user has requested a geometry report just do it and exit */ 6637c478bd9Sstevel@tonic-gate if (io_lgeom) { 6647c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6667c478bd9Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6677c478bd9Sstevel@tonic-gate argv[optind]); 6687c478bd9Sstevel@tonic-gate exit(1); 6697c478bd9Sstevel@tonic-gate } 6707c478bd9Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6717c478bd9Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6727c478bd9Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 6737c478bd9Sstevel@tonic-gate sectsiz = 512; 6747c478bd9Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 675*bb16350dSlclee (void) printf("* Label geometry for device %s\n", Dfltdev); 676*bb16350dSlclee (void) printf( 677*bb16350dSlclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 6787c478bd9Sstevel@tonic-gate " SECSIZ\n"); 679*bb16350dSlclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 6807c478bd9Sstevel@tonic-gate Numcyl, 6817c478bd9Sstevel@tonic-gate disk_geom.dkg_ncyl, 6827c478bd9Sstevel@tonic-gate disk_geom.dkg_acyl, 6837c478bd9Sstevel@tonic-gate disk_geom.dkg_bcyl, 6847c478bd9Sstevel@tonic-gate heads, 6857c478bd9Sstevel@tonic-gate sectors, 6867c478bd9Sstevel@tonic-gate sectsiz); 6877c478bd9Sstevel@tonic-gate exit(0); 6887c478bd9Sstevel@tonic-gate } else if (io_pgeom) { 6897c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 6907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6917c478bd9Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 6927c478bd9Sstevel@tonic-gate argv[optind]); 6937c478bd9Sstevel@tonic-gate exit(1); 6947c478bd9Sstevel@tonic-gate } 695*bb16350dSlclee (void) printf("* Physical geometry for device %s\n", Dfltdev); 696*bb16350dSlclee (void) printf( 697*bb16350dSlclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 6987c478bd9Sstevel@tonic-gate " SECSIZ\n"); 699*bb16350dSlclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 7007c478bd9Sstevel@tonic-gate disk_geom.dkg_pcyl, 7017c478bd9Sstevel@tonic-gate disk_geom.dkg_ncyl, 7027c478bd9Sstevel@tonic-gate disk_geom.dkg_acyl, 7037c478bd9Sstevel@tonic-gate disk_geom.dkg_bcyl, 7047c478bd9Sstevel@tonic-gate disk_geom.dkg_nhead, 7057c478bd9Sstevel@tonic-gate disk_geom.dkg_nsect, 7067c478bd9Sstevel@tonic-gate sectsiz); 7077c478bd9Sstevel@tonic-gate exit(0); 7087c478bd9Sstevel@tonic-gate } else if (io_sgeom) { 7097c478bd9Sstevel@tonic-gate if (read_geom(io_sgeom)) { 7107c478bd9Sstevel@tonic-gate exit(1); 7117c478bd9Sstevel@tonic-gate } else if (!io_image) { 7127c478bd9Sstevel@tonic-gate exit(0); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate /* Allocate memory to hold three complete sectors */ 7177c478bd9Sstevel@tonic-gate Bootsect = (char *)malloc(3 * sectsiz); 7187c478bd9Sstevel@tonic-gate if (Bootsect == NULL) { 719*bb16350dSlclee (void) fprintf(stderr, 7207c478bd9Sstevel@tonic-gate "fdisk: Unable to obtain enough buffer memory" 7217c478bd9Sstevel@tonic-gate " (%d bytes).\n", 7227c478bd9Sstevel@tonic-gate 3 * sectsiz); 7237c478bd9Sstevel@tonic-gate exit(1); 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate Nullsect = Bootsect + sectsiz; 7277c478bd9Sstevel@tonic-gate /* Zero out the "NULL" sector */ 7287c478bd9Sstevel@tonic-gate for (i = 0; i < sectsiz; i++) { 7297c478bd9Sstevel@tonic-gate Nullsect[i] = 0; 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate /* Find out what the user wants done */ 7337c478bd9Sstevel@tonic-gate if (io_rd) { /* abs disk read */ 7347c478bd9Sstevel@tonic-gate abs_read(); /* will not return */ 7357c478bd9Sstevel@tonic-gate } else if (io_wrt && !io_readonly) { 7367c478bd9Sstevel@tonic-gate abs_write(); /* will not return */ 7377c478bd9Sstevel@tonic-gate } else if (io_patt && !io_readonly) { 7387c478bd9Sstevel@tonic-gate fill_patt(); /* will not return */ 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate /* This is the fdisk edit, the real reason for the program. */ 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate sanity_check_provided_device(Dfltdev, Dev); 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate /* Get the new BOOT program in case we write a new fdisk table */ 7477c478bd9Sstevel@tonic-gate mboot_read(); 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate /* Read from disk master boot */ 7507c478bd9Sstevel@tonic-gate dev_mboot_read(); 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * Verify and copy the device's fdisk table. This will be used 7547c478bd9Sstevel@tonic-gate * as the prototype mboot if the device's mboot looks invalid. 7557c478bd9Sstevel@tonic-gate */ 7567c478bd9Sstevel@tonic-gate Bootblk = (struct mboot *)Bootsect; 7577c478bd9Sstevel@tonic-gate copy_Bootblk_to_Table(); 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate /* save away a copy of Table in Old_Table for sensing changes */ 7607c478bd9Sstevel@tonic-gate copy_Table_to_Old_Table(); 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate /* Load fdisk table from specified file (-F fdisk_file) */ 7637c478bd9Sstevel@tonic-gate if (io_ffdisk) { 7647c478bd9Sstevel@tonic-gate /* Load and verify user-specified table parameters */ 7657c478bd9Sstevel@tonic-gate load(LOADFILE, io_ffdisk); 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate /* Does user want to delete or add an entry? */ 7697c478bd9Sstevel@tonic-gate if (io_Dfdisk) { 7707c478bd9Sstevel@tonic-gate load(LOADDEL, io_Dfdisk); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate if (io_Afdisk) { 7737c478bd9Sstevel@tonic-gate load(LOADADD, io_Afdisk); 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) { 7777c478bd9Sstevel@tonic-gate /* Check if there is no fdisk table */ 7787c478bd9Sstevel@tonic-gate if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) { 7797c478bd9Sstevel@tonic-gate if (io_ifdisk && !io_wholedisk && !io_EFIdisk) { 780*bb16350dSlclee (void) printf( 781*bb16350dSlclee "No fdisk table exists. The default" 782*bb16350dSlclee " partition for the disk is:\n\n" 783*bb16350dSlclee " a 100%% \"SOLARIS System\" " 784*bb16350dSlclee "partition\n\n" 785*bb16350dSlclee "Type \"y\" to accept the default " 7867c478bd9Sstevel@tonic-gate "partition, otherwise type \"n\" to " 7877c478bd9Sstevel@tonic-gate "edit the\n partition table.\n"); 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* Edit the partition table as directed */ 7917c478bd9Sstevel@tonic-gate if (io_wholedisk ||(io_ifdisk && yesno())) { 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate /* Default scenario */ 7947c478bd9Sstevel@tonic-gate nulltbl(); 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate /* now set up UNIX System partition */ 7977c478bd9Sstevel@tonic-gate Table[0].bootid = ACTIVE; 7987c478bd9Sstevel@tonic-gate Table[0].relsect = lel(heads * sectors); 7997c478bd9Sstevel@tonic-gate Table[0].numsect = lel((long)((Numcyl - 1) * 8007c478bd9Sstevel@tonic-gate heads * sectors)); 8017c478bd9Sstevel@tonic-gate Table[0].systid = SUNIXOS2; /* Solaris */ 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate /* calculate CHS values for table entry 0 */ 8047c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(0); 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8077c478bd9Sstevel@tonic-gate } else if (io_EFIdisk) { 8087c478bd9Sstevel@tonic-gate /* create an EFI partition for the whole disk */ 8097c478bd9Sstevel@tonic-gate nulltbl(); 8107c478bd9Sstevel@tonic-gate i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1, 8117c478bd9Sstevel@tonic-gate (Numcyl * heads * sectors) - 1); 8127c478bd9Sstevel@tonic-gate if (i != 0) { 813*bb16350dSlclee (void) fprintf(stderr, 814*bb16350dSlclee "Error creating EFI partition\n"); 8157c478bd9Sstevel@tonic-gate exit(1); 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8187c478bd9Sstevel@tonic-gate } 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate /* Display complete fdisk table entries for debugging purposes */ 8237c478bd9Sstevel@tonic-gate if (io_debug) { 824*bb16350dSlclee (void) fprintf(stderr, "Partition Table Entry Values:\n"); 8257c478bd9Sstevel@tonic-gate print_Table(); 8267c478bd9Sstevel@tonic-gate if (io_ifdisk) { 827*bb16350dSlclee (void) fprintf(stderr, "\n"); 828*bb16350dSlclee (void) fprintf(stderr, "Press Enter to continue.\n"); 829*bb16350dSlclee (void) gets(s); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate /* Interactive fdisk mode */ 8347c478bd9Sstevel@tonic-gate if (io_ifdisk) { 835*bb16350dSlclee (void) printf(CLR_SCR); 8367c478bd9Sstevel@tonic-gate disptbl(); 837*bb16350dSlclee for (;;) { 838*bb16350dSlclee stage0(); 8397c478bd9Sstevel@tonic-gate copy_Bootblk_to_Table(); 8407c478bd9Sstevel@tonic-gate disptbl(); 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate /* If user wants to write the table to a file, do it */ 8457c478bd9Sstevel@tonic-gate if (io_Wfdisk) 8467c478bd9Sstevel@tonic-gate ffile_write(io_Wfdisk); 8477c478bd9Sstevel@tonic-gate else if (stdo_flag) 8487c478bd9Sstevel@tonic-gate ffile_write((char *)stdout); 8497c478bd9Sstevel@tonic-gate 8507c478bd9Sstevel@tonic-gate update_disk_and_exit(TableChanged() == 1); 851*bb16350dSlclee return (0); 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate /* 8557c478bd9Sstevel@tonic-gate * read_geom 8567c478bd9Sstevel@tonic-gate * Read geometry from specified file (-S). 8577c478bd9Sstevel@tonic-gate */ 8587c478bd9Sstevel@tonic-gate 859*bb16350dSlclee static int 860*bb16350dSlclee read_geom(char *sgeom) 8617c478bd9Sstevel@tonic-gate { 8627c478bd9Sstevel@tonic-gate char line[256]; 8637c478bd9Sstevel@tonic-gate FILE *fp; 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate /* open the prototype file */ 8667c478bd9Sstevel@tonic-gate if ((fp = fopen(sgeom, "r")) == NULL) { 8677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot open file %s.\n", 8687c478bd9Sstevel@tonic-gate io_sgeom); 8697c478bd9Sstevel@tonic-gate return (1); 8707c478bd9Sstevel@tonic-gate } 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate /* Read a line from the file */ 8737c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 8747c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 8757c478bd9Sstevel@tonic-gate continue; 8767c478bd9Sstevel@tonic-gate else { 8777c478bd9Sstevel@tonic-gate line[strlen(line)] = '\0'; 878*bb16350dSlclee if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d", 8797c478bd9Sstevel@tonic-gate &disk_geom.dkg_pcyl, 8807c478bd9Sstevel@tonic-gate &disk_geom.dkg_ncyl, 8817c478bd9Sstevel@tonic-gate &disk_geom.dkg_acyl, 8827c478bd9Sstevel@tonic-gate &disk_geom.dkg_bcyl, 8837c478bd9Sstevel@tonic-gate &disk_geom.dkg_nhead, 8847c478bd9Sstevel@tonic-gate &disk_geom.dkg_nsect, 8857c478bd9Sstevel@tonic-gate §siz) != 7) { 8867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8877c478bd9Sstevel@tonic-gate "Syntax error:\n \"%s\".\n", 8887c478bd9Sstevel@tonic-gate line); 8897c478bd9Sstevel@tonic-gate return (1); 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate break; 8927c478bd9Sstevel@tonic-gate } /* else */ 8937c478bd9Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate if (!io_image) { 8967c478bd9Sstevel@tonic-gate if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) { 8977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8987c478bd9Sstevel@tonic-gate "fdisk: Cannot set label geometry.\n"); 8997c478bd9Sstevel@tonic-gate return (1); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate } else { 9027c478bd9Sstevel@tonic-gate Numcyl = hba_Numcyl = disk_geom.dkg_ncyl; 9037c478bd9Sstevel@tonic-gate heads = hba_heads = disk_geom.dkg_nhead; 9047c478bd9Sstevel@tonic-gate sectors = hba_sectors = disk_geom.dkg_nsect; 9057c478bd9Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate 908*bb16350dSlclee (void) fclose(fp); 9097c478bd9Sstevel@tonic-gate return (0); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * dev_mboot_read 9147c478bd9Sstevel@tonic-gate * Read the master boot sector from the device. 9157c478bd9Sstevel@tonic-gate */ 916*bb16350dSlclee static void 917*bb16350dSlclee dev_mboot_read(void) 9187c478bd9Sstevel@tonic-gate { 9197c478bd9Sstevel@tonic-gate if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) { 9207c478bd9Sstevel@tonic-gate perror("Error in ioctl DKIOCGMBOOT"); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate if (errno == 0) 9237c478bd9Sstevel@tonic-gate return; 9247c478bd9Sstevel@tonic-gate if (lseek(Dev, 0, SEEK_SET) == -1) { 925*bb16350dSlclee (void) fprintf(stderr, 9267c478bd9Sstevel@tonic-gate "fdisk: Error seeking to partition table on %s.\n", 9277c478bd9Sstevel@tonic-gate Dfltdev); 9287c478bd9Sstevel@tonic-gate if (!io_image) 9297c478bd9Sstevel@tonic-gate exit(1); 9307c478bd9Sstevel@tonic-gate } 9317c478bd9Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 932*bb16350dSlclee (void) fprintf(stderr, 9337c478bd9Sstevel@tonic-gate "fdisk: Error reading partition table from %s.\n", 9347c478bd9Sstevel@tonic-gate Dfltdev); 9357c478bd9Sstevel@tonic-gate if (!io_image) 9367c478bd9Sstevel@tonic-gate exit(1); 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate /* 9417c478bd9Sstevel@tonic-gate * dev_mboot_write 9427c478bd9Sstevel@tonic-gate * Write the master boot sector to the device. 9437c478bd9Sstevel@tonic-gate */ 944*bb16350dSlclee static void 9457c478bd9Sstevel@tonic-gate dev_mboot_write(int sect, char *buff, int bootsiz) 9467c478bd9Sstevel@tonic-gate { 9477c478bd9Sstevel@tonic-gate int new_pt, old_pt, error; 9487c478bd9Sstevel@tonic-gate int clr_efi = -1, old_solaris = -1, new_solaris = -1; 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate if (io_readonly) 951*bb16350dSlclee return; 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate if (io_debug) { 954*bb16350dSlclee (void) fprintf(stderr, "About to write fdisk table:\n"); 9557c478bd9Sstevel@tonic-gate print_Table(); 9567c478bd9Sstevel@tonic-gate if (io_ifdisk) { 957*bb16350dSlclee (void) fprintf(stderr, "Press Enter to continue.\n"); 958*bb16350dSlclee (void) gets(s); 9597c478bd9Sstevel@tonic-gate } 9607c478bd9Sstevel@tonic-gate } 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate /* see if the old table had EFI or Solaris partitions */ 9637c478bd9Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 9647c478bd9Sstevel@tonic-gate if (Old_Table[old_pt].systid == SUNIXOS || 9657c478bd9Sstevel@tonic-gate Old_Table[old_pt].systid == SUNIXOS2) { 9667c478bd9Sstevel@tonic-gate old_solaris = old_pt; 9677c478bd9Sstevel@tonic-gate } else if (Old_Table[old_pt].systid == EFI_PMBR) { 9687c478bd9Sstevel@tonic-gate clr_efi = old_pt; 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate /* look to see if Solaris partition changed in relsect/numsect */ 9737c478bd9Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate /* 9767c478bd9Sstevel@tonic-gate * if this is not a Solaris partition, ignore it 9777c478bd9Sstevel@tonic-gate */ 9787c478bd9Sstevel@tonic-gate if (Table[new_pt].systid != SUNIXOS && 9797c478bd9Sstevel@tonic-gate Table[new_pt].systid != SUNIXOS2) 9807c478bd9Sstevel@tonic-gate continue; 9817c478bd9Sstevel@tonic-gate 9827c478bd9Sstevel@tonic-gate /* 9837c478bd9Sstevel@tonic-gate * if there was no previous Solaris partition 9847c478bd9Sstevel@tonic-gate * or if the old partition was in a different place 9857c478bd9Sstevel@tonic-gate * or if the old partition was a different size 9867c478bd9Sstevel@tonic-gate * then this must be a new Solaris partition 9877c478bd9Sstevel@tonic-gate */ 9887c478bd9Sstevel@tonic-gate if (old_solaris == -1 || 9897c478bd9Sstevel@tonic-gate Old_Table[old_solaris].relsect != Table[new_pt].relsect || 9907c478bd9Sstevel@tonic-gate Old_Table[old_solaris].numsect != Table[new_pt].numsect) { 9917c478bd9Sstevel@tonic-gate new_solaris = new_pt; 9927c478bd9Sstevel@tonic-gate break; 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate /* look to see if a EFI partition changed in relsect/numsect */ 9977c478bd9Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 9987c478bd9Sstevel@tonic-gate if (Table[new_pt].systid != EFI_PMBR) 9997c478bd9Sstevel@tonic-gate continue; 10007c478bd9Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 10017c478bd9Sstevel@tonic-gate if ((Old_Table[old_pt].systid == Table[new_pt].systid) && 10027c478bd9Sstevel@tonic-gate (Old_Table[old_pt].relsect == Table[new_pt].relsect) && 10037c478bd9Sstevel@tonic-gate (Old_Table[old_pt].numsect == Table[new_pt].numsect)) 10047c478bd9Sstevel@tonic-gate break; 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate /* 10087c478bd9Sstevel@tonic-gate * if EFI partition changed, set the flag to clear 10097c478bd9Sstevel@tonic-gate * the EFI GPT 10107c478bd9Sstevel@tonic-gate */ 10117c478bd9Sstevel@tonic-gate if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) { 10127c478bd9Sstevel@tonic-gate clr_efi = 0; 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate break; 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate /* clear labels if necessary */ 10187c478bd9Sstevel@tonic-gate if (clr_efi >= 0) { 10197c478bd9Sstevel@tonic-gate if (io_debug) { 1020*bb16350dSlclee (void) fprintf(stderr, "Clearing EFI labels\n"); 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate if ((error = clear_efi()) != 0) { 10237c478bd9Sstevel@tonic-gate if (io_debug) { 1024*bb16350dSlclee (void) fprintf(stderr, 1025*bb16350dSlclee "\tError %d clearing EFI labels" 10267c478bd9Sstevel@tonic-gate " (probably no EFI labels exist)\n", 10277c478bd9Sstevel@tonic-gate error); 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate } 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate 10327c478bd9Sstevel@tonic-gate if (new_solaris >= 0) { 10337c478bd9Sstevel@tonic-gate if (io_debug) { 1034*bb16350dSlclee (void) fprintf(stderr, "Clearing VTOC labels from NEW" 10357c478bd9Sstevel@tonic-gate " table\n"); 10367c478bd9Sstevel@tonic-gate } 10377c478bd9Sstevel@tonic-gate clear_vtoc(NEW, new_solaris); 10387c478bd9Sstevel@tonic-gate } 10397c478bd9Sstevel@tonic-gate 10407c478bd9Sstevel@tonic-gate if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) { 1041*bb16350dSlclee (void) fprintf(stderr, 10427c478bd9Sstevel@tonic-gate "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n", 10437c478bd9Sstevel@tonic-gate Dfltdev); 10447c478bd9Sstevel@tonic-gate } 10457c478bd9Sstevel@tonic-gate if (errno == 0) 10467c478bd9Sstevel@tonic-gate return; 10477c478bd9Sstevel@tonic-gate 10487c478bd9Sstevel@tonic-gate /* write to disk drive */ 10497c478bd9Sstevel@tonic-gate if (lseek(Dev, sect, SEEK_SET) == -1) { 1050*bb16350dSlclee (void) fprintf(stderr, 10517c478bd9Sstevel@tonic-gate "fdisk: Error seeking to master boot record on %s.\n", 10527c478bd9Sstevel@tonic-gate Dfltdev); 10537c478bd9Sstevel@tonic-gate exit(1); 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate if (write(Dev, buff, bootsiz) != bootsiz) { 1056*bb16350dSlclee (void) fprintf(stderr, 10577c478bd9Sstevel@tonic-gate "fdisk: Error writing master boot record to %s.\n", 10587c478bd9Sstevel@tonic-gate Dfltdev); 10597c478bd9Sstevel@tonic-gate exit(1); 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate } 10627c478bd9Sstevel@tonic-gate 10637c478bd9Sstevel@tonic-gate /* 10647c478bd9Sstevel@tonic-gate * mboot_read 10657c478bd9Sstevel@tonic-gate * Read the prototype boot records from the files. 10667c478bd9Sstevel@tonic-gate */ 1067*bb16350dSlclee static void 1068*bb16350dSlclee mboot_read(void) 10697c478bd9Sstevel@tonic-gate { 10707c478bd9Sstevel@tonic-gate int mDev, i; 10717c478bd9Sstevel@tonic-gate struct ipart *part; 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate #if defined(i386) || defined(sparc) 10747c478bd9Sstevel@tonic-gate /* 10757c478bd9Sstevel@tonic-gate * If the master boot file hasn't been specified, use the 10767c478bd9Sstevel@tonic-gate * implementation architecture name to generate the default one. 10777c478bd9Sstevel@tonic-gate */ 10787c478bd9Sstevel@tonic-gate if (io_mboot == (char *)0) { 10797c478bd9Sstevel@tonic-gate /* 10807c478bd9Sstevel@tonic-gate * Bug ID 1249035: 10817c478bd9Sstevel@tonic-gate * The mboot file must be delivered on all platforms 10827c478bd9Sstevel@tonic-gate * and installed in a non-platform-dependent 10837c478bd9Sstevel@tonic-gate * directory; i.e., /usr/lib/fs/ufs. 10847c478bd9Sstevel@tonic-gate */ 10857c478bd9Sstevel@tonic-gate io_mboot = "/usr/lib/fs/ufs/mboot"; 10867c478bd9Sstevel@tonic-gate } 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate /* First read in the master boot record */ 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate /* Open the master boot proto file */ 10917c478bd9Sstevel@tonic-gate if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) { 1092*bb16350dSlclee (void) fprintf(stderr, 10937c478bd9Sstevel@tonic-gate "fdisk: Cannot open master boot file %s.\n", 10947c478bd9Sstevel@tonic-gate io_mboot); 10957c478bd9Sstevel@tonic-gate exit(1); 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate /* Read the master boot program */ 10997c478bd9Sstevel@tonic-gate if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof 11007c478bd9Sstevel@tonic-gate (struct mboot)) { 1101*bb16350dSlclee (void) fprintf(stderr, 11027c478bd9Sstevel@tonic-gate "fdisk: Cannot read master boot file %s.\n", 11037c478bd9Sstevel@tonic-gate io_mboot); 11047c478bd9Sstevel@tonic-gate exit(1); 11057c478bd9Sstevel@tonic-gate } 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate /* Is this really a master boot record? */ 11087c478bd9Sstevel@tonic-gate if (les(BootCod.signature) != MBB_MAGIC) { 1109*bb16350dSlclee (void) fprintf(stderr, 11107c478bd9Sstevel@tonic-gate "fdisk: Invalid master boot file %s.\n", io_mboot); 1111*bb16350dSlclee (void) fprintf(stderr, 1112*bb16350dSlclee "Bad magic number: is %x, but should be %x.\n", 11137c478bd9Sstevel@tonic-gate les(BootCod.signature), MBB_MAGIC); 11147c478bd9Sstevel@tonic-gate exit(1); 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate 1117*bb16350dSlclee (void) close(mDev); 11187c478bd9Sstevel@tonic-gate #else 11197c478bd9Sstevel@tonic-gate #error fdisk needs to be ported to new architecture 11207c478bd9Sstevel@tonic-gate #endif 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate /* Zero out the partitions part of this record */ 11237c478bd9Sstevel@tonic-gate part = (struct ipart *)BootCod.parts; 11247c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++, part++) { 1125*bb16350dSlclee (void) memset(part, 0, sizeof (struct ipart)); 11267c478bd9Sstevel@tonic-gate } 11277c478bd9Sstevel@tonic-gate 11287c478bd9Sstevel@tonic-gate } 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate /* 11317c478bd9Sstevel@tonic-gate * fill_patt 11327c478bd9Sstevel@tonic-gate * Fill the disk with user/sector number pattern. 11337c478bd9Sstevel@tonic-gate */ 1134*bb16350dSlclee static void 1135*bb16350dSlclee fill_patt(void) 11367c478bd9Sstevel@tonic-gate { 1137*bb16350dSlclee int *buff_ptr, i; 11387c478bd9Sstevel@tonic-gate int io_fpatt = 0; 11397c478bd9Sstevel@tonic-gate int io_ipatt = 0; 11407c478bd9Sstevel@tonic-gate 11417c478bd9Sstevel@tonic-gate if (strncmp(io_fatt, "#", 1) != 0) { 11427c478bd9Sstevel@tonic-gate io_fpatt++; 11437c478bd9Sstevel@tonic-gate io_ipatt = strtoul(io_fatt, 0, 0); 11447c478bd9Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 11457c478bd9Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 11467c478bd9Sstevel@tonic-gate *buff_ptr = io_ipatt; 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate /* 11507c478bd9Sstevel@tonic-gate * Fill disk with pattern based on block number. 11517c478bd9Sstevel@tonic-gate * Write to the disk at absolute relative block io_offset 11527c478bd9Sstevel@tonic-gate * for io_size blocks. 11537c478bd9Sstevel@tonic-gate */ 11547c478bd9Sstevel@tonic-gate while (io_size--) { 11557c478bd9Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 11567c478bd9Sstevel@tonic-gate if (!io_fpatt) { 11577c478bd9Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 11587c478bd9Sstevel@tonic-gate *buff_ptr = io_offset; 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate /* Write the data to disk */ 11617c478bd9Sstevel@tonic-gate if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) { 1162*bb16350dSlclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 11637c478bd9Sstevel@tonic-gate Dfltdev); 11647c478bd9Sstevel@tonic-gate exit(1); 11657c478bd9Sstevel@tonic-gate } 11667c478bd9Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1167*bb16350dSlclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 11687c478bd9Sstevel@tonic-gate Dfltdev); 11697c478bd9Sstevel@tonic-gate exit(1); 11707c478bd9Sstevel@tonic-gate } 11717c478bd9Sstevel@tonic-gate } /* while (--io_size); */ 11727c478bd9Sstevel@tonic-gate } 11737c478bd9Sstevel@tonic-gate 11747c478bd9Sstevel@tonic-gate /* 11757c478bd9Sstevel@tonic-gate * abs_read 11767c478bd9Sstevel@tonic-gate * Read from the disk at absolute relative block io_offset for 11777c478bd9Sstevel@tonic-gate * io_size blocks. Write the data to standard ouput (-r). 11787c478bd9Sstevel@tonic-gate */ 1179*bb16350dSlclee static void 1180*bb16350dSlclee abs_read(void) 1181*bb16350dSlclee { 11827c478bd9Sstevel@tonic-gate int c; 11837c478bd9Sstevel@tonic-gate 11847c478bd9Sstevel@tonic-gate while (io_size--) { 11857c478bd9Sstevel@tonic-gate if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) { 1186*bb16350dSlclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 11877c478bd9Sstevel@tonic-gate Dfltdev); 11887c478bd9Sstevel@tonic-gate exit(1); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 1191*bb16350dSlclee (void) fprintf(stderr, "fdisk: Error reading %s.\n", 11927c478bd9Sstevel@tonic-gate Dfltdev); 11937c478bd9Sstevel@tonic-gate exit(1); 11947c478bd9Sstevel@tonic-gate } 11957c478bd9Sstevel@tonic-gate 11967c478bd9Sstevel@tonic-gate /* Write to standard ouptut */ 1197*bb16350dSlclee if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) { 11987c478bd9Sstevel@tonic-gate if (c >= 0) { 11997c478bd9Sstevel@tonic-gate if (io_debug) 1200*bb16350dSlclee (void) fprintf(stderr, 12017c478bd9Sstevel@tonic-gate "fdisk: Output warning: %d of %d" 12027c478bd9Sstevel@tonic-gate " characters written.\n", 12037c478bd9Sstevel@tonic-gate c, sectsiz); 12047c478bd9Sstevel@tonic-gate exit(2); 12057c478bd9Sstevel@tonic-gate } else { 12067c478bd9Sstevel@tonic-gate perror("write error on output file."); 12077c478bd9Sstevel@tonic-gate exit(2); 12087c478bd9Sstevel@tonic-gate } 12097c478bd9Sstevel@tonic-gate } /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */ 12107c478bd9Sstevel@tonic-gate /* != sectsiz) */ 12117c478bd9Sstevel@tonic-gate } /* while (--io_size); */ 12127c478bd9Sstevel@tonic-gate exit(0); 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate 12157c478bd9Sstevel@tonic-gate /* 12167c478bd9Sstevel@tonic-gate * abs_write 12177c478bd9Sstevel@tonic-gate * Read the data from standard input. Write to the disk at 12187c478bd9Sstevel@tonic-gate * absolute relative block io_offset for io_size blocks (-w). 12197c478bd9Sstevel@tonic-gate */ 1220*bb16350dSlclee static void 1221*bb16350dSlclee abs_write(void) 12227c478bd9Sstevel@tonic-gate { 12237c478bd9Sstevel@tonic-gate int c, i; 12247c478bd9Sstevel@tonic-gate 12257c478bd9Sstevel@tonic-gate while (io_size--) { 12267c478bd9Sstevel@tonic-gate int part_exit = 0; 12277c478bd9Sstevel@tonic-gate /* Read from standard input */ 12287c478bd9Sstevel@tonic-gate if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12297c478bd9Sstevel@tonic-gate if (c >= 0) { 12307c478bd9Sstevel@tonic-gate if (io_debug) 1231*bb16350dSlclee (void) fprintf(stderr, 12327c478bd9Sstevel@tonic-gate "fdisk: WARNING: Incomplete read (%d of" 12337c478bd9Sstevel@tonic-gate " %d characters read) on input file.\n", 12347c478bd9Sstevel@tonic-gate c, sectsiz); 12357c478bd9Sstevel@tonic-gate /* Fill pattern to mark partial sector in buf */ 12367c478bd9Sstevel@tonic-gate for (i = c; i < sectsiz; ) { 12377c478bd9Sstevel@tonic-gate Bootsect[i++] = 0x41; 12387c478bd9Sstevel@tonic-gate Bootsect[i++] = 0x62; 12397c478bd9Sstevel@tonic-gate Bootsect[i++] = 0x65; 12407c478bd9Sstevel@tonic-gate Bootsect[i++] = 0; 12417c478bd9Sstevel@tonic-gate } 12427c478bd9Sstevel@tonic-gate part_exit++; 12437c478bd9Sstevel@tonic-gate } else { 12447c478bd9Sstevel@tonic-gate perror("read error on input file."); 12457c478bd9Sstevel@tonic-gate exit(2); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate /* Write to disk drive */ 12507c478bd9Sstevel@tonic-gate if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) { 1251*bb16350dSlclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12527c478bd9Sstevel@tonic-gate Dfltdev); 12537c478bd9Sstevel@tonic-gate exit(1); 12547c478bd9Sstevel@tonic-gate } 12557c478bd9Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1256*bb16350dSlclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 12577c478bd9Sstevel@tonic-gate Dfltdev); 12587c478bd9Sstevel@tonic-gate exit(1); 12597c478bd9Sstevel@tonic-gate } 12607c478bd9Sstevel@tonic-gate if (part_exit) 12617c478bd9Sstevel@tonic-gate exit(0); 12627c478bd9Sstevel@tonic-gate } /* while (--io_size); */ 12637c478bd9Sstevel@tonic-gate exit(1); 12647c478bd9Sstevel@tonic-gate } 12657c478bd9Sstevel@tonic-gate 1266*bb16350dSlclee 12677c478bd9Sstevel@tonic-gate /* 12687c478bd9Sstevel@tonic-gate * load 12697c478bd9Sstevel@tonic-gate * Load will either read the fdisk table from a file or add or 12707c478bd9Sstevel@tonic-gate * delete an entry (-A, -D, -F). 12717c478bd9Sstevel@tonic-gate */ 12727c478bd9Sstevel@tonic-gate 1273*bb16350dSlclee static void 1274*bb16350dSlclee load(int funct, char *file) 12757c478bd9Sstevel@tonic-gate { 12767c478bd9Sstevel@tonic-gate int id; 12777c478bd9Sstevel@tonic-gate int act; 12787c478bd9Sstevel@tonic-gate int bhead; 12797c478bd9Sstevel@tonic-gate int bsect; 12807c478bd9Sstevel@tonic-gate int bcyl; 12817c478bd9Sstevel@tonic-gate int ehead; 12827c478bd9Sstevel@tonic-gate int esect; 12837c478bd9Sstevel@tonic-gate int ecyl; 12847c478bd9Sstevel@tonic-gate int rsect; 12857c478bd9Sstevel@tonic-gate int numsect; 12867c478bd9Sstevel@tonic-gate char line[256]; 12877c478bd9Sstevel@tonic-gate int i = 0; 12887c478bd9Sstevel@tonic-gate int j; 12897c478bd9Sstevel@tonic-gate FILE *fp; 12907c478bd9Sstevel@tonic-gate 12917c478bd9Sstevel@tonic-gate switch (funct) { 12927c478bd9Sstevel@tonic-gate 12937c478bd9Sstevel@tonic-gate case LOADFILE: 12947c478bd9Sstevel@tonic-gate 12957c478bd9Sstevel@tonic-gate /* 12967c478bd9Sstevel@tonic-gate * Zero out the table before loading it, which will 12977c478bd9Sstevel@tonic-gate * force it to be updated on disk later (-F 12987c478bd9Sstevel@tonic-gate * fdisk_file). 12997c478bd9Sstevel@tonic-gate */ 13007c478bd9Sstevel@tonic-gate nulltbl(); 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate /* Open the prototype file */ 13037c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) { 13047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13057c478bd9Sstevel@tonic-gate "fdisk: Cannot open prototype partition file %s.\n", 13067c478bd9Sstevel@tonic-gate file); 13077c478bd9Sstevel@tonic-gate exit(1); 13087c478bd9Sstevel@tonic-gate } 13097c478bd9Sstevel@tonic-gate 13107c478bd9Sstevel@tonic-gate /* Read a line from the file */ 13117c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 13127c478bd9Sstevel@tonic-gate if (pars_fdisk(line, &id, &act, &bhead, &bsect, 13137c478bd9Sstevel@tonic-gate &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) { 13147c478bd9Sstevel@tonic-gate continue; 13157c478bd9Sstevel@tonic-gate } 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate /* 13187c478bd9Sstevel@tonic-gate * Validate the partition. It cannot start at sector 13197c478bd9Sstevel@tonic-gate * 0 unless it is UNUSED or already exists 13207c478bd9Sstevel@tonic-gate */ 13217c478bd9Sstevel@tonic-gate if (validate_part(id, rsect, numsect) < 0) { 13227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13237c478bd9Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13247c478bd9Sstevel@tonic-gate line); 13257c478bd9Sstevel@tonic-gate exit(1); 13267c478bd9Sstevel@tonic-gate } 13277c478bd9Sstevel@tonic-gate /* 13287c478bd9Sstevel@tonic-gate * Find an unused entry to use and put the entry 13297c478bd9Sstevel@tonic-gate * in table 13307c478bd9Sstevel@tonic-gate */ 13317c478bd9Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, 13327c478bd9Sstevel@tonic-gate esect, ecyl, rsect, numsect) < 0) { 13337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 13347c478bd9Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13357c478bd9Sstevel@tonic-gate line); 13367c478bd9Sstevel@tonic-gate exit(1); 13377c478bd9Sstevel@tonic-gate } 13387c478bd9Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 13397c478bd9Sstevel@tonic-gate 13407c478bd9Sstevel@tonic-gate if (verify_tbl() < 0) { 1341*bb16350dSlclee (void) fprintf(stderr, 13427c478bd9Sstevel@tonic-gate "fdisk: Cannot create partition table\n"); 13437c478bd9Sstevel@tonic-gate exit(1); 13447c478bd9Sstevel@tonic-gate } 13457c478bd9Sstevel@tonic-gate 1346*bb16350dSlclee (void) fclose(fp); 13477c478bd9Sstevel@tonic-gate return; 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate case LOADDEL: 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate /* Parse the user-supplied deletion line (-D) */ 1352*bb16350dSlclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, 1353*bb16350dSlclee &ehead, &esect, &ecyl, &rsect, &numsect)) { 1354*bb16350dSlclee (void) fprintf(stderr, 1355*bb16350dSlclee "fdisk: Syntax error \"%s\"\n", file); 1356*bb16350dSlclee exit(1); 1357*bb16350dSlclee } 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate /* Find the exact entry in the table */ 13607c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 13617c478bd9Sstevel@tonic-gate if (Table[i].systid == id && 13627c478bd9Sstevel@tonic-gate Table[i].bootid == act && 13637c478bd9Sstevel@tonic-gate Table[i].beghead == bhead && 13647c478bd9Sstevel@tonic-gate Table[i].begsect == ((bsect & 0x3f) | 1365*bb16350dSlclee (uchar_t)((bcyl>>2) & 0xc0)) && 1366*bb16350dSlclee Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 13677c478bd9Sstevel@tonic-gate Table[i].endhead == ehead && 13687c478bd9Sstevel@tonic-gate Table[i].endsect == ((esect & 0x3f) | 1369*bb16350dSlclee (uchar_t)((ecyl>>2) & 0xc0)) && 1370*bb16350dSlclee Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 13717c478bd9Sstevel@tonic-gate Table[i].relsect == lel(rsect) && 13727c478bd9Sstevel@tonic-gate Table[i].numsect == lel(numsect)) { 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate /* 13757c478bd9Sstevel@tonic-gate * Found the entry. Now move rest of 13767c478bd9Sstevel@tonic-gate * entries up toward the top of the 13777c478bd9Sstevel@tonic-gate * table, leaving available entries at 13787c478bd9Sstevel@tonic-gate * the end of the fdisk table. 13797c478bd9Sstevel@tonic-gate */ 13807c478bd9Sstevel@tonic-gate for (j = i; j < FD_NUMPART - 1; j++) { 13817c478bd9Sstevel@tonic-gate Table[j].systid = Table[j + 1].systid; 13827c478bd9Sstevel@tonic-gate Table[j].bootid = Table[j + 1].bootid; 13837c478bd9Sstevel@tonic-gate Table[j].beghead = Table[j + 1].beghead; 13847c478bd9Sstevel@tonic-gate Table[j].begsect = Table[j + 1].begsect; 13857c478bd9Sstevel@tonic-gate Table[j].begcyl = Table[j + 1].begcyl; 13867c478bd9Sstevel@tonic-gate Table[j].endhead = Table[j + 1].endhead; 13877c478bd9Sstevel@tonic-gate Table[j].endsect = Table[j + 1].endsect; 13887c478bd9Sstevel@tonic-gate Table[j].endcyl = Table[j + 1].endcyl; 13897c478bd9Sstevel@tonic-gate Table[j].relsect = Table[j + 1].relsect; 13907c478bd9Sstevel@tonic-gate Table[j].numsect = Table[j + 1].numsect; 13917c478bd9Sstevel@tonic-gate } 13927c478bd9Sstevel@tonic-gate 13937c478bd9Sstevel@tonic-gate /* 13947c478bd9Sstevel@tonic-gate * Mark the last entry as unused in case 13957c478bd9Sstevel@tonic-gate * all table entries were in use prior 13967c478bd9Sstevel@tonic-gate * to the deletion. 13977c478bd9Sstevel@tonic-gate */ 13987c478bd9Sstevel@tonic-gate 13997c478bd9Sstevel@tonic-gate Table[FD_NUMPART - 1].systid = UNUSED; 14007c478bd9Sstevel@tonic-gate Table[FD_NUMPART - 1].bootid = 0; 14017c478bd9Sstevel@tonic-gate return; 14027c478bd9Sstevel@tonic-gate } 14037c478bd9Sstevel@tonic-gate } 1404*bb16350dSlclee (void) fprintf(stderr, 14057c478bd9Sstevel@tonic-gate "fdisk: Entry does not match any existing partition:\n" 14067c478bd9Sstevel@tonic-gate " \"%s\"\n", 14077c478bd9Sstevel@tonic-gate file); 14087c478bd9Sstevel@tonic-gate exit(1); 1409*bb16350dSlclee /* FALLTHRU */ 14107c478bd9Sstevel@tonic-gate 14117c478bd9Sstevel@tonic-gate case LOADADD: 14127c478bd9Sstevel@tonic-gate 14137c478bd9Sstevel@tonic-gate /* Parse the user-supplied addition line (-A) */ 1414*bb16350dSlclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead, 1415*bb16350dSlclee &esect, &ecyl, &rsect, &numsect)) { 1416*bb16350dSlclee (void) fprintf(stderr, 1417*bb16350dSlclee "fdisk: Syntax error \"%s\"\n", file); 1418*bb16350dSlclee exit(1); 1419*bb16350dSlclee } 14207c478bd9Sstevel@tonic-gate 14217c478bd9Sstevel@tonic-gate /* Validate the partition. It cannot start at sector 0 */ 14227c478bd9Sstevel@tonic-gate if (rsect == 0) { 14237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14247c478bd9Sstevel@tonic-gate "fdisk: New partition cannot start at sector 0:\n" 14257c478bd9Sstevel@tonic-gate " \"%s\".\n", 14267c478bd9Sstevel@tonic-gate file); 14277c478bd9Sstevel@tonic-gate exit(1); 14287c478bd9Sstevel@tonic-gate } 14297c478bd9Sstevel@tonic-gate 14307c478bd9Sstevel@tonic-gate /* 14317c478bd9Sstevel@tonic-gate * if the user wishes to add an EFI partition, we need 14327c478bd9Sstevel@tonic-gate * more extensive validation. rsect should be 1, and 14337c478bd9Sstevel@tonic-gate * numsect should equal the entire disk capacity - 1 14347c478bd9Sstevel@tonic-gate */ 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate if (id == EFI_PMBR) { 14377c478bd9Sstevel@tonic-gate if (rsect != 1) { 14387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14397c478bd9Sstevel@tonic-gate "fdisk: EFI partitions must start at sector" 14407c478bd9Sstevel@tonic-gate " 1 (input rsect = %d)\n", rsect); 14417c478bd9Sstevel@tonic-gate exit(1); 14427c478bd9Sstevel@tonic-gate } 14437c478bd9Sstevel@tonic-gate 14447c478bd9Sstevel@tonic-gate if (numsect != ((Numcyl * heads * sectors) -1)) { 14457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14467c478bd9Sstevel@tonic-gate "fdisk: EFI partitions must encompass the " 1447*bb16350dSlclee "entire disk\n" 1448*bb16350dSlclee "(input numsect: %d - avail: %d)\n", 1449*bb16350dSlclee numsect, 14507c478bd9Sstevel@tonic-gate ((Numcyl * heads * sectors) -1)); 14517c478bd9Sstevel@tonic-gate exit(1); 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate } 14547c478bd9Sstevel@tonic-gate 14557c478bd9Sstevel@tonic-gate /* Find unused entry for use and put entry in table */ 14567c478bd9Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect, 14577c478bd9Sstevel@tonic-gate ecyl, rsect, numsect) < 0) { 14587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 14597c478bd9Sstevel@tonic-gate "fdisk: Invalid entry could not be inserted:\n" 14607c478bd9Sstevel@tonic-gate " \"%s\"\n", 14617c478bd9Sstevel@tonic-gate file); 14627c478bd9Sstevel@tonic-gate exit(1); 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate /* Make sure new entry does not overlap existing entry */ 14667c478bd9Sstevel@tonic-gate if (verify_tbl() < 0) { 1467*bb16350dSlclee (void) fprintf(stderr, 1468*bb16350dSlclee "fdisk: Cannot create partition \"%s\"\n", file); 14697c478bd9Sstevel@tonic-gate exit(1); 14707c478bd9Sstevel@tonic-gate } 14717c478bd9Sstevel@tonic-gate } /* switch funct */ 14727c478bd9Sstevel@tonic-gate } 14737c478bd9Sstevel@tonic-gate 14747c478bd9Sstevel@tonic-gate /* 14757c478bd9Sstevel@tonic-gate * Set_Table_CHS_Values 14767c478bd9Sstevel@tonic-gate * 14777c478bd9Sstevel@tonic-gate * This will calculate the CHS values for beginning and ending CHS 14787c478bd9Sstevel@tonic-gate * for a single partition table entry (ti) based on the relsect 14797c478bd9Sstevel@tonic-gate * and numsect values contained in the partion table entry. 14807c478bd9Sstevel@tonic-gate * 14817c478bd9Sstevel@tonic-gate * hba_heads and hba_sectors contain the number of heads and sectors. 14827c478bd9Sstevel@tonic-gate * 14837c478bd9Sstevel@tonic-gate * If the number of cylinders exceeds the MAX_CYL, 14847c478bd9Sstevel@tonic-gate * then maximum values will be placed in the corresponding chs entry. 14857c478bd9Sstevel@tonic-gate */ 14867c478bd9Sstevel@tonic-gate static void 14877c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(int ti) 14887c478bd9Sstevel@tonic-gate { 14897c478bd9Sstevel@tonic-gate uint32_t lba, cy, hd, sc; 14907c478bd9Sstevel@tonic-gate 14917c478bd9Sstevel@tonic-gate lba = (uint32_t)Table[ti].relsect; 14927c478bd9Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 14937c478bd9Sstevel@tonic-gate /* 14947c478bd9Sstevel@tonic-gate * the lba address cannot be expressed in CHS value 14957c478bd9Sstevel@tonic-gate * so store the maximum CHS field values in the CHS fields. 14967c478bd9Sstevel@tonic-gate */ 14977c478bd9Sstevel@tonic-gate cy = MAX_CYL + 1; 14987c478bd9Sstevel@tonic-gate hd = MAX_HEAD; 14997c478bd9Sstevel@tonic-gate sc = MAX_SECT; 15007c478bd9Sstevel@tonic-gate } else { 15017c478bd9Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 15027c478bd9Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 15037c478bd9Sstevel@tonic-gate sc = lba % hba_sectors + 1; 15047c478bd9Sstevel@tonic-gate } 15057c478bd9Sstevel@tonic-gate Table[ti].begcyl = cy & 0xff; 1506*bb16350dSlclee Table[ti].beghead = (uchar_t)hd; 1507*bb16350dSlclee Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 15087c478bd9Sstevel@tonic-gate 15097c478bd9Sstevel@tonic-gate /* 15107c478bd9Sstevel@tonic-gate * This code is identical to the code above 15117c478bd9Sstevel@tonic-gate * except that it works on ending CHS values 15127c478bd9Sstevel@tonic-gate */ 15137c478bd9Sstevel@tonic-gate lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1); 15147c478bd9Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 15157c478bd9Sstevel@tonic-gate cy = MAX_CYL + 1; 15167c478bd9Sstevel@tonic-gate hd = MAX_HEAD; 15177c478bd9Sstevel@tonic-gate sc = MAX_SECT; 15187c478bd9Sstevel@tonic-gate } else { 15197c478bd9Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 15207c478bd9Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 15217c478bd9Sstevel@tonic-gate sc = lba % hba_sectors + 1; 15227c478bd9Sstevel@tonic-gate } 15237c478bd9Sstevel@tonic-gate Table[ti].endcyl = cy & 0xff; 1524*bb16350dSlclee Table[ti].endhead = (uchar_t)hd; 1525*bb16350dSlclee Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate 15287c478bd9Sstevel@tonic-gate /* 15297c478bd9Sstevel@tonic-gate * insert_tbl 15307c478bd9Sstevel@tonic-gate * Insert entry into fdisk table. Check all user-supplied values 15317c478bd9Sstevel@tonic-gate * for the entry, but not the validity relative to other table 15327c478bd9Sstevel@tonic-gate * entries! 15337c478bd9Sstevel@tonic-gate */ 1534*bb16350dSlclee static int 1535*bb16350dSlclee insert_tbl( 1536*bb16350dSlclee int id, int act, 1537*bb16350dSlclee int bhead, int bsect, int bcyl, 1538*bb16350dSlclee int ehead, int esect, int ecyl, 1539*bb16350dSlclee int rsect, int numsect) 15407c478bd9Sstevel@tonic-gate { 15417c478bd9Sstevel@tonic-gate int i; 15427c478bd9Sstevel@tonic-gate 15437c478bd9Sstevel@tonic-gate /* validate partition size */ 15447c478bd9Sstevel@tonic-gate if (rsect + numsect > (Numcyl * heads * sectors)) { 1545*bb16350dSlclee (void) fprintf(stderr, 15467c478bd9Sstevel@tonic-gate "fdisk: Partition table exceeds the size of the disk.\n"); 15477c478bd9Sstevel@tonic-gate return (-1); 15487c478bd9Sstevel@tonic-gate } 15497c478bd9Sstevel@tonic-gate 15507c478bd9Sstevel@tonic-gate /* find UNUSED partition table entry */ 15517c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 15527c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 15537c478bd9Sstevel@tonic-gate break; 15547c478bd9Sstevel@tonic-gate } 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate if (i >= FD_NUMPART) { 1557*bb16350dSlclee (void) fprintf(stderr, "fdisk: Partition table is full.\n"); 15587c478bd9Sstevel@tonic-gate return (-1); 15597c478bd9Sstevel@tonic-gate } 15607c478bd9Sstevel@tonic-gate 15617c478bd9Sstevel@tonic-gate 1562*bb16350dSlclee Table[i].systid = (uchar_t)id; 1563*bb16350dSlclee Table[i].bootid = (uchar_t)act; 15647c478bd9Sstevel@tonic-gate Table[i].numsect = lel(numsect); 15657c478bd9Sstevel@tonic-gate Table[i].relsect = lel(rsect); 15667c478bd9Sstevel@tonic-gate 15677c478bd9Sstevel@tonic-gate /* 15687c478bd9Sstevel@tonic-gate * If we have been called with a valid geometry, use it 15697c478bd9Sstevel@tonic-gate * valid means non-zero values that fit in the BIOS fields 15707c478bd9Sstevel@tonic-gate */ 15717c478bd9Sstevel@tonic-gate if (0 < bsect && bsect <= MAX_SECT && 15727c478bd9Sstevel@tonic-gate 0 <= bhead && bhead <= MAX_HEAD && 15737c478bd9Sstevel@tonic-gate 0 < esect && esect <= MAX_SECT && 15747c478bd9Sstevel@tonic-gate 0 <= ehead && ehead <= MAX_HEAD) { 15757c478bd9Sstevel@tonic-gate if (bcyl > MAX_CYL) 15767c478bd9Sstevel@tonic-gate bcyl = MAX_CYL + 1; 15777c478bd9Sstevel@tonic-gate if (ecyl > MAX_CYL) 15787c478bd9Sstevel@tonic-gate ecyl = MAX_CYL + 1; 15797c478bd9Sstevel@tonic-gate Table[i].begcyl = bcyl & 0xff; 15807c478bd9Sstevel@tonic-gate Table[i].endcyl = ecyl & 0xff; 1581*bb16350dSlclee Table[i].beghead = (uchar_t)bhead; 1582*bb16350dSlclee Table[i].endhead = (uchar_t)ehead; 1583*bb16350dSlclee Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect); 15847c478bd9Sstevel@tonic-gate Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect; 15857c478bd9Sstevel@tonic-gate } else { 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate /* 15887c478bd9Sstevel@tonic-gate * The specified values are invalid, 15897c478bd9Sstevel@tonic-gate * so calculate the values based on hba_heads, hba_sectors 15907c478bd9Sstevel@tonic-gate */ 15917c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(i); 15927c478bd9Sstevel@tonic-gate } 15937c478bd9Sstevel@tonic-gate 15947c478bd9Sstevel@tonic-gate /* 15957c478bd9Sstevel@tonic-gate * return partition index 15967c478bd9Sstevel@tonic-gate */ 15977c478bd9Sstevel@tonic-gate return (i); 15987c478bd9Sstevel@tonic-gate } 15997c478bd9Sstevel@tonic-gate 16007c478bd9Sstevel@tonic-gate /* 16017c478bd9Sstevel@tonic-gate * verify_tbl 16027c478bd9Sstevel@tonic-gate * Verify that no partition entries overlap or exceed the size of 16037c478bd9Sstevel@tonic-gate * the disk. 16047c478bd9Sstevel@tonic-gate */ 1605*bb16350dSlclee static int 1606*bb16350dSlclee verify_tbl(void) 16077c478bd9Sstevel@tonic-gate { 16087c478bd9Sstevel@tonic-gate int i, j, rsect, numsect; 16097c478bd9Sstevel@tonic-gate int noMoreParts = 0; 16107c478bd9Sstevel@tonic-gate int numParts = 0; 16117c478bd9Sstevel@tonic-gate 16127c478bd9Sstevel@tonic-gate /* Make sure new entry does not overlap an existing entry */ 16137c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART - 1; i++) { 16147c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 16157c478bd9Sstevel@tonic-gate numParts++; 16167c478bd9Sstevel@tonic-gate /* 16177c478bd9Sstevel@tonic-gate * No valid partitions allowed after an UNUSED or 16187c478bd9Sstevel@tonic-gate * EFI_PMBR part 16197c478bd9Sstevel@tonic-gate */ 16207c478bd9Sstevel@tonic-gate if (noMoreParts) { 16217c478bd9Sstevel@tonic-gate return (-1); 16227c478bd9Sstevel@tonic-gate } 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate /* 16257c478bd9Sstevel@tonic-gate * EFI_PMBR partitions must be the only partition 16267c478bd9Sstevel@tonic-gate * and must be Table entry 0 16277c478bd9Sstevel@tonic-gate */ 16287c478bd9Sstevel@tonic-gate if (Table[i].systid == EFI_PMBR) { 16297c478bd9Sstevel@tonic-gate if (i == 0) { 16307c478bd9Sstevel@tonic-gate noMoreParts = 1; 16317c478bd9Sstevel@tonic-gate } else { 16327c478bd9Sstevel@tonic-gate return (-1); 16337c478bd9Sstevel@tonic-gate } 16347c478bd9Sstevel@tonic-gate 16357c478bd9Sstevel@tonic-gate if (Table[i].relsect != 1) { 1636*bb16350dSlclee (void) fprintf(stderr, "ERROR: " 16377c478bd9Sstevel@tonic-gate "Invalid starting sector " 16387c478bd9Sstevel@tonic-gate "for EFI_PMBR partition:\n" 16397c478bd9Sstevel@tonic-gate "relsect %d " 16407c478bd9Sstevel@tonic-gate "(should be 1)\n", 16417c478bd9Sstevel@tonic-gate Table[i].relsect); 16427c478bd9Sstevel@tonic-gate 16437c478bd9Sstevel@tonic-gate return (-1); 16447c478bd9Sstevel@tonic-gate } 16457c478bd9Sstevel@tonic-gate 16467c478bd9Sstevel@tonic-gate if (Table[i].numsect != 16477c478bd9Sstevel@tonic-gate ((Numcyl * heads * sectors) - 1)) { 1648*bb16350dSlclee (void) fprintf(stderr, "ERROR: " 16497c478bd9Sstevel@tonic-gate "EFI_PMBR partition must " 16507c478bd9Sstevel@tonic-gate "encompass the entire " 1651*bb16350dSlclee "disk.\n numsect %d - " 1652*bb16350dSlclee "actual %d\n", 16537c478bd9Sstevel@tonic-gate Table[i].numsect, 16547c478bd9Sstevel@tonic-gate ((Numcyl * heads * sectors) - 1)); 16557c478bd9Sstevel@tonic-gate 16567c478bd9Sstevel@tonic-gate return (-1); 16577c478bd9Sstevel@tonic-gate } 16587c478bd9Sstevel@tonic-gate } 16597c478bd9Sstevel@tonic-gate 16607c478bd9Sstevel@tonic-gate /* make sure the partition isn't larger than the disk */ 16617c478bd9Sstevel@tonic-gate rsect = lel(Table[i].relsect); 16627c478bd9Sstevel@tonic-gate numsect = lel(Table[i].numsect); 16637c478bd9Sstevel@tonic-gate if ((rsect + numsect) > (Numcyl * heads * sectors)) { 16647c478bd9Sstevel@tonic-gate return (-1); 16657c478bd9Sstevel@tonic-gate } 16667c478bd9Sstevel@tonic-gate 16677c478bd9Sstevel@tonic-gate for (j = i + 1; j < FD_NUMPART; j++) { 16687c478bd9Sstevel@tonic-gate if (Table[j].systid != UNUSED) { 16697c478bd9Sstevel@tonic-gate int t_relsect = lel(Table[j].relsect); 16707c478bd9Sstevel@tonic-gate int t_numsect = lel(Table[j].numsect); 16717c478bd9Sstevel@tonic-gate 16727c478bd9Sstevel@tonic-gate if (noMoreParts) { 1673*bb16350dSlclee (void) fprintf(stderr, 16747c478bd9Sstevel@tonic-gate "Cannot add partition to " 16757c478bd9Sstevel@tonic-gate "table; no more partitions " 16767c478bd9Sstevel@tonic-gate "allowed\n"); 16777c478bd9Sstevel@tonic-gate 16787c478bd9Sstevel@tonic-gate if (io_debug) { 1679*bb16350dSlclee (void) fprintf(stderr, 16807c478bd9Sstevel@tonic-gate "DEBUG: Current " 16817c478bd9Sstevel@tonic-gate "partition:\t" 16827c478bd9Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1683*bb16350dSlclee "%d:%d:%d:%d:%d\n" 16847c478bd9Sstevel@tonic-gate " Next " 16857c478bd9Sstevel@tonic-gate "partition:\t\t" 16867c478bd9Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1687*bb16350dSlclee "%d:%d:%d:%d:%d\n", 16887c478bd9Sstevel@tonic-gate Table[i].systid, 16897c478bd9Sstevel@tonic-gate Table[i].bootid, 16907c478bd9Sstevel@tonic-gate Table[i].begcyl, 16917c478bd9Sstevel@tonic-gate Table[i].beghead, 16927c478bd9Sstevel@tonic-gate Table[i].begsect, 16937c478bd9Sstevel@tonic-gate Table[i].endcyl, 16947c478bd9Sstevel@tonic-gate Table[i].endhead, 16957c478bd9Sstevel@tonic-gate Table[i].endsect, 16967c478bd9Sstevel@tonic-gate Table[i].relsect, 16977c478bd9Sstevel@tonic-gate Table[i].numsect, 16987c478bd9Sstevel@tonic-gate Table[j].systid, 16997c478bd9Sstevel@tonic-gate Table[j].bootid, 17007c478bd9Sstevel@tonic-gate Table[j].begcyl, 17017c478bd9Sstevel@tonic-gate Table[j].beghead, 17027c478bd9Sstevel@tonic-gate Table[j].begsect, 17037c478bd9Sstevel@tonic-gate Table[j].endcyl, 17047c478bd9Sstevel@tonic-gate Table[j].endhead, 17057c478bd9Sstevel@tonic-gate Table[j].endsect, 17067c478bd9Sstevel@tonic-gate Table[j].relsect, 17077c478bd9Sstevel@tonic-gate Table[j].numsect); 17087c478bd9Sstevel@tonic-gate } 17097c478bd9Sstevel@tonic-gate 17107c478bd9Sstevel@tonic-gate return (-1); 17117c478bd9Sstevel@tonic-gate } 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate if ((rsect >= 17147c478bd9Sstevel@tonic-gate (t_relsect + t_numsect)) || 17157c478bd9Sstevel@tonic-gate ((rsect + numsect) <= t_relsect)) { 17167c478bd9Sstevel@tonic-gate continue; 17177c478bd9Sstevel@tonic-gate } else { 1718*bb16350dSlclee (void) fprintf(stderr, "ERROR: " 17197c478bd9Sstevel@tonic-gate "current partition overlaps" 17207c478bd9Sstevel@tonic-gate " following partition\n"); 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate return (-1); 17237c478bd9Sstevel@tonic-gate } 17247c478bd9Sstevel@tonic-gate } 17257c478bd9Sstevel@tonic-gate } 17267c478bd9Sstevel@tonic-gate } else { 17277c478bd9Sstevel@tonic-gate noMoreParts = 1; 17287c478bd9Sstevel@tonic-gate } 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 17317c478bd9Sstevel@tonic-gate if (noMoreParts || 17327c478bd9Sstevel@tonic-gate ((lel(Table[i].relsect) + lel(Table[i].numsect)) > 17337c478bd9Sstevel@tonic-gate (Numcyl * heads * sectors))) { 17347c478bd9Sstevel@tonic-gate return (-1); 17357c478bd9Sstevel@tonic-gate } 17367c478bd9Sstevel@tonic-gate } 17377c478bd9Sstevel@tonic-gate 17387c478bd9Sstevel@tonic-gate return (numParts); 17397c478bd9Sstevel@tonic-gate } 17407c478bd9Sstevel@tonic-gate 17417c478bd9Sstevel@tonic-gate /* 17427c478bd9Sstevel@tonic-gate * pars_fdisk 17437c478bd9Sstevel@tonic-gate * Parse user-supplied data to set up fdisk partitions 17447c478bd9Sstevel@tonic-gate * (-A, -D, -F). 17457c478bd9Sstevel@tonic-gate */ 1746*bb16350dSlclee static int 1747*bb16350dSlclee pars_fdisk( 1748*bb16350dSlclee char *line, 1749*bb16350dSlclee int *id, int *act, 1750*bb16350dSlclee int *bhead, int *bsect, int *bcyl, 1751*bb16350dSlclee int *ehead, int *esect, int *ecyl, 1752*bb16350dSlclee int *rsect, int *numsect) 17537c478bd9Sstevel@tonic-gate { 17547c478bd9Sstevel@tonic-gate int i; 17557c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 17567c478bd9Sstevel@tonic-gate return (1); 17577c478bd9Sstevel@tonic-gate line[strlen(line)] = '\0'; 17587c478bd9Sstevel@tonic-gate for (i = 0; i < strlen(line); i++) { 17597c478bd9Sstevel@tonic-gate if (line[i] == '\0') { 17607c478bd9Sstevel@tonic-gate break; 17617c478bd9Sstevel@tonic-gate } else if (line[i] == ':') { 17627c478bd9Sstevel@tonic-gate line[i] = ' '; 17637c478bd9Sstevel@tonic-gate } 17647c478bd9Sstevel@tonic-gate } 1765*bb16350dSlclee if (sscanf(line, "%d %d %d %d %d %d %d %d %d %d", 17667c478bd9Sstevel@tonic-gate id, act, bhead, bsect, bcyl, ehead, esect, ecyl, 17677c478bd9Sstevel@tonic-gate rsect, numsect) != 10) { 17687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error:\n \"%s\".\n", line); 17697c478bd9Sstevel@tonic-gate exit(1); 17707c478bd9Sstevel@tonic-gate } 17717c478bd9Sstevel@tonic-gate return (0); 17727c478bd9Sstevel@tonic-gate } 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate /* 17757c478bd9Sstevel@tonic-gate * validate_part 17767c478bd9Sstevel@tonic-gate * Validate that a new partition does not start at sector 0. Only UNUSED 17777c478bd9Sstevel@tonic-gate * partitions and previously existing partitions are allowed to start at 0. 17787c478bd9Sstevel@tonic-gate */ 1779*bb16350dSlclee static int 1780*bb16350dSlclee validate_part(int id, int rsect, int numsect) 17817c478bd9Sstevel@tonic-gate { 17827c478bd9Sstevel@tonic-gate int i; 17837c478bd9Sstevel@tonic-gate if ((id != UNUSED) && (rsect == 0)) { 17847c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 17857c478bd9Sstevel@tonic-gate if ((Old_Table[i].systid == id) && 17867c478bd9Sstevel@tonic-gate (Old_Table[i].relsect == lel(rsect)) && 17877c478bd9Sstevel@tonic-gate (Old_Table[i].numsect == lel(numsect))) return (0); 17887c478bd9Sstevel@tonic-gate } 1789*bb16350dSlclee (void) fprintf(stderr, 1790*bb16350dSlclee "New partition cannot start at sector 0\n"); 17917c478bd9Sstevel@tonic-gate return (-1); 17927c478bd9Sstevel@tonic-gate } 17937c478bd9Sstevel@tonic-gate return (0); 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate /* 17977c478bd9Sstevel@tonic-gate * stage0 17987c478bd9Sstevel@tonic-gate * Print out interactive menu and process user input. 17997c478bd9Sstevel@tonic-gate */ 1800*bb16350dSlclee static void 1801*bb16350dSlclee stage0(void) 18027c478bd9Sstevel@tonic-gate { 1803*bb16350dSlclee dispmenu(); 1804*bb16350dSlclee for (;;) { 1805*bb16350dSlclee (void) printf(Q_LINE); 1806*bb16350dSlclee (void) printf("Enter Selection: "); 1807*bb16350dSlclee (void) gets(s); 18087c478bd9Sstevel@tonic-gate rm_blanks(s); 18097c478bd9Sstevel@tonic-gate while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) { 1810*bb16350dSlclee (void) printf(E_LINE); /* Clear any previous error */ 1811*bb16350dSlclee (void) printf( 1812*bb16350dSlclee "Enter a one-digit number between 1 and 6."); 1813*bb16350dSlclee (void) printf(Q_LINE); 1814*bb16350dSlclee (void) printf("Enter Selection: "); 1815*bb16350dSlclee (void) gets(s); 18167c478bd9Sstevel@tonic-gate rm_blanks(s); 18177c478bd9Sstevel@tonic-gate } 1818*bb16350dSlclee (void) printf(E_LINE); 18197c478bd9Sstevel@tonic-gate switch (s[0]) { 18207c478bd9Sstevel@tonic-gate case '1': 18217c478bd9Sstevel@tonic-gate if (pcreate() == -1) 18227c478bd9Sstevel@tonic-gate return; 18237c478bd9Sstevel@tonic-gate break; 18247c478bd9Sstevel@tonic-gate case '2': 18257c478bd9Sstevel@tonic-gate if (pchange() == -1) 18267c478bd9Sstevel@tonic-gate return; 18277c478bd9Sstevel@tonic-gate break; 18287c478bd9Sstevel@tonic-gate case '3': 18297c478bd9Sstevel@tonic-gate if (pdelete() == -1) 18307c478bd9Sstevel@tonic-gate return; 18317c478bd9Sstevel@tonic-gate break; 18327c478bd9Sstevel@tonic-gate case '4': 18337c478bd9Sstevel@tonic-gate if (ppartid() == -1) 18347c478bd9Sstevel@tonic-gate return; 18357c478bd9Sstevel@tonic-gate break; 18367c478bd9Sstevel@tonic-gate case '5': 18377c478bd9Sstevel@tonic-gate /* update disk partition table, if changed */ 18387c478bd9Sstevel@tonic-gate if (TableChanged() == 1) { 18397c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 18407c478bd9Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 18417c478bd9Sstevel@tonic-gate } 18427c478bd9Sstevel@tonic-gate /* 18437c478bd9Sstevel@tonic-gate * If the VTOC table is wrong fix it 18447c478bd9Sstevel@tonic-gate * (truncate only) 18457c478bd9Sstevel@tonic-gate */ 18467c478bd9Sstevel@tonic-gate if (io_adjt) { 18477c478bd9Sstevel@tonic-gate fix_slice(); 18487c478bd9Sstevel@tonic-gate } 1849*bb16350dSlclee (void) close(Dev); 18507c478bd9Sstevel@tonic-gate exit(0); 1851*bb16350dSlclee /* FALLTHRU */ 18527c478bd9Sstevel@tonic-gate case '6': 18537c478bd9Sstevel@tonic-gate /* 18547c478bd9Sstevel@tonic-gate * If the VTOC table is wrong fix it 18557c478bd9Sstevel@tonic-gate * (truncate only) 18567c478bd9Sstevel@tonic-gate */ 18577c478bd9Sstevel@tonic-gate if (io_adjt) { 18587c478bd9Sstevel@tonic-gate fix_slice(); 18597c478bd9Sstevel@tonic-gate } 1860*bb16350dSlclee (void) close(Dev); 18617c478bd9Sstevel@tonic-gate exit(0); 1862*bb16350dSlclee /* FALLTHRU */ 18637c478bd9Sstevel@tonic-gate default: 18647c478bd9Sstevel@tonic-gate break; 18657c478bd9Sstevel@tonic-gate } 18667c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 18677c478bd9Sstevel@tonic-gate disptbl(); 1868*bb16350dSlclee dispmenu(); 18697c478bd9Sstevel@tonic-gate } 18707c478bd9Sstevel@tonic-gate } 18717c478bd9Sstevel@tonic-gate 18727c478bd9Sstevel@tonic-gate /* 18737c478bd9Sstevel@tonic-gate * pcreate 18747c478bd9Sstevel@tonic-gate * Create partition entry in the table (interactive mode). 18757c478bd9Sstevel@tonic-gate */ 1876*bb16350dSlclee static int 1877*bb16350dSlclee pcreate(void) 18787c478bd9Sstevel@tonic-gate { 1879*bb16350dSlclee uchar_t tsystid = 'z'; 18807c478bd9Sstevel@tonic-gate int i, j; 18817c478bd9Sstevel@tonic-gate int rsect = 1; 18827c478bd9Sstevel@tonic-gate int retCode = 0; 18837c478bd9Sstevel@tonic-gate 18847c478bd9Sstevel@tonic-gate i = 0; 1885*bb16350dSlclee for (;;) { 18867c478bd9Sstevel@tonic-gate if (i == FD_NUMPART) { 1887*bb16350dSlclee (void) printf(E_LINE); 1888*bb16350dSlclee (void) printf( 1889*bb16350dSlclee "The partition table is full!\n" 1890*bb16350dSlclee "You must delete a partition before creating" 18917c478bd9Sstevel@tonic-gate " a new one.\n"); 18927c478bd9Sstevel@tonic-gate return (-1); 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 18957c478bd9Sstevel@tonic-gate break; 18967c478bd9Sstevel@tonic-gate } 18977c478bd9Sstevel@tonic-gate i++; 18987c478bd9Sstevel@tonic-gate } 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate j = 0; 19017c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 19027c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 19037c478bd9Sstevel@tonic-gate j += lel(Table[i].numsect); 19047c478bd9Sstevel@tonic-gate } 19057c478bd9Sstevel@tonic-gate if (j >= Numcyl * heads * sectors) { 1906*bb16350dSlclee (void) printf(E_LINE); 1907*bb16350dSlclee (void) printf("There is no more room on the disk for" 19087c478bd9Sstevel@tonic-gate " another partition.\n"); 1909*bb16350dSlclee (void) printf( 1910*bb16350dSlclee "You must delete a partition before creating" 19117c478bd9Sstevel@tonic-gate " a new one.\n"); 19127c478bd9Sstevel@tonic-gate return (-1); 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate } 19157c478bd9Sstevel@tonic-gate while (tsystid == 'z') { 1916*bb16350dSlclee (void) printf(Q_LINE); 1917*bb16350dSlclee (void) printf( 1918*bb16350dSlclee "Select the partition type to create:\n" 1919*bb16350dSlclee " 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n" 1920*bb16350dSlclee " 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n" 1921*bb16350dSlclee " 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n" 1922*bb16350dSlclee " D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? "); 1923*bb16350dSlclee (void) gets(s); 19247c478bd9Sstevel@tonic-gate rm_blanks(s); 19257c478bd9Sstevel@tonic-gate if (s[1] != 0) { 1926*bb16350dSlclee (void) printf(E_LINE); 1927*bb16350dSlclee (void) printf("Invalid selection, try again."); 19287c478bd9Sstevel@tonic-gate continue; 19297c478bd9Sstevel@tonic-gate } 19307c478bd9Sstevel@tonic-gate switch (s[0]) { 19317c478bd9Sstevel@tonic-gate case '0': /* exit */ 1932*bb16350dSlclee (void) printf(E_LINE); 19337c478bd9Sstevel@tonic-gate return (-1); 19347c478bd9Sstevel@tonic-gate case '1': /* Solaris partition */ 19357c478bd9Sstevel@tonic-gate tsystid = SUNIXOS2; 19367c478bd9Sstevel@tonic-gate break; 19377c478bd9Sstevel@tonic-gate case '2': /* UNIX partition */ 19387c478bd9Sstevel@tonic-gate tsystid = UNIXOS; 19397c478bd9Sstevel@tonic-gate break; 19407c478bd9Sstevel@tonic-gate case '3': /* PCIXOS partition */ 19417c478bd9Sstevel@tonic-gate tsystid = PCIXOS; 19427c478bd9Sstevel@tonic-gate break; 19437c478bd9Sstevel@tonic-gate case '4': /* OTHEROS System partition */ 19447c478bd9Sstevel@tonic-gate tsystid = OTHEROS; 19457c478bd9Sstevel@tonic-gate break; 19467c478bd9Sstevel@tonic-gate case '5': 19477c478bd9Sstevel@tonic-gate tsystid = DOSOS12; /* DOS 12 bit fat */ 19487c478bd9Sstevel@tonic-gate break; 19497c478bd9Sstevel@tonic-gate case '6': 19507c478bd9Sstevel@tonic-gate tsystid = DOSOS16; /* DOS 16 bit fat */ 19517c478bd9Sstevel@tonic-gate break; 19527c478bd9Sstevel@tonic-gate case '7': 19537c478bd9Sstevel@tonic-gate tsystid = EXTDOS; 19547c478bd9Sstevel@tonic-gate break; 19557c478bd9Sstevel@tonic-gate case '8': 19567c478bd9Sstevel@tonic-gate tsystid = DOSHUGE; 19577c478bd9Sstevel@tonic-gate break; 19587c478bd9Sstevel@tonic-gate case '9': 19597c478bd9Sstevel@tonic-gate tsystid = FDISK_FAT95; /* FAT16, need extended int13 */ 19607c478bd9Sstevel@tonic-gate break; 19617c478bd9Sstevel@tonic-gate case 'a': /* x86 Boot partition */ 19627c478bd9Sstevel@tonic-gate case 'A': 19637c478bd9Sstevel@tonic-gate tsystid = X86BOOT; 19647c478bd9Sstevel@tonic-gate break; 19657c478bd9Sstevel@tonic-gate case 'b': /* Diagnostic boot partition */ 19667c478bd9Sstevel@tonic-gate case 'B': 19677c478bd9Sstevel@tonic-gate tsystid = DIAGPART; 19687c478bd9Sstevel@tonic-gate break; 19697c478bd9Sstevel@tonic-gate case 'c': /* FAT32 */ 19707c478bd9Sstevel@tonic-gate case 'C': 19717c478bd9Sstevel@tonic-gate tsystid = FDISK_WINDOWS; 19727c478bd9Sstevel@tonic-gate break; 19737c478bd9Sstevel@tonic-gate case 'd': /* FAT32 and need extended int13 */ 19747c478bd9Sstevel@tonic-gate case 'D': 19757c478bd9Sstevel@tonic-gate tsystid = FDISK_EXT_WIN; 19767c478bd9Sstevel@tonic-gate break; 19777c478bd9Sstevel@tonic-gate case 'e': /* Extended partition, need extended int13 */ 19787c478bd9Sstevel@tonic-gate case 'E': 19797c478bd9Sstevel@tonic-gate tsystid = FDISK_EXTLBA; 19807c478bd9Sstevel@tonic-gate break; 19817c478bd9Sstevel@tonic-gate case 'f': 19827c478bd9Sstevel@tonic-gate case 'F': 19837c478bd9Sstevel@tonic-gate tsystid = EFI_PMBR; 19847c478bd9Sstevel@tonic-gate break; 19857c478bd9Sstevel@tonic-gate default: 1986*bb16350dSlclee (void) printf(E_LINE); 1987*bb16350dSlclee (void) printf("Invalid selection, try again."); 19887c478bd9Sstevel@tonic-gate continue; 19897c478bd9Sstevel@tonic-gate } 19907c478bd9Sstevel@tonic-gate } 19917c478bd9Sstevel@tonic-gate 1992*bb16350dSlclee (void) printf(E_LINE); 19937c478bd9Sstevel@tonic-gate 19947c478bd9Sstevel@tonic-gate if (tsystid != EFI_PMBR) { 19957c478bd9Sstevel@tonic-gate /* create the new partition */ 19967c478bd9Sstevel@tonic-gate i = specify(tsystid); 19977c478bd9Sstevel@tonic-gate 19987c478bd9Sstevel@tonic-gate if (i != -1) { 19997c478bd9Sstevel@tonic-gate /* see if it should be the active partition */ 2000*bb16350dSlclee (void) printf(E_LINE); 2001*bb16350dSlclee (void) printf(Q_LINE); 20027c478bd9Sstevel@tonic-gate 2003*bb16350dSlclee (void) printf( 2004*bb16350dSlclee "Should this become the active partition? If " 2005*bb16350dSlclee "yes, it will be activated\n" 2006*bb16350dSlclee "each time the computer is reset or turned on.\n" 2007*bb16350dSlclee "Please type \"y\" or \"n\". "); 20087c478bd9Sstevel@tonic-gate 20097c478bd9Sstevel@tonic-gate if (yesno()) { 2010*bb16350dSlclee (void) printf(E_LINE); 20117c478bd9Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 20127c478bd9Sstevel@tonic-gate if (j == i) { 20137c478bd9Sstevel@tonic-gate Table[j].bootid = ACTIVE; 2014*bb16350dSlclee (void) printf(E_LINE); 2015*bb16350dSlclee (void) printf( 2016*bb16350dSlclee "Partition %d is now " 20177c478bd9Sstevel@tonic-gate "the active partition.", 20187c478bd9Sstevel@tonic-gate j + 1); 20197c478bd9Sstevel@tonic-gate } else { 20207c478bd9Sstevel@tonic-gate Table[j].bootid = 0; 20217c478bd9Sstevel@tonic-gate } 20227c478bd9Sstevel@tonic-gate } 20237c478bd9Sstevel@tonic-gate } else { 20247c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 20257c478bd9Sstevel@tonic-gate } 20267c478bd9Sstevel@tonic-gate 20277c478bd9Sstevel@tonic-gate /* set up the return code */ 20287c478bd9Sstevel@tonic-gate i = 1; 20297c478bd9Sstevel@tonic-gate } 20307c478bd9Sstevel@tonic-gate } else { 20317c478bd9Sstevel@tonic-gate /* 20327c478bd9Sstevel@tonic-gate * partitions of type EFI_PMBR must be the only partitions in 20337c478bd9Sstevel@tonic-gate * the table 20347c478bd9Sstevel@tonic-gate * 20357c478bd9Sstevel@tonic-gate * First, make sure there were no errors the table is 20367c478bd9Sstevel@tonic-gate * empty 20377c478bd9Sstevel@tonic-gate */ 20387c478bd9Sstevel@tonic-gate retCode = verify_tbl(); 20397c478bd9Sstevel@tonic-gate 20407c478bd9Sstevel@tonic-gate if (retCode < 0) { 2041*bb16350dSlclee (void) fprintf(stderr, 20427c478bd9Sstevel@tonic-gate "fdisk: Cannot create EFI partition table; \n" 20437c478bd9Sstevel@tonic-gate "current partition table is invalid.\n"); 20447c478bd9Sstevel@tonic-gate return (-1); 20457c478bd9Sstevel@tonic-gate } else if (retCode > 0) { 2046*bb16350dSlclee (void) printf( 2047*bb16350dSlclee "An EFI partition must be the only partition on " 20487c478bd9Sstevel@tonic-gate "disk. You may manually delete existing\n" 2049*bb16350dSlclee "partitions, or fdisk can do it.\n" 2050*bb16350dSlclee "Do you want fdisk to destroy existing " 2051*bb16350dSlclee "partitions?\n" 2052*bb16350dSlclee "Please type \"y\" or \"n\". "); 20537c478bd9Sstevel@tonic-gate 20547c478bd9Sstevel@tonic-gate if (yesno()) { 20557c478bd9Sstevel@tonic-gate nulltbl(); 20567c478bd9Sstevel@tonic-gate } else { 20577c478bd9Sstevel@tonic-gate return (-1); 20587c478bd9Sstevel@tonic-gate } 20597c478bd9Sstevel@tonic-gate } 20607c478bd9Sstevel@tonic-gate 20617c478bd9Sstevel@tonic-gate /* create the table entry - i should be 0 */ 20627c478bd9Sstevel@tonic-gate i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, rsect, 20637c478bd9Sstevel@tonic-gate (Numcyl * heads * sectors) - rsect); 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate if (i != 0) { 2066*bb16350dSlclee (void) printf("Error creating EFI partition!!!\n"); 20677c478bd9Sstevel@tonic-gate i = -1; 20687c478bd9Sstevel@tonic-gate } else { 20697c478bd9Sstevel@tonic-gate 20707c478bd9Sstevel@tonic-gate /* EFI partitions are currently never active */ 20717c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 20727c478bd9Sstevel@tonic-gate 20737c478bd9Sstevel@tonic-gate /* set up the return code */ 20747c478bd9Sstevel@tonic-gate i = 1; 20757c478bd9Sstevel@tonic-gate } 20767c478bd9Sstevel@tonic-gate } 20777c478bd9Sstevel@tonic-gate 20787c478bd9Sstevel@tonic-gate return (i); 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate /* 20827c478bd9Sstevel@tonic-gate * specify 20837c478bd9Sstevel@tonic-gate * Query the user to specify the size of the new partition in 20847c478bd9Sstevel@tonic-gate * terms of percentage of the disk or by specifying the starting 20857c478bd9Sstevel@tonic-gate * cylinder and length in cylinders. 20867c478bd9Sstevel@tonic-gate */ 2087*bb16350dSlclee static int 2088*bb16350dSlclee specify(uchar_t tsystid) 20897c478bd9Sstevel@tonic-gate { 20907c478bd9Sstevel@tonic-gate int i, j, 20917c478bd9Sstevel@tonic-gate percent = -1; 20927c478bd9Sstevel@tonic-gate int cyl, cylen, first_free, size_free; 20937c478bd9Sstevel@tonic-gate struct ipart *partition[FD_NUMPART]; 20947c478bd9Sstevel@tonic-gate 2095*bb16350dSlclee (void) printf(Q_LINE); 2096*bb16350dSlclee (void) printf( 2097*bb16350dSlclee "Specify the percentage of disk to use for this partition\n" 2098*bb16350dSlclee "(or type \"c\" to specify the size in cylinders). "); 2099*bb16350dSlclee (void) gets(s); 21007c478bd9Sstevel@tonic-gate rm_blanks(s); 21017c478bd9Sstevel@tonic-gate if (s[0] != 'c') { /* Specify size in percentage of disk */ 21027c478bd9Sstevel@tonic-gate i = 0; 21037c478bd9Sstevel@tonic-gate while (s[i] != '\0') { 21047c478bd9Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 2105*bb16350dSlclee (void) printf(E_LINE); 2106*bb16350dSlclee (void) printf("Invalid percentage value specified; retry" 21077c478bd9Sstevel@tonic-gate " the operation."); 21087c478bd9Sstevel@tonic-gate return (-1); 21097c478bd9Sstevel@tonic-gate } 21107c478bd9Sstevel@tonic-gate i++; 21117c478bd9Sstevel@tonic-gate if (i > 3) { 2112*bb16350dSlclee (void) printf(E_LINE); 2113*bb16350dSlclee (void) printf("Invalid percentage value specified; retry" 21147c478bd9Sstevel@tonic-gate " the operation."); 21157c478bd9Sstevel@tonic-gate return (-1); 21167c478bd9Sstevel@tonic-gate } 21177c478bd9Sstevel@tonic-gate } 21187c478bd9Sstevel@tonic-gate if ((percent = atoi(s)) > 100) { 2119*bb16350dSlclee (void) printf(E_LINE); 2120*bb16350dSlclee (void) printf( 2121*bb16350dSlclee "Percentage value is too large. The value must be" 21227c478bd9Sstevel@tonic-gate " between 1 and 100;\nretry the operation.\n"); 21237c478bd9Sstevel@tonic-gate return (-1); 21247c478bd9Sstevel@tonic-gate } 21257c478bd9Sstevel@tonic-gate if (percent < 1) { 2126*bb16350dSlclee (void) printf(E_LINE); 2127*bb16350dSlclee (void) printf( 2128*bb16350dSlclee "Percentage value is too small. The value must be" 21297c478bd9Sstevel@tonic-gate " between 1 and 100;\nretry the operation.\n"); 21307c478bd9Sstevel@tonic-gate return (-1); 21317c478bd9Sstevel@tonic-gate } 21327c478bd9Sstevel@tonic-gate 21337c478bd9Sstevel@tonic-gate cylen = (Numcyl * percent) / 100; 21347c478bd9Sstevel@tonic-gate if ((percent < 100) && (((Numcyl * percent) % 10) > 5)) 21357c478bd9Sstevel@tonic-gate cylen++; 21367c478bd9Sstevel@tonic-gate 21377c478bd9Sstevel@tonic-gate /* Verify that the DOS12 partition does not exceed the maximum */ 21387c478bd9Sstevel@tonic-gate /* size of 32MB. */ 21397c478bd9Sstevel@tonic-gate if ((tsystid == DOSOS12) && ((long)((long)cylen * heads * sectors) > 21407c478bd9Sstevel@tonic-gate MAXDOS)) { 21417c478bd9Sstevel@tonic-gate int n; 21427c478bd9Sstevel@tonic-gate n = (int)(MAXDOS * 100 / (int)(heads * sectors) / Numcyl); 2143*bb16350dSlclee (void) printf(E_LINE); 2144*bb16350dSlclee (void) printf("Maximum size for a DOS partition is %d%%;" 21457c478bd9Sstevel@tonic-gate " retry the operation.", 21467c478bd9Sstevel@tonic-gate n <= 100 ? n : 100); 21477c478bd9Sstevel@tonic-gate return (-1); 21487c478bd9Sstevel@tonic-gate } 21497c478bd9Sstevel@tonic-gate 21507c478bd9Sstevel@tonic-gate /* Before searching the partitions, sort them into sector order */ 21517c478bd9Sstevel@tonic-gate /* in the partition array, note that we only need to sort */ 21527c478bd9Sstevel@tonic-gate /* NUMPART-1 entries as at least the last one must be empty */ 21537c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) partition[i] = &Table[i]; 21547c478bd9Sstevel@tonic-gate 21557c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART - 2; i++) { 21567c478bd9Sstevel@tonic-gate if (partition[i]->systid == UNUSED) break; 21577c478bd9Sstevel@tonic-gate for (j = i + 1; j < FD_NUMPART - 1; j++) { 21587c478bd9Sstevel@tonic-gate if (partition[j]->systid == UNUSED) break; 21597c478bd9Sstevel@tonic-gate if (lel(partition[j]->relsect) < 21607c478bd9Sstevel@tonic-gate lel(partition[i]->relsect)) { 21617c478bd9Sstevel@tonic-gate struct ipart *temp = partition[i]; 21627c478bd9Sstevel@tonic-gate partition[i] = partition[j]; 21637c478bd9Sstevel@tonic-gate partition[j] = temp; 21647c478bd9Sstevel@tonic-gate } 21657c478bd9Sstevel@tonic-gate } 21667c478bd9Sstevel@tonic-gate } 21677c478bd9Sstevel@tonic-gate 21687c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 21697c478bd9Sstevel@tonic-gate int last_ent = 0; 21707c478bd9Sstevel@tonic-gate 21717c478bd9Sstevel@tonic-gate /* Find start of current check area */ 21727c478bd9Sstevel@tonic-gate if (i) { /* Not an empty table */ 21737c478bd9Sstevel@tonic-gate first_free = lel(partition[i - 1]->relsect) + 21747c478bd9Sstevel@tonic-gate lel(partition[i - 1]->numsect); 21757c478bd9Sstevel@tonic-gate } else { 21767c478bd9Sstevel@tonic-gate first_free = heads * sectors; 21777c478bd9Sstevel@tonic-gate } 21787c478bd9Sstevel@tonic-gate 21797c478bd9Sstevel@tonic-gate /* Determine size of current check area */ 21807c478bd9Sstevel@tonic-gate if (partition[i]->systid == UNUSED) { 21817c478bd9Sstevel@tonic-gate /* Special case hack for whole unused disk */ 21827c478bd9Sstevel@tonic-gate if (percent == 100 && i == 0) 21837c478bd9Sstevel@tonic-gate cylen--; 21847c478bd9Sstevel@tonic-gate size_free = (Numcyl * heads * sectors) - first_free; 21857c478bd9Sstevel@tonic-gate last_ent++; 21867c478bd9Sstevel@tonic-gate } else { 21877c478bd9Sstevel@tonic-gate if (i && ((lel(partition[i - 1]->relsect) + 21887c478bd9Sstevel@tonic-gate lel(partition[i - 1]->numsect)) != 21897c478bd9Sstevel@tonic-gate lel(partition[i]->relsect))) { 21907c478bd9Sstevel@tonic-gate /* There is a hole in table */ 21917c478bd9Sstevel@tonic-gate size_free = lel(partition[i]->relsect) - 21927c478bd9Sstevel@tonic-gate (lel(partition[i - 1]->relsect) + 21937c478bd9Sstevel@tonic-gate lel(partition[i - 1]->numsect)); 21947c478bd9Sstevel@tonic-gate } else if (i == 0) { 21957c478bd9Sstevel@tonic-gate size_free = lel(partition[i]->relsect) - 21967c478bd9Sstevel@tonic-gate heads * sectors; 21977c478bd9Sstevel@tonic-gate } else { 21987c478bd9Sstevel@tonic-gate size_free = 0; 21997c478bd9Sstevel@tonic-gate } 22007c478bd9Sstevel@tonic-gate } 22017c478bd9Sstevel@tonic-gate 22027c478bd9Sstevel@tonic-gate if ((cylen * heads * sectors) <= size_free) { 22037c478bd9Sstevel@tonic-gate /* We found a place to use */ 22047c478bd9Sstevel@tonic-gate break; 22057c478bd9Sstevel@tonic-gate } else if (last_ent) { 22067c478bd9Sstevel@tonic-gate size_free = 0; 22077c478bd9Sstevel@tonic-gate break; 22087c478bd9Sstevel@tonic-gate } 22097c478bd9Sstevel@tonic-gate } 22107c478bd9Sstevel@tonic-gate if (i < FD_NUMPART && size_free) { 2211*bb16350dSlclee (void) printf(E_LINE); 22127c478bd9Sstevel@tonic-gate if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 22137c478bd9Sstevel@tonic-gate first_free, cylen * heads * sectors)) < 0) { 2214*bb16350dSlclee (void) fprintf(stderr, 22157c478bd9Sstevel@tonic-gate "fdisk: Partition entry too big.\n"); 22167c478bd9Sstevel@tonic-gate return (-1); 22177c478bd9Sstevel@tonic-gate } 22187c478bd9Sstevel@tonic-gate } else { 2219*bb16350dSlclee (void) printf(E_LINE); 2220*bb16350dSlclee (void) fprintf(stderr, "fdisk: Partition entry too big.\n"); 22217c478bd9Sstevel@tonic-gate i = -1; 22227c478bd9Sstevel@tonic-gate } 22237c478bd9Sstevel@tonic-gate return (i); 22247c478bd9Sstevel@tonic-gate } else { /* Specifying size in cylinders */ 22257c478bd9Sstevel@tonic-gate 2226*bb16350dSlclee (void) printf(E_LINE); 2227*bb16350dSlclee (void) printf(Q_LINE); 2228*bb16350dSlclee (void) printf("Enter starting cylinder number: "); 22297c478bd9Sstevel@tonic-gate if ((cyl = getcyl()) == -1) { 2230*bb16350dSlclee (void) printf(E_LINE); 2231*bb16350dSlclee (void) printf("Invalid number; retry the operation."); 22327c478bd9Sstevel@tonic-gate return (-1); 22337c478bd9Sstevel@tonic-gate } 22347c478bd9Sstevel@tonic-gate if (cyl == 0) { 2235*bb16350dSlclee (void) printf(E_LINE); 2236*bb16350dSlclee (void) printf("New partition cannot start at cylinder 0.\n"); 22377c478bd9Sstevel@tonic-gate return (-1); 22387c478bd9Sstevel@tonic-gate } 22397c478bd9Sstevel@tonic-gate if (cyl >= (unsigned int)Numcyl) { 2240*bb16350dSlclee (void) printf(E_LINE); 2241*bb16350dSlclee (void) printf( 2242*bb16350dSlclee "Cylinder %d is out of bounds, the maximum is %d.\n", 22437c478bd9Sstevel@tonic-gate cyl, Numcyl - 1); 22447c478bd9Sstevel@tonic-gate return (-1); 22457c478bd9Sstevel@tonic-gate } 2246*bb16350dSlclee (void) printf(Q_LINE); 2247*bb16350dSlclee (void) printf("Enter partition size in cylinders: "); 22487c478bd9Sstevel@tonic-gate if ((cylen = getcyl()) == -1) { 2249*bb16350dSlclee (void) printf(E_LINE); 2250*bb16350dSlclee (void) printf("Invalid number, retry the operation."); 22517c478bd9Sstevel@tonic-gate return (-1); 22527c478bd9Sstevel@tonic-gate } 22537c478bd9Sstevel@tonic-gate 22547c478bd9Sstevel@tonic-gate /* Verify that the DOS12 partition does not exceed the maximum */ 22557c478bd9Sstevel@tonic-gate /* size of 32MB. */ 22567c478bd9Sstevel@tonic-gate if ((tsystid == DOSOS12) && 22577c478bd9Sstevel@tonic-gate ((long)((long)cylen * heads * sectors) > MAXDOS)) { 2258*bb16350dSlclee (void) printf(E_LINE); 2259*bb16350dSlclee (void) printf( 2260*bb16350dSlclee "Maximum size for a %s partition is %ld cylinders;" 22617c478bd9Sstevel@tonic-gate "\nretry the operation.", 22627c478bd9Sstevel@tonic-gate Dstr, MAXDOS / (int)(heads * sectors)); 22637c478bd9Sstevel@tonic-gate return (-1); 22647c478bd9Sstevel@tonic-gate } 22657c478bd9Sstevel@tonic-gate 22667c478bd9Sstevel@tonic-gate i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, cyl * heads * sectors, 22677c478bd9Sstevel@tonic-gate cylen * heads * sectors); 22687c478bd9Sstevel@tonic-gate 22697c478bd9Sstevel@tonic-gate if (verify_tbl() < 0) { 2270*bb16350dSlclee (void) printf(E_LINE); 2271*bb16350dSlclee (void) printf("fdisk: Cannot create partition table\n"); 22727c478bd9Sstevel@tonic-gate return (-1); 22737c478bd9Sstevel@tonic-gate } 22747c478bd9Sstevel@tonic-gate 22757c478bd9Sstevel@tonic-gate return (i); 22767c478bd9Sstevel@tonic-gate } 22777c478bd9Sstevel@tonic-gate } 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate /* 22807c478bd9Sstevel@tonic-gate * dispmenu 22817c478bd9Sstevel@tonic-gate * Display command menu (interactive mode). 22827c478bd9Sstevel@tonic-gate */ 2283*bb16350dSlclee static void 2284*bb16350dSlclee dispmenu(void) 22857c478bd9Sstevel@tonic-gate { 2286*bb16350dSlclee (void) printf(M_LINE); 2287*bb16350dSlclee (void) printf( 2288*bb16350dSlclee "SELECT ONE OF THE FOLLOWING:\n" 2289*bb16350dSlclee " 1. Create a partition\n" 2290*bb16350dSlclee " 2. Specify the active partition\n" 2291*bb16350dSlclee " 3. Delete a partition\n" 2292*bb16350dSlclee " 4. Change between Solaris and Solaris2 Partition IDs\n" 2293*bb16350dSlclee " 5. Exit (update disk configuration and exit)\n" 2294*bb16350dSlclee " 6. Cancel (exit without updating disk configuration)\n"); 22957c478bd9Sstevel@tonic-gate } 22967c478bd9Sstevel@tonic-gate 22977c478bd9Sstevel@tonic-gate /* 22987c478bd9Sstevel@tonic-gate * pchange 22997c478bd9Sstevel@tonic-gate * Change the ACTIVE designation of a partition. 23007c478bd9Sstevel@tonic-gate */ 2301*bb16350dSlclee static int 2302*bb16350dSlclee pchange(void) 23037c478bd9Sstevel@tonic-gate { 23047c478bd9Sstevel@tonic-gate char s[80]; 23057c478bd9Sstevel@tonic-gate int i, j; 23067c478bd9Sstevel@tonic-gate 2307*bb16350dSlclee for (;;) { 2308*bb16350dSlclee (void) printf(Q_LINE); 23097c478bd9Sstevel@tonic-gate { 2310*bb16350dSlclee (void) printf( 2311*bb16350dSlclee "Specify the partition number to boot from" 23127c478bd9Sstevel@tonic-gate " (or specify 0 for none): "); 23137c478bd9Sstevel@tonic-gate } 2314*bb16350dSlclee (void) gets(s); 23157c478bd9Sstevel@tonic-gate rm_blanks(s); 23167c478bd9Sstevel@tonic-gate if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) { 2317*bb16350dSlclee (void) printf(E_LINE); 2318*bb16350dSlclee (void) printf( 2319*bb16350dSlclee "Invalid response, please specify a number" 23207c478bd9Sstevel@tonic-gate " between 0 and 4.\n"); 23217c478bd9Sstevel@tonic-gate } else { 23227c478bd9Sstevel@tonic-gate break; 23237c478bd9Sstevel@tonic-gate } 23247c478bd9Sstevel@tonic-gate } 23257c478bd9Sstevel@tonic-gate if (s[0] == '0') { /* No active partitions */ 23267c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 23277c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED && 23287c478bd9Sstevel@tonic-gate Table[i].bootid == ACTIVE) 23297c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 23307c478bd9Sstevel@tonic-gate } 2331*bb16350dSlclee (void) printf(E_LINE); 2332*bb16350dSlclee (void) printf( 2333*bb16350dSlclee "No partition is currently marked as active."); 23347c478bd9Sstevel@tonic-gate return (0); 23357c478bd9Sstevel@tonic-gate } else { /* User has selected a partition to be active */ 23367c478bd9Sstevel@tonic-gate i = s[0] - '1'; 23377c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2338*bb16350dSlclee (void) printf(E_LINE); 2339*bb16350dSlclee (void) printf("Partition does not exist."); 23407c478bd9Sstevel@tonic-gate return (-1); 23417c478bd9Sstevel@tonic-gate } 23427c478bd9Sstevel@tonic-gate /* a DOS-DATA or EXT-DOS partition cannot be active */ 23437c478bd9Sstevel@tonic-gate else if ((Table[i].systid == DOSDATA) || 23447c478bd9Sstevel@tonic-gate (Table[i].systid == EXTDOS) || 23457c478bd9Sstevel@tonic-gate (Table[i].systid == FDISK_EXTLBA)) { 2346*bb16350dSlclee (void) printf(E_LINE); 2347*bb16350dSlclee (void) printf( 2348*bb16350dSlclee "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions " 23497c478bd9Sstevel@tonic-gate "cannot be made active.\n"); 2350*bb16350dSlclee (void) printf("Select another partition."); 23517c478bd9Sstevel@tonic-gate return (-1); 23527c478bd9Sstevel@tonic-gate } 23537c478bd9Sstevel@tonic-gate Table[i].bootid = ACTIVE; 23547c478bd9Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 23557c478bd9Sstevel@tonic-gate if (j != i) 23567c478bd9Sstevel@tonic-gate Table[j].bootid = 0; 23577c478bd9Sstevel@tonic-gate } 23587c478bd9Sstevel@tonic-gate } 2359*bb16350dSlclee (void) printf(E_LINE); 23607c478bd9Sstevel@tonic-gate { 2361*bb16350dSlclee (void) printf( 2362*bb16350dSlclee "Partition %d is now active. The system will start up" 23637c478bd9Sstevel@tonic-gate " from this\n", i + 1); 2364*bb16350dSlclee (void) printf("partition after the next reboot."); 23657c478bd9Sstevel@tonic-gate } 23667c478bd9Sstevel@tonic-gate return (1); 23677c478bd9Sstevel@tonic-gate } 23687c478bd9Sstevel@tonic-gate 23697c478bd9Sstevel@tonic-gate /* 23707c478bd9Sstevel@tonic-gate * Change between SOLARIS and SOLARIS2 partition id 23717c478bd9Sstevel@tonic-gate */ 2372*bb16350dSlclee static int 2373*bb16350dSlclee ppartid(void) 23747c478bd9Sstevel@tonic-gate { 23757c478bd9Sstevel@tonic-gate char *p, s[80]; 23767c478bd9Sstevel@tonic-gate int i; 23777c478bd9Sstevel@tonic-gate 23787c478bd9Sstevel@tonic-gate for (;;) { 2379*bb16350dSlclee (void) printf(Q_LINE); 2380*bb16350dSlclee (void) printf("Specify the partition number to change" 23817c478bd9Sstevel@tonic-gate " (or enter 0 to exit): "); 2382*bb16350dSlclee if (!fgets(s, sizeof (s), stdin)) 2383*bb16350dSlclee return (1); 23847c478bd9Sstevel@tonic-gate i = strtol(s, &p, 10); 23857c478bd9Sstevel@tonic-gate 23867c478bd9Sstevel@tonic-gate if (*p != '\n' || i < 0 || i > FD_NUMPART) { 2387*bb16350dSlclee (void) printf(E_LINE); 2388*bb16350dSlclee (void) printf( 2389*bb16350dSlclee "Invalid response, retry the operation.\n"); 23907c478bd9Sstevel@tonic-gate continue; 23917c478bd9Sstevel@tonic-gate } 23927c478bd9Sstevel@tonic-gate 23937c478bd9Sstevel@tonic-gate if (i == 0) { 23947c478bd9Sstevel@tonic-gate /* exit delete command */ 2395*bb16350dSlclee (void) printf(E_LINE); /* clear error message */ 23967c478bd9Sstevel@tonic-gate return (1); 23977c478bd9Sstevel@tonic-gate } 23987c478bd9Sstevel@tonic-gate 23997c478bd9Sstevel@tonic-gate i -= 1; 24007c478bd9Sstevel@tonic-gate if (Table[i].systid == SUNIXOS) { 24017c478bd9Sstevel@tonic-gate Table[i].systid = SUNIXOS2; 24027c478bd9Sstevel@tonic-gate } else if (Table[i].systid == SUNIXOS2) { 24037c478bd9Sstevel@tonic-gate Table[i].systid = SUNIXOS; 24047c478bd9Sstevel@tonic-gate } else { 2405*bb16350dSlclee (void) printf(E_LINE); 2406*bb16350dSlclee (void) printf( 2407*bb16350dSlclee "Partition %d is not a Solaris partition.", 24087c478bd9Sstevel@tonic-gate i + 1); 24097c478bd9Sstevel@tonic-gate continue; 24107c478bd9Sstevel@tonic-gate } 24117c478bd9Sstevel@tonic-gate 2412*bb16350dSlclee (void) printf(E_LINE); 2413*bb16350dSlclee (void) printf("Partition %d has been changed.", i + 1); 24147c478bd9Sstevel@tonic-gate return (1); 24157c478bd9Sstevel@tonic-gate } 24167c478bd9Sstevel@tonic-gate } 24177c478bd9Sstevel@tonic-gate 24187c478bd9Sstevel@tonic-gate /* 24197c478bd9Sstevel@tonic-gate * pdelete 24207c478bd9Sstevel@tonic-gate * Remove partition entry from the table (interactive mode). 24217c478bd9Sstevel@tonic-gate */ 2422*bb16350dSlclee static char 2423*bb16350dSlclee pdelete(void) 24247c478bd9Sstevel@tonic-gate { 24257c478bd9Sstevel@tonic-gate char s[80]; 24267c478bd9Sstevel@tonic-gate int i, j; 24277c478bd9Sstevel@tonic-gate char pactive; 24287c478bd9Sstevel@tonic-gate 2429*bb16350dSlclee DEL1: (void) printf(Q_LINE); 2430*bb16350dSlclee (void) printf("Specify the partition number to delete" 24317c478bd9Sstevel@tonic-gate " (or enter 0 to exit): "); 2432*bb16350dSlclee (void) gets(s); 24337c478bd9Sstevel@tonic-gate rm_blanks(s); 24347c478bd9Sstevel@tonic-gate if ((s[0] == '0')) { /* exit delete command */ 2435*bb16350dSlclee (void) printf(E_LINE); /* clear error message */ 24367c478bd9Sstevel@tonic-gate return (1); 24377c478bd9Sstevel@tonic-gate } 24387c478bd9Sstevel@tonic-gate /* Accept only a single digit between 1 and 4 */ 24397c478bd9Sstevel@tonic-gate if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) { 2440*bb16350dSlclee (void) printf(E_LINE); 2441*bb16350dSlclee (void) printf("Invalid response, retry the operation.\n"); 24427c478bd9Sstevel@tonic-gate goto DEL1; 24437c478bd9Sstevel@tonic-gate } else { /* Found a digit between 1 and 4 */ 24447c478bd9Sstevel@tonic-gate --i; /* Structure begins with element 0 */ 24457c478bd9Sstevel@tonic-gate } 24467c478bd9Sstevel@tonic-gate 24477c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2448*bb16350dSlclee (void) printf(E_LINE); 2449*bb16350dSlclee (void) printf("Partition %d does not exist.", i + 1); 24507c478bd9Sstevel@tonic-gate return (-1); 24517c478bd9Sstevel@tonic-gate } 24527c478bd9Sstevel@tonic-gate 2453*bb16350dSlclee (void) printf(Q_LINE); 2454*bb16350dSlclee (void) printf("Are you sure you want to delete partition %d?" 24557c478bd9Sstevel@tonic-gate " This will make all files and \n", i + 1); 2456*bb16350dSlclee (void) printf("programs in this partition inaccessible (type" 24577c478bd9Sstevel@tonic-gate " \"y\" or \"n\"). "); 24587c478bd9Sstevel@tonic-gate 2459*bb16350dSlclee (void) printf(E_LINE); 24607c478bd9Sstevel@tonic-gate if (! yesno()) { 24617c478bd9Sstevel@tonic-gate return (1); 24627c478bd9Sstevel@tonic-gate } 24637c478bd9Sstevel@tonic-gate 24647c478bd9Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) { 24657c478bd9Sstevel@tonic-gate pactive = 1; 24667c478bd9Sstevel@tonic-gate } else { 24677c478bd9Sstevel@tonic-gate pactive = 0; 24687c478bd9Sstevel@tonic-gate } 24697c478bd9Sstevel@tonic-gate 24707c478bd9Sstevel@tonic-gate for (j = i; j < FD_NUMPART - 1; j++) { 24717c478bd9Sstevel@tonic-gate Table[j] = Table[j + 1]; 24727c478bd9Sstevel@tonic-gate } 24737c478bd9Sstevel@tonic-gate 24747c478bd9Sstevel@tonic-gate Table[j].systid = UNUSED; 24757c478bd9Sstevel@tonic-gate Table[j].numsect = 0; 24767c478bd9Sstevel@tonic-gate Table[j].relsect = 0; 24777c478bd9Sstevel@tonic-gate Table[j].bootid = 0; 2478*bb16350dSlclee (void) printf(E_LINE); 2479*bb16350dSlclee (void) printf("Partition %d has been deleted.", i + 1); 24807c478bd9Sstevel@tonic-gate 24817c478bd9Sstevel@tonic-gate if (pactive) { 2482*bb16350dSlclee (void) printf(" This was the active partition."); 24837c478bd9Sstevel@tonic-gate } 24847c478bd9Sstevel@tonic-gate 24857c478bd9Sstevel@tonic-gate return (1); 24867c478bd9Sstevel@tonic-gate } 24877c478bd9Sstevel@tonic-gate 24887c478bd9Sstevel@tonic-gate /* 24897c478bd9Sstevel@tonic-gate * rm_blanks 24907c478bd9Sstevel@tonic-gate * Remove blanks from strings of user responses. 24917c478bd9Sstevel@tonic-gate */ 2492*bb16350dSlclee static void 2493*bb16350dSlclee rm_blanks(char *s) 24947c478bd9Sstevel@tonic-gate { 24957c478bd9Sstevel@tonic-gate register int i, j; 24967c478bd9Sstevel@tonic-gate 24977c478bd9Sstevel@tonic-gate for (i = 0; i < CBUFLEN; i++) { 24987c478bd9Sstevel@tonic-gate if ((s[i] == ' ') || (s[i] == '\t')) 24997c478bd9Sstevel@tonic-gate continue; 25007c478bd9Sstevel@tonic-gate else 25017c478bd9Sstevel@tonic-gate /* Found first non-blank character of the string */ 25027c478bd9Sstevel@tonic-gate break; 25037c478bd9Sstevel@tonic-gate } 25047c478bd9Sstevel@tonic-gate for (j = 0; i < CBUFLEN; j++, i++) { 25057c478bd9Sstevel@tonic-gate if ((s[j] = s[i]) == '\0') { 25067c478bd9Sstevel@tonic-gate /* Reached end of string */ 25077c478bd9Sstevel@tonic-gate return; 25087c478bd9Sstevel@tonic-gate } 25097c478bd9Sstevel@tonic-gate } 25107c478bd9Sstevel@tonic-gate } 25117c478bd9Sstevel@tonic-gate 25127c478bd9Sstevel@tonic-gate /* 25137c478bd9Sstevel@tonic-gate * getcyl 25147c478bd9Sstevel@tonic-gate * Take the user-specified cylinder number and convert it from a 25157c478bd9Sstevel@tonic-gate * string to a decimal value. 25167c478bd9Sstevel@tonic-gate */ 2517*bb16350dSlclee static int 2518*bb16350dSlclee getcyl(void) 25197c478bd9Sstevel@tonic-gate { 25207c478bd9Sstevel@tonic-gate int slen, i, j; 25217c478bd9Sstevel@tonic-gate unsigned int cyl; 2522*bb16350dSlclee (void) gets(s); 25237c478bd9Sstevel@tonic-gate rm_blanks(s); 25247c478bd9Sstevel@tonic-gate slen = strlen(s); 25257c478bd9Sstevel@tonic-gate j = 1; 25267c478bd9Sstevel@tonic-gate cyl = 0; 25277c478bd9Sstevel@tonic-gate for (i = slen - 1; i >= 0; i--) { 25287c478bd9Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 25297c478bd9Sstevel@tonic-gate return (-1); 25307c478bd9Sstevel@tonic-gate } 25317c478bd9Sstevel@tonic-gate cyl += (j * (s[i] - '0')); 25327c478bd9Sstevel@tonic-gate j *= 10; 25337c478bd9Sstevel@tonic-gate } 25347c478bd9Sstevel@tonic-gate return (cyl); 25357c478bd9Sstevel@tonic-gate } 25367c478bd9Sstevel@tonic-gate 25377c478bd9Sstevel@tonic-gate /* 25387c478bd9Sstevel@tonic-gate * disptbl 25397c478bd9Sstevel@tonic-gate * Display the current fdisk table; determine percentage 25407c478bd9Sstevel@tonic-gate * of the disk used for each partition. 25417c478bd9Sstevel@tonic-gate */ 2542*bb16350dSlclee static void 2543*bb16350dSlclee disptbl(void) 25447c478bd9Sstevel@tonic-gate { 25457c478bd9Sstevel@tonic-gate int i; 25467c478bd9Sstevel@tonic-gate unsigned int startcyl, endcyl, length, percent, remainder; 25477c478bd9Sstevel@tonic-gate char *stat, *type; 25487c478bd9Sstevel@tonic-gate 25497c478bd9Sstevel@tonic-gate if ((heads == 0) || (sectors == 0)) { 2550*bb16350dSlclee (void) printf("WARNING: critical disk geometry information" 25517c478bd9Sstevel@tonic-gate " missing!\n"); 2552*bb16350dSlclee (void) printf("\theads = %d, sectors = %d\n", heads, sectors); 25537c478bd9Sstevel@tonic-gate exit(1); 25547c478bd9Sstevel@tonic-gate } 25557c478bd9Sstevel@tonic-gate 2556*bb16350dSlclee (void) printf(HOME); 2557*bb16350dSlclee (void) printf(T_LINE); 2558*bb16350dSlclee (void) printf(" Total disk size is %d cylinders\n", Numcyl); 2559*bb16350dSlclee (void) printf(" Cylinder size is %d (512 byte) blocks\n\n", 25607c478bd9Sstevel@tonic-gate heads * sectors); 2561*bb16350dSlclee (void) printf( 2562*bb16350dSlclee " Cylinders\n"); 2563*bb16350dSlclee (void) printf( 2564*bb16350dSlclee " Partition Status Type Start End Length" 25657c478bd9Sstevel@tonic-gate " %%\n"); 2566*bb16350dSlclee (void) printf( 2567*bb16350dSlclee " ========= ====== ============ ===== === ======" 25687c478bd9Sstevel@tonic-gate " ==="); 25697c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 25707c478bd9Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2571*bb16350dSlclee (void) printf("\n"); 2572*bb16350dSlclee (void) printf(CLR_LIN); 25737c478bd9Sstevel@tonic-gate continue; 25747c478bd9Sstevel@tonic-gate } 25757c478bd9Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) 25767c478bd9Sstevel@tonic-gate stat = Actvstr; 25777c478bd9Sstevel@tonic-gate else 25787c478bd9Sstevel@tonic-gate stat = NAstr; 25797c478bd9Sstevel@tonic-gate switch (Table[i].systid) { 25807c478bd9Sstevel@tonic-gate case UNIXOS: 25817c478bd9Sstevel@tonic-gate type = Ustr; 25827c478bd9Sstevel@tonic-gate break; 25837c478bd9Sstevel@tonic-gate case SUNIXOS: 25847c478bd9Sstevel@tonic-gate type = SUstr; 25857c478bd9Sstevel@tonic-gate break; 25867c478bd9Sstevel@tonic-gate case SUNIXOS2: 25877c478bd9Sstevel@tonic-gate type = SU2str; 25887c478bd9Sstevel@tonic-gate break; 25897c478bd9Sstevel@tonic-gate case X86BOOT: 25907c478bd9Sstevel@tonic-gate type = X86str; 25917c478bd9Sstevel@tonic-gate break; 25927c478bd9Sstevel@tonic-gate case DOSOS12: 25937c478bd9Sstevel@tonic-gate type = Dstr; 25947c478bd9Sstevel@tonic-gate break; 25957c478bd9Sstevel@tonic-gate case DOSOS16: 25967c478bd9Sstevel@tonic-gate type = D16str; 25977c478bd9Sstevel@tonic-gate break; 25987c478bd9Sstevel@tonic-gate case EXTDOS: 25997c478bd9Sstevel@tonic-gate type = EDstr; 26007c478bd9Sstevel@tonic-gate break; 26017c478bd9Sstevel@tonic-gate case DOSDATA: 26027c478bd9Sstevel@tonic-gate type = DDstr; 26037c478bd9Sstevel@tonic-gate break; 26047c478bd9Sstevel@tonic-gate case DOSHUGE: 26057c478bd9Sstevel@tonic-gate type = DBstr; 26067c478bd9Sstevel@tonic-gate break; 26077c478bd9Sstevel@tonic-gate case PCIXOS: 26087c478bd9Sstevel@tonic-gate type = PCstr; 26097c478bd9Sstevel@tonic-gate break; 26107c478bd9Sstevel@tonic-gate case DIAGPART: 26117c478bd9Sstevel@tonic-gate type = DIAGstr; 26127c478bd9Sstevel@tonic-gate break; 26137c478bd9Sstevel@tonic-gate case FDISK_IFS: 26147c478bd9Sstevel@tonic-gate type = IFSstr; 26157c478bd9Sstevel@tonic-gate break; 26167c478bd9Sstevel@tonic-gate case FDISK_AIXBOOT: 26177c478bd9Sstevel@tonic-gate type = AIXstr; 26187c478bd9Sstevel@tonic-gate break; 26197c478bd9Sstevel@tonic-gate case FDISK_AIXDATA: 26207c478bd9Sstevel@tonic-gate type = AIXDstr; 26217c478bd9Sstevel@tonic-gate break; 26227c478bd9Sstevel@tonic-gate case FDISK_OS2BOOT: 26237c478bd9Sstevel@tonic-gate type = OS2str; 26247c478bd9Sstevel@tonic-gate break; 26257c478bd9Sstevel@tonic-gate case FDISK_WINDOWS: 26267c478bd9Sstevel@tonic-gate type = WINstr; 26277c478bd9Sstevel@tonic-gate break; 26287c478bd9Sstevel@tonic-gate case FDISK_EXT_WIN: 26297c478bd9Sstevel@tonic-gate type = EWINstr; 26307c478bd9Sstevel@tonic-gate break; 26317c478bd9Sstevel@tonic-gate case FDISK_FAT95: 26327c478bd9Sstevel@tonic-gate type = FAT95str; 26337c478bd9Sstevel@tonic-gate break; 26347c478bd9Sstevel@tonic-gate case FDISK_EXTLBA: 26357c478bd9Sstevel@tonic-gate type = EXTLstr; 26367c478bd9Sstevel@tonic-gate break; 26377c478bd9Sstevel@tonic-gate case FDISK_LINUX: 26387c478bd9Sstevel@tonic-gate type = LINUXstr; 26397c478bd9Sstevel@tonic-gate break; 26407c478bd9Sstevel@tonic-gate case FDISK_CPM: 26417c478bd9Sstevel@tonic-gate type = CPMstr; 26427c478bd9Sstevel@tonic-gate break; 26437c478bd9Sstevel@tonic-gate case FDISK_NOVELL3: 26447c478bd9Sstevel@tonic-gate type = NOVstr; 26457c478bd9Sstevel@tonic-gate break; 26467c478bd9Sstevel@tonic-gate case FDISK_QNX4: 26477c478bd9Sstevel@tonic-gate type = QNXstr; 26487c478bd9Sstevel@tonic-gate break; 26497c478bd9Sstevel@tonic-gate case FDISK_QNX42: 26507c478bd9Sstevel@tonic-gate type = QNX2str; 26517c478bd9Sstevel@tonic-gate break; 26527c478bd9Sstevel@tonic-gate case FDISK_QNX43: 26537c478bd9Sstevel@tonic-gate type = QNX3str; 26547c478bd9Sstevel@tonic-gate break; 26557c478bd9Sstevel@tonic-gate case FDISK_LINUXNAT: 26567c478bd9Sstevel@tonic-gate type = LINNATstr; 26577c478bd9Sstevel@tonic-gate break; 26587c478bd9Sstevel@tonic-gate case FDISK_NTFSVOL1: 26597c478bd9Sstevel@tonic-gate type = NTFSVOL1str; 26607c478bd9Sstevel@tonic-gate break; 26617c478bd9Sstevel@tonic-gate case FDISK_NTFSVOL2: 26627c478bd9Sstevel@tonic-gate type = NTFSVOL2str; 26637c478bd9Sstevel@tonic-gate break; 26647c478bd9Sstevel@tonic-gate case FDISK_BSD: 26657c478bd9Sstevel@tonic-gate type = BSDstr; 26667c478bd9Sstevel@tonic-gate break; 26677c478bd9Sstevel@tonic-gate case FDISK_NEXTSTEP: 26687c478bd9Sstevel@tonic-gate type = NEXTSTEPstr; 26697c478bd9Sstevel@tonic-gate break; 26707c478bd9Sstevel@tonic-gate case FDISK_BSDIFS: 26717c478bd9Sstevel@tonic-gate type = BSDIFSstr; 26727c478bd9Sstevel@tonic-gate break; 26737c478bd9Sstevel@tonic-gate case FDISK_BSDISWAP: 26747c478bd9Sstevel@tonic-gate type = BSDISWAPstr; 26757c478bd9Sstevel@tonic-gate break; 26767c478bd9Sstevel@tonic-gate case EFI_PMBR: 26777c478bd9Sstevel@tonic-gate type = EFIstr; 26787c478bd9Sstevel@tonic-gate break; 26797c478bd9Sstevel@tonic-gate default: 26807c478bd9Sstevel@tonic-gate type = Ostr; 26817c478bd9Sstevel@tonic-gate break; 26827c478bd9Sstevel@tonic-gate } 26837c478bd9Sstevel@tonic-gate startcyl = lel(Table[i].relsect) / (heads * sectors); 26847c478bd9Sstevel@tonic-gate length = lel(Table[i].numsect) / (long)(heads * sectors); 26857c478bd9Sstevel@tonic-gate if (lel(Table[i].numsect) % (long)(heads * sectors)) 26867c478bd9Sstevel@tonic-gate length++; 26877c478bd9Sstevel@tonic-gate endcyl = startcyl + length - 1; 26887c478bd9Sstevel@tonic-gate percent = length * 100 / Numcyl; 26897c478bd9Sstevel@tonic-gate if ((remainder = (length * 100 % Numcyl)) != 0) { 26907c478bd9Sstevel@tonic-gate if ((remainder * 100 / Numcyl) > 50) { 26917c478bd9Sstevel@tonic-gate /* round up */ 26927c478bd9Sstevel@tonic-gate percent++; 26937c478bd9Sstevel@tonic-gate } 26947c478bd9Sstevel@tonic-gate /* Else leave the percent as is since it's already */ 26957c478bd9Sstevel@tonic-gate /* rounded down */ 26967c478bd9Sstevel@tonic-gate } 26977c478bd9Sstevel@tonic-gate if (percent > 100) 26987c478bd9Sstevel@tonic-gate percent = 100; 2699*bb16350dSlclee (void) printf( 2700*bb16350dSlclee "\n %d %s %-12.12s %4d %4d %4d" 2701*bb16350dSlclee " %3d", 2702*bb16350dSlclee i + 1, stat, type, startcyl, endcyl, length, percent); 27037c478bd9Sstevel@tonic-gate } 27047c478bd9Sstevel@tonic-gate /* Print warning message if table is empty */ 27057c478bd9Sstevel@tonic-gate if (Table[0].systid == UNUSED) { 2706*bb16350dSlclee (void) printf(W_LINE); 2707*bb16350dSlclee (void) printf("WARNING: no partitions are defined!"); 27087c478bd9Sstevel@tonic-gate } else { 27097c478bd9Sstevel@tonic-gate /* Clear the warning line */ 2710*bb16350dSlclee (void) printf(W_LINE); 27117c478bd9Sstevel@tonic-gate } 27127c478bd9Sstevel@tonic-gate } 27137c478bd9Sstevel@tonic-gate 27147c478bd9Sstevel@tonic-gate /* 27157c478bd9Sstevel@tonic-gate * print_Table 27167c478bd9Sstevel@tonic-gate * Write the detailed fdisk table to standard error for 27177c478bd9Sstevel@tonic-gate * the selected disk device. 27187c478bd9Sstevel@tonic-gate */ 2719*bb16350dSlclee static void 2720*bb16350dSlclee print_Table(void) 2721*bb16350dSlclee { 27227c478bd9Sstevel@tonic-gate int i; 27237c478bd9Sstevel@tonic-gate 2724*bb16350dSlclee (void) fprintf(stderr, 27257c478bd9Sstevel@tonic-gate " SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT" 27267c478bd9Sstevel@tonic-gate " NUMSECT\n"); 27277c478bd9Sstevel@tonic-gate 27287c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2729*bb16350dSlclee (void) fprintf(stderr, " %-5d ", Table[i].systid); 2730*bb16350dSlclee (void) fprintf(stderr, "%-3d ", Table[i].bootid); 2731*bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].beghead); 2732*bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f); 2733*bb16350dSlclee (void) fprintf(stderr, "%-8d ", (((uint_t)Table[i].begsect & 27347c478bd9Sstevel@tonic-gate 0xc0) << 2) + Table[i].begcyl); 27357c478bd9Sstevel@tonic-gate 2736*bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].endhead); 2737*bb16350dSlclee (void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f); 2738*bb16350dSlclee (void) fprintf(stderr, "%-8d ", (((uint_t)Table[i].endsect & 27397c478bd9Sstevel@tonic-gate 0xc0) << 2) + Table[i].endcyl); 2740*bb16350dSlclee (void) fprintf(stderr, "%-9d ", lel(Table[i].relsect)); 2741*bb16350dSlclee (void) fprintf(stderr, "%-9d\n", lel(Table[i].numsect)); 27427c478bd9Sstevel@tonic-gate 27437c478bd9Sstevel@tonic-gate } 27447c478bd9Sstevel@tonic-gate } 27457c478bd9Sstevel@tonic-gate 27467c478bd9Sstevel@tonic-gate /* 27477c478bd9Sstevel@tonic-gate * copy_Table_to_Old_Table 27487c478bd9Sstevel@tonic-gate * Copy Table into Old_Table. The function only copies the systid, 27497c478bd9Sstevel@tonic-gate * numsect, relsect, and bootid values because they are the only 27507c478bd9Sstevel@tonic-gate * ones compared when determining if Table has changed. 27517c478bd9Sstevel@tonic-gate */ 2752*bb16350dSlclee static void 2753*bb16350dSlclee copy_Table_to_Old_Table(void) 27547c478bd9Sstevel@tonic-gate { 27557c478bd9Sstevel@tonic-gate int i; 27567c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2757*bb16350dSlclee (void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0])); 27587c478bd9Sstevel@tonic-gate } 27597c478bd9Sstevel@tonic-gate } 27607c478bd9Sstevel@tonic-gate 27617c478bd9Sstevel@tonic-gate /* 27627c478bd9Sstevel@tonic-gate * nulltbl 27637c478bd9Sstevel@tonic-gate * Zero out the systid, numsect, relsect, and bootid values in the 27647c478bd9Sstevel@tonic-gate * fdisk table. 27657c478bd9Sstevel@tonic-gate */ 2766*bb16350dSlclee static void 2767*bb16350dSlclee nulltbl(void) 27687c478bd9Sstevel@tonic-gate { 27697c478bd9Sstevel@tonic-gate int i; 27707c478bd9Sstevel@tonic-gate 27717c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 27727c478bd9Sstevel@tonic-gate Table[i].systid = UNUSED; 27737c478bd9Sstevel@tonic-gate Table[i].numsect = lel(UNUSED); 27747c478bd9Sstevel@tonic-gate Table[i].relsect = lel(UNUSED); 27757c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 27767c478bd9Sstevel@tonic-gate } 27777c478bd9Sstevel@tonic-gate } 27787c478bd9Sstevel@tonic-gate 27797c478bd9Sstevel@tonic-gate /* 27807c478bd9Sstevel@tonic-gate * copy_Bootblk_to_Table 27817c478bd9Sstevel@tonic-gate * Copy the bytes from the boot record to an internal "Table". 27827c478bd9Sstevel@tonic-gate * All unused are padded with zeros starting at offset 446. 27837c478bd9Sstevel@tonic-gate */ 2784*bb16350dSlclee static void 2785*bb16350dSlclee copy_Bootblk_to_Table(void) 27867c478bd9Sstevel@tonic-gate { 27877c478bd9Sstevel@tonic-gate int i, j; 27887c478bd9Sstevel@tonic-gate char *bootptr; 27897c478bd9Sstevel@tonic-gate struct ipart iparts[FD_NUMPART]; 27907c478bd9Sstevel@tonic-gate 27917c478bd9Sstevel@tonic-gate /* Get an aligned copy of the partition tables */ 2792*bb16350dSlclee (void) memcpy(iparts, Bootblk->parts, sizeof (iparts)); 27937c478bd9Sstevel@tonic-gate bootptr = (char *)iparts; /* Points to start of partition table */ 27947c478bd9Sstevel@tonic-gate if (les(Bootblk->signature) != MBB_MAGIC) { 27957c478bd9Sstevel@tonic-gate /* Signature is missing */ 27967c478bd9Sstevel@tonic-gate nulltbl(); 2797*bb16350dSlclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 27987c478bd9Sstevel@tonic-gate return; 27997c478bd9Sstevel@tonic-gate } 28007c478bd9Sstevel@tonic-gate /* 28017c478bd9Sstevel@tonic-gate * When the DOS fdisk command deletes a partition, it is not 28027c478bd9Sstevel@tonic-gate * recognized by the old algorithm. The algorithm that 28037c478bd9Sstevel@tonic-gate * follows looks at each entry in the Bootrec and copies all 28047c478bd9Sstevel@tonic-gate * those that are valid. 28057c478bd9Sstevel@tonic-gate */ 28067c478bd9Sstevel@tonic-gate j = 0; 28077c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 28087c478bd9Sstevel@tonic-gate if (iparts[i].systid == 0) { 28097c478bd9Sstevel@tonic-gate /* Null entry */ 28107c478bd9Sstevel@tonic-gate bootptr += sizeof (struct ipart); 28117c478bd9Sstevel@tonic-gate } else { 2812*bb16350dSlclee fill_ipart(bootptr, &Table[j]); 28137c478bd9Sstevel@tonic-gate j++; 28147c478bd9Sstevel@tonic-gate bootptr += sizeof (struct ipart); 28157c478bd9Sstevel@tonic-gate } 28167c478bd9Sstevel@tonic-gate } 28177c478bd9Sstevel@tonic-gate for (i = j; i < FD_NUMPART; i++) { 28187c478bd9Sstevel@tonic-gate Table[i].systid = UNUSED; 28197c478bd9Sstevel@tonic-gate Table[i].numsect = lel(UNUSED); 28207c478bd9Sstevel@tonic-gate Table[i].relsect = lel(UNUSED); 28217c478bd9Sstevel@tonic-gate Table[i].bootid = 0; 28227c478bd9Sstevel@tonic-gate 28237c478bd9Sstevel@tonic-gate } 28247c478bd9Sstevel@tonic-gate /* For now, always replace the bootcode with ours */ 2825*bb16350dSlclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 28267c478bd9Sstevel@tonic-gate copy_Table_to_Bootblk(); 28277c478bd9Sstevel@tonic-gate } 28287c478bd9Sstevel@tonic-gate 28297c478bd9Sstevel@tonic-gate /* 28307c478bd9Sstevel@tonic-gate * fill_ipart 28317c478bd9Sstevel@tonic-gate * Initialize ipart structure values. 28327c478bd9Sstevel@tonic-gate */ 2833*bb16350dSlclee static void 28347c478bd9Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp) 28357c478bd9Sstevel@tonic-gate { 28367c478bd9Sstevel@tonic-gate #ifdef sparc 28377c478bd9Sstevel@tonic-gate /* Packing struct ipart for Sparc */ 28387c478bd9Sstevel@tonic-gate partp->bootid = getbyte(&bootptr); 28397c478bd9Sstevel@tonic-gate partp->beghead = getbyte(&bootptr); 28407c478bd9Sstevel@tonic-gate partp->begsect = getbyte(&bootptr); 28417c478bd9Sstevel@tonic-gate partp->begcyl = getbyte(&bootptr); 28427c478bd9Sstevel@tonic-gate partp->systid = getbyte(&bootptr); 28437c478bd9Sstevel@tonic-gate partp->endhead = getbyte(&bootptr); 28447c478bd9Sstevel@tonic-gate partp->endsect = getbyte(&bootptr); 28457c478bd9Sstevel@tonic-gate partp->endcyl = getbyte(&bootptr); 2846*bb16350dSlclee partp->relsect = (int32_t)getlong(&bootptr); 2847*bb16350dSlclee partp->numsect = (int32_t)getlong(&bootptr); 28487c478bd9Sstevel@tonic-gate #else 28497c478bd9Sstevel@tonic-gate *partp = *(struct ipart *)bootptr; 28507c478bd9Sstevel@tonic-gate #endif 28517c478bd9Sstevel@tonic-gate } 28527c478bd9Sstevel@tonic-gate 28537c478bd9Sstevel@tonic-gate /* 2854*bb16350dSlclee * getbyte, getlong 28557c478bd9Sstevel@tonic-gate * Get a byte, a short, or a long (SPARC only). 28567c478bd9Sstevel@tonic-gate */ 28577c478bd9Sstevel@tonic-gate #ifdef sparc 2858*bb16350dSlclee uchar_t 2859*bb16350dSlclee getbyte(char **bp) 28607c478bd9Sstevel@tonic-gate { 2861*bb16350dSlclee uchar_t b; 28627c478bd9Sstevel@tonic-gate 2863*bb16350dSlclee b = (uchar_t)**bp; 28647c478bd9Sstevel@tonic-gate *bp = *bp + 1; 28657c478bd9Sstevel@tonic-gate return (b); 28667c478bd9Sstevel@tonic-gate } 28677c478bd9Sstevel@tonic-gate 2868*bb16350dSlclee uint32_t 2869*bb16350dSlclee getlong(char **bp) 28707c478bd9Sstevel@tonic-gate { 2871*bb16350dSlclee int32_t b, bh, bl; 28727c478bd9Sstevel@tonic-gate 28737c478bd9Sstevel@tonic-gate bh = ((**bp) << 8) | *(*bp + 1); 28747c478bd9Sstevel@tonic-gate *bp += 2; 28757c478bd9Sstevel@tonic-gate bl = ((**bp) << 8) | *(*bp + 1); 28767c478bd9Sstevel@tonic-gate *bp += 2; 28777c478bd9Sstevel@tonic-gate 28787c478bd9Sstevel@tonic-gate b = (bh << 16) | bl; 2879*bb16350dSlclee return ((uint32_t)b); 28807c478bd9Sstevel@tonic-gate } 28817c478bd9Sstevel@tonic-gate #endif 28827c478bd9Sstevel@tonic-gate 28837c478bd9Sstevel@tonic-gate /* 28847c478bd9Sstevel@tonic-gate * copy_Table_to_Bootblk 28857c478bd9Sstevel@tonic-gate * Copy the table into the 512 boot record. Note that the unused 28867c478bd9Sstevel@tonic-gate * entries will always be the last ones in the table and they are 28877c478bd9Sstevel@tonic-gate * marked with 100 in sysind. The the unused portion of the table 28887c478bd9Sstevel@tonic-gate * is padded with zeros in the bytes after the used entries. 28897c478bd9Sstevel@tonic-gate */ 2890*bb16350dSlclee static void 2891*bb16350dSlclee copy_Table_to_Bootblk(void) 28927c478bd9Sstevel@tonic-gate { 28937c478bd9Sstevel@tonic-gate struct ipart *boot_ptr, *tbl_ptr; 28947c478bd9Sstevel@tonic-gate 28957c478bd9Sstevel@tonic-gate boot_ptr = (struct ipart *)Bootblk->parts; 28967c478bd9Sstevel@tonic-gate tbl_ptr = (struct ipart *)&Table[0].bootid; 28977c478bd9Sstevel@tonic-gate for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid; 28987c478bd9Sstevel@tonic-gate tbl_ptr++, boot_ptr++) { 28997c478bd9Sstevel@tonic-gate if (tbl_ptr->systid == UNUSED) 2900*bb16350dSlclee (void) memset(boot_ptr, 0, sizeof (struct ipart)); 29017c478bd9Sstevel@tonic-gate else 2902*bb16350dSlclee (void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart)); 29037c478bd9Sstevel@tonic-gate } 29047c478bd9Sstevel@tonic-gate Bootblk->signature = les(MBB_MAGIC); 29057c478bd9Sstevel@tonic-gate } 29067c478bd9Sstevel@tonic-gate 29077c478bd9Sstevel@tonic-gate /* 29087c478bd9Sstevel@tonic-gate * TableChanged 29097c478bd9Sstevel@tonic-gate * Check for any changes in the partition table. 29107c478bd9Sstevel@tonic-gate */ 2911*bb16350dSlclee static int 2912*bb16350dSlclee TableChanged(void) 29137c478bd9Sstevel@tonic-gate { 29147c478bd9Sstevel@tonic-gate int i, changed; 29157c478bd9Sstevel@tonic-gate 29167c478bd9Sstevel@tonic-gate changed = 0; 29177c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 29187c478bd9Sstevel@tonic-gate if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) { 29197c478bd9Sstevel@tonic-gate /* Partition table changed, write back to disk */ 29207c478bd9Sstevel@tonic-gate changed = 1; 29217c478bd9Sstevel@tonic-gate } 29227c478bd9Sstevel@tonic-gate } 29237c478bd9Sstevel@tonic-gate 29247c478bd9Sstevel@tonic-gate return (changed); 29257c478bd9Sstevel@tonic-gate } 29267c478bd9Sstevel@tonic-gate 29277c478bd9Sstevel@tonic-gate /* 29287c478bd9Sstevel@tonic-gate * ffile_write 29297c478bd9Sstevel@tonic-gate * Display contents of partition table to standard output or 29307c478bd9Sstevel@tonic-gate * another file name without writing it to the disk (-W file). 29317c478bd9Sstevel@tonic-gate */ 2932*bb16350dSlclee static void 2933*bb16350dSlclee ffile_write(char *file) 29347c478bd9Sstevel@tonic-gate { 29357c478bd9Sstevel@tonic-gate register int i; 29367c478bd9Sstevel@tonic-gate FILE *fp; 29377c478bd9Sstevel@tonic-gate 29387c478bd9Sstevel@tonic-gate /* 29397c478bd9Sstevel@tonic-gate * If file isn't standard output, then it's a file name. 29407c478bd9Sstevel@tonic-gate * Open file and write it. 29417c478bd9Sstevel@tonic-gate */ 29427c478bd9Sstevel@tonic-gate if (file != (char *)stdout) { 29437c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "w")) == NULL) { 29447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot open output file %s.\n", 29457c478bd9Sstevel@tonic-gate file); 29467c478bd9Sstevel@tonic-gate exit(1); 29477c478bd9Sstevel@tonic-gate } 29487c478bd9Sstevel@tonic-gate } 29497c478bd9Sstevel@tonic-gate else 29507c478bd9Sstevel@tonic-gate fp = stdout; 29517c478bd9Sstevel@tonic-gate 29527c478bd9Sstevel@tonic-gate /* 29537c478bd9Sstevel@tonic-gate * Write the fdisk table information 29547c478bd9Sstevel@tonic-gate */ 2955*bb16350dSlclee (void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev); 2956*bb16350dSlclee (void) fprintf(fp, "* Dimensions:\n"); 2957*bb16350dSlclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 2958*bb16350dSlclee (void) fprintf(fp, "* %4d sectors/track\n", sectors); 2959*bb16350dSlclee (void) fprintf(fp, "* %4d tracks/cylinder\n", heads); 2960*bb16350dSlclee (void) fprintf(fp, "* %4d cylinders\n", Numcyl); 2961*bb16350dSlclee (void) fprintf(fp, "*\n"); 29627c478bd9Sstevel@tonic-gate /* Write virtual (HBA) geometry, if required */ 29637c478bd9Sstevel@tonic-gate if (v_flag) { 2964*bb16350dSlclee (void) fprintf(fp, "* HBA Dimensions:\n"); 2965*bb16350dSlclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 2966*bb16350dSlclee (void) fprintf(fp, "* %4d sectors/track\n", hba_sectors); 2967*bb16350dSlclee (void) fprintf(fp, "* %4d tracks/cylinder\n", hba_heads); 2968*bb16350dSlclee (void) fprintf(fp, "* %4d cylinders\n", hba_Numcyl); 2969*bb16350dSlclee (void) fprintf(fp, "*\n"); 29707c478bd9Sstevel@tonic-gate } 2971*bb16350dSlclee (void) fprintf(fp, "* systid:\n"); 2972*bb16350dSlclee (void) fprintf(fp, "* 1: DOSOS12\n"); 2973*bb16350dSlclee (void) fprintf(fp, "* 2: PCIXOS\n"); 2974*bb16350dSlclee (void) fprintf(fp, "* 4: DOSOS16\n"); 2975*bb16350dSlclee (void) fprintf(fp, "* 5: EXTDOS\n"); 2976*bb16350dSlclee (void) fprintf(fp, "* 6: DOSBIG\n"); 2977*bb16350dSlclee (void) fprintf(fp, "* 7: FDISK_IFS\n"); 2978*bb16350dSlclee (void) fprintf(fp, "* 8: FDISK_AIXBOOT\n"); 2979*bb16350dSlclee (void) fprintf(fp, "* 9: FDISK_AIXDATA\n"); 2980*bb16350dSlclee (void) fprintf(fp, "* 10: FDISK_0S2BOOT\n"); 2981*bb16350dSlclee (void) fprintf(fp, "* 11: FDISK_WINDOWS\n"); 2982*bb16350dSlclee (void) fprintf(fp, "* 12: FDISK_EXT_WIN\n"); 2983*bb16350dSlclee (void) fprintf(fp, "* 14: FDISK_FAT95\n"); 2984*bb16350dSlclee (void) fprintf(fp, "* 15: FDISK_EXTLBA\n"); 2985*bb16350dSlclee (void) fprintf(fp, "* 18: DIAGPART\n"); 2986*bb16350dSlclee (void) fprintf(fp, "* 65: FDISK_LINUX\n"); 2987*bb16350dSlclee (void) fprintf(fp, "* 82: FDISK_CPM\n"); 2988*bb16350dSlclee (void) fprintf(fp, "* 86: DOSDATA\n"); 2989*bb16350dSlclee (void) fprintf(fp, "* 98: OTHEROS\n"); 2990*bb16350dSlclee (void) fprintf(fp, "* 99: UNIXOS\n"); 2991*bb16350dSlclee (void) fprintf(fp, "* 101: FDISK_NOVELL3\n"); 2992*bb16350dSlclee (void) fprintf(fp, "* 119: FDISK_QNX4\n"); 2993*bb16350dSlclee (void) fprintf(fp, "* 120: FDISK_QNX42\n"); 2994*bb16350dSlclee (void) fprintf(fp, "* 121: FDISK_QNX43\n"); 2995*bb16350dSlclee (void) fprintf(fp, "* 130: SUNIXOS\n"); 2996*bb16350dSlclee (void) fprintf(fp, "* 131: FDISK_LINUXNAT\n"); 2997*bb16350dSlclee (void) fprintf(fp, "* 134: FDISK_NTFSVOL1\n"); 2998*bb16350dSlclee (void) fprintf(fp, "* 135: FDISK_NTFSVOL2\n"); 2999*bb16350dSlclee (void) fprintf(fp, "* 165: FDISK_BSD\n"); 3000*bb16350dSlclee (void) fprintf(fp, "* 167: FDISK_NEXTSTEP\n"); 3001*bb16350dSlclee (void) fprintf(fp, "* 183: FDISK_BSDIFS\n"); 3002*bb16350dSlclee (void) fprintf(fp, "* 184: FDISK_BSDISWAP\n"); 3003*bb16350dSlclee (void) fprintf(fp, "* 190: X86BOOT\n"); 3004*bb16350dSlclee (void) fprintf(fp, "* 191: SUNIXOS2\n"); 3005*bb16350dSlclee (void) fprintf(fp, "* 238: EFI_PMBR\n"); 3006*bb16350dSlclee (void) fprintf(fp, "* 239: EFI_FS\n"); 3007*bb16350dSlclee (void) fprintf(fp, "*\n"); 3008*bb16350dSlclee (void) fprintf(fp, 30097c478bd9Sstevel@tonic-gate "\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl" 30107c478bd9Sstevel@tonic-gate " Rsect Numsect\n"); 30117c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 30127c478bd9Sstevel@tonic-gate if (Table[i].systid != UNUSED) 3013*bb16350dSlclee (void) fprintf(fp, 30147c478bd9Sstevel@tonic-gate " %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-8d" 30157c478bd9Sstevel@tonic-gate " %-8d\n", 30167c478bd9Sstevel@tonic-gate Table[i].systid, 30177c478bd9Sstevel@tonic-gate Table[i].bootid, 30187c478bd9Sstevel@tonic-gate Table[i].beghead, 30197c478bd9Sstevel@tonic-gate Table[i].begsect & 0x3f, 30207c478bd9Sstevel@tonic-gate ((Table[i].begcyl & 0xff) | ((Table[i].begsect & 30217c478bd9Sstevel@tonic-gate 0xc0) << 2)), 30227c478bd9Sstevel@tonic-gate Table[i].endhead, 30237c478bd9Sstevel@tonic-gate Table[i].endsect & 0x3f, 30247c478bd9Sstevel@tonic-gate ((Table[i].endcyl & 0xff) | ((Table[i].endsect & 30257c478bd9Sstevel@tonic-gate 0xc0) << 2)), 30267c478bd9Sstevel@tonic-gate lel(Table[i].relsect), 30277c478bd9Sstevel@tonic-gate lel(Table[i].numsect)); 30287c478bd9Sstevel@tonic-gate } 30297c478bd9Sstevel@tonic-gate if (fp != stdout) 3030*bb16350dSlclee (void) fclose(fp); 30317c478bd9Sstevel@tonic-gate } 30327c478bd9Sstevel@tonic-gate 30337c478bd9Sstevel@tonic-gate /* 30347c478bd9Sstevel@tonic-gate * fix_slice 30357c478bd9Sstevel@tonic-gate * Read the VTOC table on the Solaris partition and check that no 30367c478bd9Sstevel@tonic-gate * slices exist that extend past the end of the Solaris partition. 30377c478bd9Sstevel@tonic-gate * If no Solaris partition exists, nothing is done. 30387c478bd9Sstevel@tonic-gate */ 3039*bb16350dSlclee static void 3040*bb16350dSlclee fix_slice(void) 30417c478bd9Sstevel@tonic-gate { 30427c478bd9Sstevel@tonic-gate int i; 30437c478bd9Sstevel@tonic-gate int numsect; 30447c478bd9Sstevel@tonic-gate 30457c478bd9Sstevel@tonic-gate if (io_image) { 3046*bb16350dSlclee return; 30477c478bd9Sstevel@tonic-gate } 30487c478bd9Sstevel@tonic-gate 30497c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 30507c478bd9Sstevel@tonic-gate if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) { 30517c478bd9Sstevel@tonic-gate /* 30527c478bd9Sstevel@tonic-gate * Only the size matters (not starting point), since 30537c478bd9Sstevel@tonic-gate * VTOC entries are relative to the start of 30547c478bd9Sstevel@tonic-gate * the partition. 30557c478bd9Sstevel@tonic-gate */ 30567c478bd9Sstevel@tonic-gate numsect = lel(Table[i].numsect); 30577c478bd9Sstevel@tonic-gate break; 30587c478bd9Sstevel@tonic-gate } 30597c478bd9Sstevel@tonic-gate } 30607c478bd9Sstevel@tonic-gate 30617c478bd9Sstevel@tonic-gate if (i >= FD_NUMPART) { 30627c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 30637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 30647c478bd9Sstevel@tonic-gate "fdisk: No Solaris partition found - VTOC not" 30657c478bd9Sstevel@tonic-gate " checked.\n"); 30667c478bd9Sstevel@tonic-gate } 3067*bb16350dSlclee return; 30687c478bd9Sstevel@tonic-gate } 30697c478bd9Sstevel@tonic-gate 3070*bb16350dSlclee if (readvtoc() != VTOC_OK) { 30717c478bd9Sstevel@tonic-gate exit(1); /* Failed to read the VTOC */ 30727c478bd9Sstevel@tonic-gate } else { 30737c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 30747c478bd9Sstevel@tonic-gate /* Special case for slice two (entire disk) */ 30757c478bd9Sstevel@tonic-gate if (i == 2) { 30767c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_start != 0) { 30777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3078*bb16350dSlclee "slice %d starts at %ld, is not at" 30797c478bd9Sstevel@tonic-gate " start of partition", 30807c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_start); 30817c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3082*bb16350dSlclee (void) printf(" adjust ?:"); 30837c478bd9Sstevel@tonic-gate if (yesno()) 30847c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 30857c478bd9Sstevel@tonic-gate } else { 30867c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 30877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 30887c478bd9Sstevel@tonic-gate " adjusted!\n"); 30897c478bd9Sstevel@tonic-gate } 30907c478bd9Sstevel@tonic-gate 30917c478bd9Sstevel@tonic-gate } 30927c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_size != numsect) { 30937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3094*bb16350dSlclee "slice %d size %ld does not cover" 30957c478bd9Sstevel@tonic-gate " complete partition", 30967c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_size); 30977c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3098*bb16350dSlclee (void) printf(" adjust ?:"); 30997c478bd9Sstevel@tonic-gate if (yesno()) 31007c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 31017c478bd9Sstevel@tonic-gate numsect; 31027c478bd9Sstevel@tonic-gate } else { 31037c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 31047c478bd9Sstevel@tonic-gate numsect; 31057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 31067c478bd9Sstevel@tonic-gate " adjusted!\n"); 31077c478bd9Sstevel@tonic-gate } 31087c478bd9Sstevel@tonic-gate } 31097c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_tag != V_BACKUP) { 31107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 31117c478bd9Sstevel@tonic-gate "slice %d tag was %d should be %d", 31127c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_tag, 31137c478bd9Sstevel@tonic-gate V_BACKUP); 31147c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3115*bb16350dSlclee (void) printf(" fix ?:"); 31167c478bd9Sstevel@tonic-gate if (yesno()) 31177c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 31187c478bd9Sstevel@tonic-gate V_BACKUP; 31197c478bd9Sstevel@tonic-gate } else { 31207c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = V_BACKUP; 31217c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " fixed!\n"); 31227c478bd9Sstevel@tonic-gate } 31237c478bd9Sstevel@tonic-gate } 31247c478bd9Sstevel@tonic-gate } else { 31257c478bd9Sstevel@tonic-gate if (io_ADJT) { 31267c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_start > numsect || 31277c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start + 31287c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size > numsect) { 31297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3130*bb16350dSlclee "slice %d (start %ld, end %ld)" 3131*bb16350dSlclee " is larger than the partition", 31327c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_start, 31337c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start + 31347c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size); 31357c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3136*bb16350dSlclee (void) printf(" remove ?:"); 31377c478bd9Sstevel@tonic-gate if (yesno()) { 31387c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 31397c478bd9Sstevel@tonic-gate 0; 31407c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start 31417c478bd9Sstevel@tonic-gate = 0; 31427c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 31437c478bd9Sstevel@tonic-gate 0; 31447c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 31457c478bd9Sstevel@tonic-gate 0; 31467c478bd9Sstevel@tonic-gate } 31477c478bd9Sstevel@tonic-gate } else { 31487c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 31497c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 31507c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 31517c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 31527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 31537c478bd9Sstevel@tonic-gate " removed!\n"); 31547c478bd9Sstevel@tonic-gate } 31557c478bd9Sstevel@tonic-gate } 31567c478bd9Sstevel@tonic-gate } else { 31577c478bd9Sstevel@tonic-gate if (disk_vtoc.v_part[i].p_start > 31587c478bd9Sstevel@tonic-gate numsect) { 31597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3160*bb16350dSlclee "slice %d (start %ld) is larger" 31617c478bd9Sstevel@tonic-gate " than the partition", 31627c478bd9Sstevel@tonic-gate i, disk_vtoc.v_part[i].p_start); 31637c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3164*bb16350dSlclee (void) printf(" remove ?:"); 31657c478bd9Sstevel@tonic-gate if (yesno()) { 31667c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 31677c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 31687c478bd9Sstevel@tonic-gate 0; 31697c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 31707c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 31717c478bd9Sstevel@tonic-gate } 31727c478bd9Sstevel@tonic-gate } else { 31737c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 31747c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 31757c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 31767c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 31777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 31787c478bd9Sstevel@tonic-gate " removed!\n"); 31797c478bd9Sstevel@tonic-gate } 31807c478bd9Sstevel@tonic-gate } else if (disk_vtoc.v_part[i].p_start 31817c478bd9Sstevel@tonic-gate + disk_vtoc.v_part[i].p_size > 31827c478bd9Sstevel@tonic-gate numsect) { 31837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3184*bb16350dSlclee "slice %d (end %ld) is larger" 31857c478bd9Sstevel@tonic-gate " than the partition", 31867c478bd9Sstevel@tonic-gate i, 31877c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_start + 31887c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size); 31897c478bd9Sstevel@tonic-gate if (!io_nifdisk) { 3190*bb16350dSlclee (void) printf(" adjust ?:"); 31917c478bd9Sstevel@tonic-gate if (yesno()) { 31927c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size 31937c478bd9Sstevel@tonic-gate = numsect; 31947c478bd9Sstevel@tonic-gate } 31957c478bd9Sstevel@tonic-gate } else { 31967c478bd9Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 31977c478bd9Sstevel@tonic-gate numsect; 31987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 31997c478bd9Sstevel@tonic-gate " adjusted!\n"); 32007c478bd9Sstevel@tonic-gate } 32017c478bd9Sstevel@tonic-gate } 32027c478bd9Sstevel@tonic-gate } 32037c478bd9Sstevel@tonic-gate } 32047c478bd9Sstevel@tonic-gate } 32057c478bd9Sstevel@tonic-gate } 32067c478bd9Sstevel@tonic-gate #if 1 /* bh for now */ 32077c478bd9Sstevel@tonic-gate /* Make the VTOC look sane - ha ha */ 32087c478bd9Sstevel@tonic-gate disk_vtoc.v_version = V_VERSION; 32097c478bd9Sstevel@tonic-gate disk_vtoc.v_sanity = VTOC_SANE; 32107c478bd9Sstevel@tonic-gate disk_vtoc.v_nparts = V_NUMPAR; 32117c478bd9Sstevel@tonic-gate if (disk_vtoc.v_sectorsz == 0) 32127c478bd9Sstevel@tonic-gate disk_vtoc.v_sectorsz = NBPSCTR; 32137c478bd9Sstevel@tonic-gate #endif 32147c478bd9Sstevel@tonic-gate 32157c478bd9Sstevel@tonic-gate /* Write the VTOC back to the disk */ 32167c478bd9Sstevel@tonic-gate if (!io_readonly) 3217*bb16350dSlclee (void) writevtoc(); 32187c478bd9Sstevel@tonic-gate } 32197c478bd9Sstevel@tonic-gate 32207c478bd9Sstevel@tonic-gate /* 32217c478bd9Sstevel@tonic-gate * yesno 32227c478bd9Sstevel@tonic-gate * Get yes or no answer. Return 1 for yes and 0 for no. 32237c478bd9Sstevel@tonic-gate */ 32247c478bd9Sstevel@tonic-gate 3225*bb16350dSlclee static int 3226*bb16350dSlclee yesno(void) 32277c478bd9Sstevel@tonic-gate { 32287c478bd9Sstevel@tonic-gate char s[80]; 32297c478bd9Sstevel@tonic-gate 32307c478bd9Sstevel@tonic-gate for (;;) { 3231*bb16350dSlclee (void) gets(s); 32327c478bd9Sstevel@tonic-gate rm_blanks(s); 32337c478bd9Sstevel@tonic-gate if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) { 3234*bb16350dSlclee (void) printf(E_LINE); 3235*bb16350dSlclee (void) printf("Please answer with \"y\" or \"n\": "); 32367c478bd9Sstevel@tonic-gate continue; 32377c478bd9Sstevel@tonic-gate } 32387c478bd9Sstevel@tonic-gate if (s[0] == 'y') 32397c478bd9Sstevel@tonic-gate return (1); 32407c478bd9Sstevel@tonic-gate else 32417c478bd9Sstevel@tonic-gate return (0); 32427c478bd9Sstevel@tonic-gate } 32437c478bd9Sstevel@tonic-gate } 32447c478bd9Sstevel@tonic-gate 32457c478bd9Sstevel@tonic-gate /* 32467c478bd9Sstevel@tonic-gate * readvtoc 32477c478bd9Sstevel@tonic-gate * Read the VTOC from the Solaris partition of the device. 32487c478bd9Sstevel@tonic-gate */ 3249*bb16350dSlclee static int 3250*bb16350dSlclee readvtoc(void) 32517c478bd9Sstevel@tonic-gate { 32527c478bd9Sstevel@tonic-gate int i; 32537c478bd9Sstevel@tonic-gate int retval = VTOC_OK; 32547c478bd9Sstevel@tonic-gate 32557c478bd9Sstevel@tonic-gate if ((i = read_vtoc(Dev, &disk_vtoc)) < VTOC_OK) { 32567c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 32577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Invalid VTOC.\n"); 32587c478bd9Sstevel@tonic-gate vt_inval++; 32597c478bd9Sstevel@tonic-gate retval = VTOC_INVAL; 32607c478bd9Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 32617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 32627c478bd9Sstevel@tonic-gate "GPT\n"); 32637c478bd9Sstevel@tonic-gate retval = VTOC_NOTSUP; 32647c478bd9Sstevel@tonic-gate } else { 32657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot read VTOC.\n"); 32667c478bd9Sstevel@tonic-gate retval = VTOC_RWERR; 32677c478bd9Sstevel@tonic-gate } 32687c478bd9Sstevel@tonic-gate } 32697c478bd9Sstevel@tonic-gate return (retval); 32707c478bd9Sstevel@tonic-gate } 32717c478bd9Sstevel@tonic-gate 32727c478bd9Sstevel@tonic-gate /* 32737c478bd9Sstevel@tonic-gate * writevtoc 32747c478bd9Sstevel@tonic-gate * Write the VTOC to the Solaris partition on the device. 32757c478bd9Sstevel@tonic-gate */ 3276*bb16350dSlclee static int 3277*bb16350dSlclee writevtoc(void) 32787c478bd9Sstevel@tonic-gate { 32797c478bd9Sstevel@tonic-gate int i; 32807c478bd9Sstevel@tonic-gate int retval = 0; 32817c478bd9Sstevel@tonic-gate 32827c478bd9Sstevel@tonic-gate if ((i = write_vtoc(Dev, &disk_vtoc)) != 0) { 32837c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) { 32847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 32857c478bd9Sstevel@tonic-gate "fdisk: Invalid entry exists in VTOC.\n"); 32867c478bd9Sstevel@tonic-gate retval = VTOC_INVAL; 32877c478bd9Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 32887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 32897c478bd9Sstevel@tonic-gate "GPT\n"); 32907c478bd9Sstevel@tonic-gate retval = VTOC_NOTSUP; 32917c478bd9Sstevel@tonic-gate } else { 32927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot write VTOC.\n"); 32937c478bd9Sstevel@tonic-gate retval = VTOC_RWERR; 32947c478bd9Sstevel@tonic-gate } 32957c478bd9Sstevel@tonic-gate } 32967c478bd9Sstevel@tonic-gate return (retval); 32977c478bd9Sstevel@tonic-gate } 32987c478bd9Sstevel@tonic-gate 32997c478bd9Sstevel@tonic-gate /* 33007c478bd9Sstevel@tonic-gate * efi_ioctl 33017c478bd9Sstevel@tonic-gate * issues DKIOCSETEFI IOCTL 33027c478bd9Sstevel@tonic-gate * (duplicate of private efi_ioctl() in rdwr_efi.c 33037c478bd9Sstevel@tonic-gate */ 33047c478bd9Sstevel@tonic-gate static int 33057c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 33067c478bd9Sstevel@tonic-gate { 33077c478bd9Sstevel@tonic-gate void *data = dk_ioc->dki_data; 33087c478bd9Sstevel@tonic-gate int error; 33097c478bd9Sstevel@tonic-gate 33107c478bd9Sstevel@tonic-gate dk_ioc->dki_data_64 = (uintptr_t)data; 33117c478bd9Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc); 33127c478bd9Sstevel@tonic-gate 33137c478bd9Sstevel@tonic-gate return (error); 33147c478bd9Sstevel@tonic-gate } 33157c478bd9Sstevel@tonic-gate 33167c478bd9Sstevel@tonic-gate /* 33177c478bd9Sstevel@tonic-gate * clear_efi 33187c478bd9Sstevel@tonic-gate * Clear EFI labels from the EFI_PMBR partition on the device 33197c478bd9Sstevel@tonic-gate * This function is modeled on the libefi(3LIB) call efi_write() 33207c478bd9Sstevel@tonic-gate */ 3321*bb16350dSlclee static int 3322*bb16350dSlclee clear_efi(void) 33237c478bd9Sstevel@tonic-gate { 33247c478bd9Sstevel@tonic-gate struct dk_gpt *efi_vtoc; 33257c478bd9Sstevel@tonic-gate dk_efi_t dk_ioc; 33267c478bd9Sstevel@tonic-gate 33277c478bd9Sstevel@tonic-gate /* 33287c478bd9Sstevel@tonic-gate * see if we can read the EFI label 33297c478bd9Sstevel@tonic-gate */ 33307c478bd9Sstevel@tonic-gate if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) { 33317c478bd9Sstevel@tonic-gate return (VT_ERROR); 33327c478bd9Sstevel@tonic-gate } 33337c478bd9Sstevel@tonic-gate 33347c478bd9Sstevel@tonic-gate /* 33357c478bd9Sstevel@tonic-gate * set up the dk_ioc structure for writing 33367c478bd9Sstevel@tonic-gate */ 33377c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = 1; 33387c478bd9Sstevel@tonic-gate dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize; 33397c478bd9Sstevel@tonic-gate 33407c478bd9Sstevel@tonic-gate if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) { 33417c478bd9Sstevel@tonic-gate return (VT_ERROR); 33427c478bd9Sstevel@tonic-gate } 33437c478bd9Sstevel@tonic-gate 33447c478bd9Sstevel@tonic-gate /* 33457c478bd9Sstevel@tonic-gate * clear the primary label 33467c478bd9Sstevel@tonic-gate */ 33477c478bd9Sstevel@tonic-gate if (io_debug) { 3348*bb16350dSlclee (void) fprintf(stderr, 3349*bb16350dSlclee "\tClearing primary EFI label at block %lld\n", 3350*bb16350dSlclee dk_ioc.dki_lba); 33517c478bd9Sstevel@tonic-gate } 33527c478bd9Sstevel@tonic-gate 33537c478bd9Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 33547c478bd9Sstevel@tonic-gate free(dk_ioc.dki_data); 33557c478bd9Sstevel@tonic-gate switch (errno) { 33567c478bd9Sstevel@tonic-gate case EIO: 33577c478bd9Sstevel@tonic-gate return (VT_EIO); 33587c478bd9Sstevel@tonic-gate case EINVAL: 33597c478bd9Sstevel@tonic-gate return (VT_EINVAL); 33607c478bd9Sstevel@tonic-gate default: 33617c478bd9Sstevel@tonic-gate return (VT_ERROR); 33627c478bd9Sstevel@tonic-gate } 33637c478bd9Sstevel@tonic-gate } 33647c478bd9Sstevel@tonic-gate 33657c478bd9Sstevel@tonic-gate /* 33667c478bd9Sstevel@tonic-gate * clear the backup partition table 33677c478bd9Sstevel@tonic-gate */ 33687c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1; 33697c478bd9Sstevel@tonic-gate dk_ioc.dki_length -= efi_vtoc->efi_lbasize; 33707c478bd9Sstevel@tonic-gate dk_ioc.dki_data++; 33717c478bd9Sstevel@tonic-gate if (io_debug) { 3372*bb16350dSlclee (void) fprintf(stderr, 3373*bb16350dSlclee "\tClearing backup partition table at block %lld\n", 3374*bb16350dSlclee dk_ioc.dki_lba); 33757c478bd9Sstevel@tonic-gate } 33767c478bd9Sstevel@tonic-gate 33777c478bd9Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 33787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\tUnable to clear backup EFI label at " 33797c478bd9Sstevel@tonic-gate "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1, 33807c478bd9Sstevel@tonic-gate errno); 33817c478bd9Sstevel@tonic-gate } 33827c478bd9Sstevel@tonic-gate 33837c478bd9Sstevel@tonic-gate /* 33847c478bd9Sstevel@tonic-gate * clear the backup label 33857c478bd9Sstevel@tonic-gate */ 33867c478bd9Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_lba; 33877c478bd9Sstevel@tonic-gate dk_ioc.dki_length = efi_vtoc->efi_lbasize; 33887c478bd9Sstevel@tonic-gate dk_ioc.dki_data--; 33897c478bd9Sstevel@tonic-gate if (io_debug) { 3390*bb16350dSlclee (void) fprintf(stderr, "\tClearing backup label at block " 3391*bb16350dSlclee "%lld\n", dk_ioc.dki_lba); 33927c478bd9Sstevel@tonic-gate } 33937c478bd9Sstevel@tonic-gate 33947c478bd9Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 3395*bb16350dSlclee (void) fprintf(stderr, 3396*bb16350dSlclee "\tUnable to clear backup EFI label at " 3397*bb16350dSlclee "block %llu; errno %d\n", 3398*bb16350dSlclee efi_vtoc->efi_last_lba, 33997c478bd9Sstevel@tonic-gate errno); 34007c478bd9Sstevel@tonic-gate } 34017c478bd9Sstevel@tonic-gate 34027c478bd9Sstevel@tonic-gate free(dk_ioc.dki_data); 34037c478bd9Sstevel@tonic-gate efi_free(efi_vtoc); 34047c478bd9Sstevel@tonic-gate 34057c478bd9Sstevel@tonic-gate return (0); 34067c478bd9Sstevel@tonic-gate } 34077c478bd9Sstevel@tonic-gate 34087c478bd9Sstevel@tonic-gate /* 34097c478bd9Sstevel@tonic-gate * clear_vtoc 34107c478bd9Sstevel@tonic-gate * Clear the VTOC from the current or previous Solaris partition on the 34117c478bd9Sstevel@tonic-gate * device. 34127c478bd9Sstevel@tonic-gate */ 3413*bb16350dSlclee static void 34147c478bd9Sstevel@tonic-gate clear_vtoc(int table, int part) 34157c478bd9Sstevel@tonic-gate { 34167c478bd9Sstevel@tonic-gate struct ipart *clr_table; 34177c478bd9Sstevel@tonic-gate struct dk_label disk_label; 34187c478bd9Sstevel@tonic-gate int pcyl, ncyl, backup_block, solaris_offset, count, bytes, seek_byte; 34197c478bd9Sstevel@tonic-gate 34207c478bd9Sstevel@tonic-gate #ifdef DEBUG 34217c478bd9Sstevel@tonic-gate struct dk_label read_label; 34227c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 34237c478bd9Sstevel@tonic-gate 34247c478bd9Sstevel@tonic-gate if (table == OLD) { 34257c478bd9Sstevel@tonic-gate clr_table = &Old_Table[part]; 34267c478bd9Sstevel@tonic-gate } else { 34277c478bd9Sstevel@tonic-gate clr_table = &Table[part]; 34287c478bd9Sstevel@tonic-gate } 34297c478bd9Sstevel@tonic-gate 3430*bb16350dSlclee (void) memset(&disk_label, 0, sizeof (struct dk_label)); 34317c478bd9Sstevel@tonic-gate 34327c478bd9Sstevel@tonic-gate seek_byte = (lel(clr_table->relsect) * sectsiz) + VTOC_OFFSET; 34337c478bd9Sstevel@tonic-gate 34347c478bd9Sstevel@tonic-gate if (io_debug) { 3435*bb16350dSlclee (void) fprintf(stderr, "\tClearing primary VTOC at byte %d\n", 34367c478bd9Sstevel@tonic-gate seek_byte); 34377c478bd9Sstevel@tonic-gate } 34387c478bd9Sstevel@tonic-gate 34397c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3440*bb16350dSlclee (void) fprintf(stderr, 3441*bb16350dSlclee "\tError seeking to primary label at byte %d\n", 34427c478bd9Sstevel@tonic-gate seek_byte); 3443*bb16350dSlclee return; 34447c478bd9Sstevel@tonic-gate } 34457c478bd9Sstevel@tonic-gate 34467c478bd9Sstevel@tonic-gate bytes = write(Dev, &disk_label, sizeof (struct dk_label)); 34477c478bd9Sstevel@tonic-gate 34487c478bd9Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3449*bb16350dSlclee (void) fprintf(stderr, 3450*bb16350dSlclee "\tWarning: only %d bytes written to clear primary VTOC!\n", 3451*bb16350dSlclee bytes); 34527c478bd9Sstevel@tonic-gate } 34537c478bd9Sstevel@tonic-gate 34547c478bd9Sstevel@tonic-gate #ifdef DEBUG 34557c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3456*bb16350dSlclee (void) fprintf(stderr, 3457*bb16350dSlclee "DEBUG: Error seeking to primary label at byte %d\n", 3458*bb16350dSlclee seek_byte); 3459*bb16350dSlclee return; 34607c478bd9Sstevel@tonic-gate } else { 3461*bb16350dSlclee (void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n", 34627c478bd9Sstevel@tonic-gate seek_byte); 34637c478bd9Sstevel@tonic-gate } 34647c478bd9Sstevel@tonic-gate 34657c478bd9Sstevel@tonic-gate bytes = read(Dev, &read_label, sizeof (struct dk_label)); 34667c478bd9Sstevel@tonic-gate 34677c478bd9Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3468*bb16350dSlclee (void) fprintf(stderr, 3469*bb16350dSlclee "DEBUG: Warning: only %d bytes read of label\n", 34707c478bd9Sstevel@tonic-gate bytes); 34717c478bd9Sstevel@tonic-gate } 34727c478bd9Sstevel@tonic-gate 34737c478bd9Sstevel@tonic-gate if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) { 3474*bb16350dSlclee (void) fprintf(stderr, 3475*bb16350dSlclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 34767c478bd9Sstevel@tonic-gate } else { 3477*bb16350dSlclee (void) fprintf(stderr, "DEBUG Good compare of disk_label and " 34787c478bd9Sstevel@tonic-gate "read_label\n"); 34797c478bd9Sstevel@tonic-gate } 34807c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 34817c478bd9Sstevel@tonic-gate 34827c478bd9Sstevel@tonic-gate /* Clear backup label */ 34837c478bd9Sstevel@tonic-gate pcyl = lel(clr_table->numsect) / (heads * sectors); 34847c478bd9Sstevel@tonic-gate solaris_offset = lel(clr_table->relsect); 34857c478bd9Sstevel@tonic-gate ncyl = pcyl - acyl; 34867c478bd9Sstevel@tonic-gate 34877c478bd9Sstevel@tonic-gate backup_block = ((ncyl + acyl - 1) * 34887c478bd9Sstevel@tonic-gate (heads * sectors)) + ((heads - 1) * sectors) + 1; 34897c478bd9Sstevel@tonic-gate 34907c478bd9Sstevel@tonic-gate for (count = 1; count < 6; count++) { 34917c478bd9Sstevel@tonic-gate seek_byte = (solaris_offset + backup_block) * 512; 34927c478bd9Sstevel@tonic-gate 34937c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3494*bb16350dSlclee (void) fprintf(stderr, 34957c478bd9Sstevel@tonic-gate "\tError seeking to backup label at byte %d on " 34967c478bd9Sstevel@tonic-gate "%s.\n", seek_byte, Dfltdev); 3497*bb16350dSlclee return; 34987c478bd9Sstevel@tonic-gate } 34997c478bd9Sstevel@tonic-gate 35007c478bd9Sstevel@tonic-gate if (io_debug) { 3501*bb16350dSlclee (void) fprintf(stderr, "\tClearing backup VTOC at" 35027c478bd9Sstevel@tonic-gate " byte %d (block %d)\n", 35037c478bd9Sstevel@tonic-gate (solaris_offset + backup_block) * 512, 35047c478bd9Sstevel@tonic-gate (solaris_offset + backup_block)); 35057c478bd9Sstevel@tonic-gate } 35067c478bd9Sstevel@tonic-gate 35077c478bd9Sstevel@tonic-gate bytes = write(Dev, &disk_label, sizeof (struct dk_label)); 35087c478bd9Sstevel@tonic-gate 35097c478bd9Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3510*bb16350dSlclee (void) fprintf(stderr, 3511*bb16350dSlclee "\t\tWarning: only %d bytes written to " 35127c478bd9Sstevel@tonic-gate "clear backup VTOC at block %d!\n", bytes, 35137c478bd9Sstevel@tonic-gate (solaris_offset + backup_block)); 35147c478bd9Sstevel@tonic-gate } 35157c478bd9Sstevel@tonic-gate 35167c478bd9Sstevel@tonic-gate #ifdef DEBUG 35177c478bd9Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3518*bb16350dSlclee (void) fprintf(stderr, 3519*bb16350dSlclee "DEBUG: Error seeking to backup label at byte %d\n", 3520*bb16350dSlclee seek_byte); 3521*bb16350dSlclee return; 35227c478bd9Sstevel@tonic-gate } else { 3523*bb16350dSlclee (void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n", 35247c478bd9Sstevel@tonic-gate seek_byte); 35257c478bd9Sstevel@tonic-gate } 35267c478bd9Sstevel@tonic-gate 35277c478bd9Sstevel@tonic-gate bytes = read(Dev, &read_label, sizeof (struct dk_label)); 35287c478bd9Sstevel@tonic-gate 35297c478bd9Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3530*bb16350dSlclee (void) fprintf(stderr, 3531*bb16350dSlclee "DEBUG: Warning: only %d bytes read of backup label\n", 3532*bb16350dSlclee bytes); 35337c478bd9Sstevel@tonic-gate } 35347c478bd9Sstevel@tonic-gate 35357c478bd9Sstevel@tonic-gate if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) { 3536*bb16350dSlclee (void) fprintf(stderr, 3537*bb16350dSlclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 35387c478bd9Sstevel@tonic-gate } else { 3539*bb16350dSlclee (void) fprintf(stderr, 3540*bb16350dSlclee "DEBUG: Good compare of disk_label and backup " 35417c478bd9Sstevel@tonic-gate "read_label\n"); 35427c478bd9Sstevel@tonic-gate } 35437c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 35447c478bd9Sstevel@tonic-gate 35457c478bd9Sstevel@tonic-gate backup_block += 2; 35467c478bd9Sstevel@tonic-gate } 35477c478bd9Sstevel@tonic-gate } 35487c478bd9Sstevel@tonic-gate 35497c478bd9Sstevel@tonic-gate #define FDISK_STANDARD_LECTURE \ 35507c478bd9Sstevel@tonic-gate "Fdisk is normally used with the device that " \ 35517c478bd9Sstevel@tonic-gate "represents the entire fixed disk.\n" \ 35527c478bd9Sstevel@tonic-gate "(For example, /dev/rdsk/c0d0p0 on x86 or " \ 35537c478bd9Sstevel@tonic-gate "/dev/rdsk/c0t5d0s2 on sparc).\n" 35547c478bd9Sstevel@tonic-gate 35557c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NOT_SECTOR_ZERO \ 35567c478bd9Sstevel@tonic-gate "The device does not appear to include absolute\n" \ 35577c478bd9Sstevel@tonic-gate "sector 0 of the PHYSICAL disk " \ 35587c478bd9Sstevel@tonic-gate "(the normal location for an fdisk table).\n" 35597c478bd9Sstevel@tonic-gate 35607c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NOT_FULL \ 35617c478bd9Sstevel@tonic-gate "The device does not appear to encompass the entire PHYSICAL disk.\n" 35627c478bd9Sstevel@tonic-gate 35637c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NO_VTOC \ 35647c478bd9Sstevel@tonic-gate "Unable to find a volume table of contents.\n" \ 35657c478bd9Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 35667c478bd9Sstevel@tonic-gate 35677c478bd9Sstevel@tonic-gate #define FDISK_LECTURE_NO_GEOM \ 35687c478bd9Sstevel@tonic-gate "Unable to get geometry from device.\n" \ 35697c478bd9Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 35707c478bd9Sstevel@tonic-gate 35717c478bd9Sstevel@tonic-gate #define FDISK_SHALL_I_CONTINUE \ 35727c478bd9Sstevel@tonic-gate "Are you sure you want to continue? (y/n) " 35737c478bd9Sstevel@tonic-gate 35747c478bd9Sstevel@tonic-gate /* 35757c478bd9Sstevel@tonic-gate * lecture_and_query 35767c478bd9Sstevel@tonic-gate * Called when a sanity check fails. This routine gives a warning 35777c478bd9Sstevel@tonic-gate * specific to the check that fails, followed by a generic lecture 35787c478bd9Sstevel@tonic-gate * about the "right" device to supply as input. Then, if appropriate, 35797c478bd9Sstevel@tonic-gate * it will prompt the user on whether or not they want to continue. 35807c478bd9Sstevel@tonic-gate * Inappropriate times for prompting are when the user has selected 35817c478bd9Sstevel@tonic-gate * non-interactive mode or read-only mode. 35827c478bd9Sstevel@tonic-gate */ 3583*bb16350dSlclee static int 35847c478bd9Sstevel@tonic-gate lecture_and_query(char *warning, char *devname) 35857c478bd9Sstevel@tonic-gate { 35867c478bd9Sstevel@tonic-gate if (io_nifdisk) 35877c478bd9Sstevel@tonic-gate return (0); 35887c478bd9Sstevel@tonic-gate 3589*bb16350dSlclee (void) fprintf(stderr, "WARNING: Device %s: \n", devname); 3590*bb16350dSlclee (void) fprintf(stderr, "%s", warning); 3591*bb16350dSlclee (void) fprintf(stderr, FDISK_STANDARD_LECTURE); 3592*bb16350dSlclee (void) fprintf(stderr, FDISK_SHALL_I_CONTINUE); 35937c478bd9Sstevel@tonic-gate 35947c478bd9Sstevel@tonic-gate return (yesno()); 35957c478bd9Sstevel@tonic-gate } 35967c478bd9Sstevel@tonic-gate 3597*bb16350dSlclee static void 35987c478bd9Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd) 35997c478bd9Sstevel@tonic-gate { 36007c478bd9Sstevel@tonic-gate struct vtoc v; 36017c478bd9Sstevel@tonic-gate struct dk_geom d; 36027c478bd9Sstevel@tonic-gate struct part_info pi; 36037c478bd9Sstevel@tonic-gate long totsize; 36047c478bd9Sstevel@tonic-gate int idx = -1; 36057c478bd9Sstevel@tonic-gate 36067c478bd9Sstevel@tonic-gate /* 36077c478bd9Sstevel@tonic-gate * First try the PARTINFO ioctl. If it works, we will be able 36087c478bd9Sstevel@tonic-gate * to tell if they've specified the full disk partition by checking 36097c478bd9Sstevel@tonic-gate * to see if they've specified a partition that starts at sector 0. 36107c478bd9Sstevel@tonic-gate */ 36117c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) { 36127c478bd9Sstevel@tonic-gate if (pi.p_start != 0) { 36137c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 36147c478bd9Sstevel@tonic-gate devname)) { 36157c478bd9Sstevel@tonic-gate (void) close(fd); 36167c478bd9Sstevel@tonic-gate exit(1); 36177c478bd9Sstevel@tonic-gate } 36187c478bd9Sstevel@tonic-gate } 36197c478bd9Sstevel@tonic-gate } else { 36207c478bd9Sstevel@tonic-gate if ((idx = read_vtoc(fd, &v)) < 0) { 36217c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_VTOC, 36227c478bd9Sstevel@tonic-gate devname)) { 36237c478bd9Sstevel@tonic-gate (void) close(fd); 36247c478bd9Sstevel@tonic-gate exit(1); 36257c478bd9Sstevel@tonic-gate } 36267c478bd9Sstevel@tonic-gate return; 36277c478bd9Sstevel@tonic-gate } 36287c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &d) == -1) { 36297c478bd9Sstevel@tonic-gate perror(devname); 36307c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_GEOM, 36317c478bd9Sstevel@tonic-gate devname)) { 36327c478bd9Sstevel@tonic-gate (void) close(fd); 36337c478bd9Sstevel@tonic-gate exit(1); 36347c478bd9Sstevel@tonic-gate } 36357c478bd9Sstevel@tonic-gate return; 36367c478bd9Sstevel@tonic-gate } 36377c478bd9Sstevel@tonic-gate totsize = d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect; 36387c478bd9Sstevel@tonic-gate if (v.v_part[idx].p_size != totsize) { 36397c478bd9Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_FULL, 36407c478bd9Sstevel@tonic-gate devname)) { 36417c478bd9Sstevel@tonic-gate (void) close(fd); 36427c478bd9Sstevel@tonic-gate exit(1); 36437c478bd9Sstevel@tonic-gate } 36447c478bd9Sstevel@tonic-gate } 36457c478bd9Sstevel@tonic-gate } 36467c478bd9Sstevel@tonic-gate } 36477c478bd9Sstevel@tonic-gate 36487c478bd9Sstevel@tonic-gate 36497c478bd9Sstevel@tonic-gate /* 36507c478bd9Sstevel@tonic-gate * get_node 36517c478bd9Sstevel@tonic-gate * Called from main to construct the name of the device node to open. 36527c478bd9Sstevel@tonic-gate * Initially tries to stat the node exactly as provided, if that fails 36537c478bd9Sstevel@tonic-gate * we prepend the default path (/dev/rdsk/). 36547c478bd9Sstevel@tonic-gate */ 36557c478bd9Sstevel@tonic-gate static char * 36567c478bd9Sstevel@tonic-gate get_node(char *devname) 36577c478bd9Sstevel@tonic-gate { 36587c478bd9Sstevel@tonic-gate char *node; 36597c478bd9Sstevel@tonic-gate struct stat statbuf; 36607c478bd9Sstevel@tonic-gate size_t space; 36617c478bd9Sstevel@tonic-gate 36627c478bd9Sstevel@tonic-gate /* Don't do anything if we are skipping device checks */ 36637c478bd9Sstevel@tonic-gate if (io_image) 36647c478bd9Sstevel@tonic-gate return (devname); 36657c478bd9Sstevel@tonic-gate 36667c478bd9Sstevel@tonic-gate node = devname; 36677c478bd9Sstevel@tonic-gate 36687c478bd9Sstevel@tonic-gate /* Try the node as provided first */ 36697c478bd9Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 36707c478bd9Sstevel@tonic-gate /* 36717c478bd9Sstevel@tonic-gate * Copy the passed in string to a new buffer, prepend the 36727c478bd9Sstevel@tonic-gate * default path and try again. 36737c478bd9Sstevel@tonic-gate */ 36747c478bd9Sstevel@tonic-gate space = strlen(DEFAULT_PATH) + strlen(devname) + 1; 36757c478bd9Sstevel@tonic-gate 36767c478bd9Sstevel@tonic-gate if ((node = malloc(space)) == NULL) { 3677*bb16350dSlclee (void) fprintf(stderr, "fdisk: Unable to obtain memory " 36787c478bd9Sstevel@tonic-gate "for device node.\n"); 36797c478bd9Sstevel@tonic-gate exit(1); 36807c478bd9Sstevel@tonic-gate } 36817c478bd9Sstevel@tonic-gate 36827c478bd9Sstevel@tonic-gate /* Copy over the default path and the provided node */ 36837c478bd9Sstevel@tonic-gate (void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH)); 36847c478bd9Sstevel@tonic-gate space -= strlen(DEFAULT_PATH); 36857c478bd9Sstevel@tonic-gate (void) strlcpy(node + strlen(DEFAULT_PATH), devname, space); 36867c478bd9Sstevel@tonic-gate 36877c478bd9Sstevel@tonic-gate /* Try to stat it again */ 36887c478bd9Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 36897c478bd9Sstevel@tonic-gate /* Failed all options, give up */ 3690*bb16350dSlclee (void) fprintf(stderr, 3691*bb16350dSlclee "fdisk: Cannot stat device %s.\n", 36927c478bd9Sstevel@tonic-gate devname); 36937c478bd9Sstevel@tonic-gate exit(1); 36947c478bd9Sstevel@tonic-gate } 36957c478bd9Sstevel@tonic-gate } 36967c478bd9Sstevel@tonic-gate 36977c478bd9Sstevel@tonic-gate /* Make sure the device specified is the raw device */ 36987c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 3699*bb16350dSlclee (void) fprintf(stderr, 3700*bb16350dSlclee "fdisk: %s must be a raw device.\n", node); 37017c478bd9Sstevel@tonic-gate exit(1); 37027c478bd9Sstevel@tonic-gate } 37037c478bd9Sstevel@tonic-gate 37047c478bd9Sstevel@tonic-gate return (node); 37057c478bd9Sstevel@tonic-gate } 3706