fdisk.c revision d463bb94f213817526ea7f7b2b8219ef1fe801d9
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0N/A * or http://www.opensolaris.org/os/licensing.
0N/A * See the License for the specific language governing permissions
0N/A * and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
873N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
4134N/A */
0N/A
0N/A/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
2366N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
2366N/A/* All Rights Reserved */
1689N/A
4134N/A/* Copyright (c) 1987, 1988 Microsoft Corporation */
4134N/A/* All Rights Reserved */
4134N/A
4134N/A/*
4134N/A * PROGRAM: fdisk(1M)
4134N/A * This program reads the partition table on the specified device and
4134N/A * also reads the drive parameters. The user can perform various
4134N/A * operations from a supplied menu or from the command line. Diagnostic
0N/A * options are also available.
0N/A */
2366N/A#include <stdio.h>
0N/A#include <stdlib.h>
4134N/A#include <string.h>
0N/A#include <unistd.h>
0N/A#include <errno.h>
0N/A#include <fcntl.h>
0N/A#include <ctype.h>
0N/A#include <sys/stat.h>
2366N/A#include <sys/types.h>
0N/A#include <limits.h>
2366N/A#include <sys/param.h>
2366N/A#include <sys/systeminfo.h>
0N/A#include <sys/efi_partition.h>
0N/A#include <sys/byteorder.h>
1857N/A#include <sys/systeminfo.h>
1689N/A
1689N/A#include <sys/dktp/fdisk.h>
1842N/A#include <sys/dkio.h>
1689N/A#include <sys/vtoc.h>
0N/A
1864N/A#define CLR_SCR "[1;1H[0J"
1858N/A#define CLR_LIN "[0K"
1689N/A#define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \
0N/A "[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H"
320N/A#define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K"
0N/A#define W_LINE "[12;1H[0K[11;1H[0K"
1689N/A#define E_LINE "[24;1H[0K[23;1H[0K"
1859N/A#define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \
3894N/A "[0K[18;1H[0K[19;1H[0K[13;1H"
4134N/A#define T_LINE "[1;1H[0K"
4134N/A
4134N/A#define DEFAULT_PATH "/dev/rdsk/"
0N/A
1689N/A/* XXX - should be in fdisk.h, used by sd as well */
3999N/A
4134N/A/*
4134N/A * the MAX values are the maximum usable values for BIOS chs values
4134N/A * The MAX_CYL value of 1022 is the maximum usable value
1689N/A * the value of 1023 is a fence value,
4134N/A * indicating no CHS geometry exists for the corresponding LBA value.
4134N/A * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
338N/A * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
338N/A */
0N/A#define MAX_SECT (63)
1689N/A#define MAX_CYL (1022)
1689N/A#define MAX_HEAD (254)
338N/A
0N/A#define DK_MAX_2TB UINT32_MAX /* Max # of sectors in 2TB */
0N/A
1177N/A/* for clear_vtoc() */
0N/A#define OLD 0
0N/A#define NEW 1
0N/A
1963N/A/* readvtoc/writevtoc return codes */
0N/A#define VTOC_OK 0 /* Good VTOC */
0N/A#define VTOC_INVAL 1 /* invalid VTOC */
0N/A#define VTOC_NOTSUP 2 /* operation not supported - EFI label */
0N/A#define VTOC_RWERR 3 /* couldn't read or write VTOC */
4134N/A
4134N/A/*
4134N/A * Support for fdisk(1M) on the SPARC platform
4134N/A * In order to convert little endian values to big endian for SPARC,
0N/A * byte/short and long values must be swapped.
4134N/A * These swapping macros will be used to access information in the
4134N/A * mboot and ipart structures.
4134N/A */
0N/A
1400N/A#ifdef sparc
1400N/A#define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
1400N/A#define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \
1400N/A (les((unsigned)((val)&0xffff0000)>>16)))
1400N/A#else
1963N/A#define les(val) (val)
4134N/A#define lel(val) (val)
1963N/A#endif
0N/A
4134N/A#if defined(_SUNOS_VTOC_16)
0N/A#define VTOC_OFFSET 1
2366N/A#elif defined(_SUNOS_VTOC_8)
4134N/A#define VTOC_OFFSET 0
2366N/A#else
4134N/A#error No VTOC format defined.
4134N/A#endif
0N/A
0N/Astatic char Usage[] = "Usage: fdisk\n"
4134N/A"[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
4134N/A"[ -b masterboot ]\n"
4134N/A"[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
0N/A"[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
0N/A"[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
4134N/A"[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
4134N/A
4134N/Astatic char Usage1[] = " Partition options:\n"
0N/A" -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
0N/A" Create a partition with specific attributes:\n"
4134N/A" id = system id number (fdisk.h) for the partition type\n"
0N/A" act = active partition flag (0 is off and 128 is on)\n"
3853N/A" bhead = beginning head for start of partition\n"
3853N/A" bsect = beginning sector for start of partition\n"
3853N/A" bcyl = beginning cylinder for start of partition\n"
3853N/A" ehead = ending head for end of partition\n"
3853N/A" esect = ending sector for end of partition\n"
4134N/A" ecyl = ending cylinder for end of partition\n"
0N/A" rsect = sector number from start of disk for\n"
0N/A" start of partition\n"
4134N/A" numsect = partition size in sectors\n"
0N/A" -b master_boot\n"
4134N/A" Use master_boot as the master boot file.\n"
4134N/A" -B Create one Solaris partition that uses the entire disk.\n"
0N/A" -E Create one EFI partition that uses the entire disk.\n"
0N/A" -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
0N/A" Delete a partition. See attribute definitions for -A.\n"
4134N/A" -F fdisk_file\n"
0N/A" Use fdisk_file to initialize on-line fdisk table.\n"
4134N/A" -I Forego device checks. Generate a file image of what would go\n"
4134N/A" on a disk using the geometry specified with the -S option.\n"
4134N/A" -n Do not run in interactive mode.\n"
0N/A" -R Open the disk device as read-only.\n"
0N/A" -t Check and adjust VTOC to be consistent with fdisk table.\n"
0N/A" VTOC slices exceeding the partition size will be truncated.\n"
4134N/A" -T Check and adjust VTOC to be consistent with fdisk table.\n"
0N/A" VTOC slices exceeding the partition size will be removed.\n"
0N/A" -W fdisk_file\n"
4134N/A" Write on-disk table to fdisk_file.\n"
0N/A" -W - Write on-disk table to standard output.\n"
3999N/A" -v Display virtual geometry. Must be used with the -W option.\n"
4134N/A" Diagnostic options:\n"
3999N/A" -d Activate debug information about progress.\n"
0N/A" -g Write label geometry to standard output:\n"
4134N/A" PCYL number of physical cylinders\n"
0N/A" NCYL number of usable cylinders\n"
4134N/A" ACYL number of alternate cylinders\n"
4134N/A" BCYL cylinder offset\n"
4134N/A" NHEADS number of heads\n"
0N/A" NSECTORS number of sectors per track\n"
4134N/A" SECTSIZ size of a sector in bytes\n"
4134N/A" -G Write physical geometry to standard output (see -g).\n"
4134N/A" -h Issue this verbose help message.\n"
0N/A" -o offset\n"
0N/A" Block offset from start of disk (default 0). Ignored if\n"
4134N/A" -P # specified.\n"
0N/A" -P fill_patt\n"
0N/A" Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
4134N/A" hexadecimal and is used as number for constant long word\n"
0N/A" pattern. If fill_patt is \"#\" then pattern of block #\n"
4134N/A" for each block. Pattern is put in each block as long words\n"
4134N/A" and fills each block (see -o and -s).\n"
4134N/A" -r Read from a disk to stdout (see -o and -s).\n"
4134N/A" -s size Number of blocks on which to perform operation (see -o).\n"
4134N/A" -S geom_file\n"
4134N/A" Use geom_file to set the label geometry (see -g).\n"
4134N/A" -w Write to a disk from stdin (see -o and -s).";
629N/A
4134N/Astatic char Ostr[] = "Other OS";
4134N/Astatic char Dstr[] = "DOS12";
4134N/Astatic char D16str[] = "DOS16";
0N/Astatic char DDstr[] = "DOS-DATA";
4170N/Astatic char EDstr[] = "EXT-DOS";
4170N/Astatic char DBstr[] = "DOS-BIG";
4134N/Astatic char PCstr[] = "PCIX";
4134N/Astatic char Ustr[] = "UNIX System";
4134N/Astatic char SUstr[] = "Solaris";
4134N/Astatic char SU2str[] = "Solaris2";
4134N/Astatic char X86str[] = "x86 Boot";
0N/Astatic char DIAGstr[] = "Diagnostic";
3999N/Astatic char IFSstr[] = "IFS: NTFS";
3999N/Astatic char AIXstr[] = "AIX Boot";
3999N/Astatic char AIXDstr[] = "AIX Data";
3999N/Astatic char OS2str[] = "OS/2 Boot";
3999N/Astatic char WINstr[] = "Win95 FAT32";
3999N/Astatic char EWINstr[] = "Ext Win95";
3999N/Astatic char FAT95str[] = "FAT16 LBA";
3999N/Astatic char EXTLstr[] = "EXT LBA";
3999N/Astatic char LINUXstr[] = "Linux";
3999N/Astatic char CPMstr[] = "CP/M";
3999N/Astatic char NOVstr[] = "Netware 3.x+";
3999N/Astatic char QNXstr[] = "QNX 4.x";
4134N/Astatic char QNX2str[] = "QNX part 2";
4134N/Astatic char QNX3str[] = "QNX part 3";
4134N/Astatic char LINNATstr[] = "Linux native";
4134N/Astatic char NTFSVOL1str[] = "NT volset 1";
4134N/Astatic char NTFSVOL2str[] = "NT volset 2";
4134N/Astatic char BSDstr[] = "BSD OS";
4134N/Astatic char NEXTSTEPstr[] = "NeXTSTEP";
4134N/Astatic char BSDIFSstr[] = "BSDI FS";
4134N/Astatic char BSDISWAPstr[] = "BSDI swap";
4134N/Astatic char Actvstr[] = "Active";
4134N/Astatic char EFIstr[] = "EFI";
4134N/Astatic char NAstr[] = " ";
4134N/A
0N/A/* All the user options and flags */
0N/Astatic char *Dfltdev; /* name of fixed disk drive */
0N/A
4134N/A/* Diagnostic options */
4134N/Astatic int io_wrt = 0; /* write stdin to disk (-w) */
4134N/Astatic int io_rd = 0; /* read disk and write stdout (-r) */
4134N/Astatic char *io_fatt; /* user supplied pattern (-P pattern) */
4134N/Astatic int io_patt = 0; /* write pattern to disk (-P pattern) */
4333N/Astatic int io_lgeom = 0; /* get label geometry (-g) */
0N/Astatic int io_pgeom = 0; /* get drive physical geometry (-G) */
0N/Astatic char *io_sgeom = 0; /* set label geometry (-S geom_file) */
4333N/Astatic int io_readonly = 0; /* do not write to disk (-R) */
0N/A
0N/A/* The -o offset and -s size options specify the area of the disk on */
0N/A/* which to perform the particular operation; i.e., -P, -r, or -w. */
4134N/Astatic off_t io_offset = 0; /* offset sector (-o offset) */
4134N/Astatic off_t io_size = 0; /* size in sectors (-s size) */
4134N/A
3894N/A/* Partition table flags */
3894N/Astatic int v_flag = 0; /* virtual geometry-HBA flag (-v) */
3894N/Astatic int stdo_flag = 0; /* stdout flag (-W -) */
4134N/Astatic int io_fdisk = 0; /* do fdisk operation */
2464N/Astatic int io_ifdisk = 0; /* interactive partition */
4134N/Astatic int io_nifdisk = 0; /* non-interactive partition (-n) */
4134N/A
4134N/Astatic int io_adjt = 0; /* check/adjust VTOC (truncate (-t)) */
4134N/Astatic int io_ADJT = 0; /* check/adjust VTOC (delete (-T)) */
4134N/Astatic char *io_ffdisk = 0; /* input fdisk file name (-F file) */
4134N/Astatic char *io_Wfdisk = 0; /* output fdisk file name (-W file) */
4134N/Astatic char *io_Afdisk = 0; /* add entry to partition table (-A) */
4134N/Astatic char *io_Dfdisk = 0; /* delete entry from part. table (-D) */
3853N/A
3853N/Astatic char *io_mboot = 0; /* master boot record (-b boot_file) */
4134N/A
4333N/Astatic struct mboot BootCod; /* buffer for master boot record */
4134N/A
4134N/Astatic int io_wholedisk = 0; /* use whole disk for Solaris (-B) */
4134N/Astatic int io_EFIdisk = 0; /* use whole disk for EFI (-E) */
4134N/Astatic int io_debug = 0; /* activate verbose mode (-d) */
4134N/Astatic int io_image = 0; /* create image using geometry (-I) */
4134N/A
4134N/Astatic struct mboot *Bootblk; /* pointer to cut/paste sector zero */
3999N/Astatic char *Bootsect; /* pointer to sector zero buffer */
4134N/Astatic char *Nullsect;
4134N/Astatic struct extvtoc disk_vtoc; /* verify VTOC table */
3999N/Astatic int vt_inval = 0;
4134N/Astatic int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */
4134N/Astatic int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */
4134N/A
0N/Astatic struct ipart Table[FD_NUMPART];
0N/Astatic struct ipart Old_Table[FD_NUMPART];
0N/Astatic int skip_verify[FD_NUMPART]; /* special case skip sz chk */
0N/A
0N/A/* Disk geometry information */
0N/Astatic struct dk_minfo minfo;
4134N/Astatic struct dk_geom disk_geom;
4134N/A
4134N/Astatic int Dev; /* fd for open device */
4134N/A
4134N/Astatic diskaddr_t dev_capacity; /* number of blocks on device */
4134N/Astatic diskaddr_t chs_capacity; /* Numcyl_usable * heads * sectors */
4170N/A
4134N/Astatic int Numcyl_usable; /* Number of usable cylinders */
4142N/A /* used to limit fdisk to 2TB */
4142N/A
4142N/A/* Physical geometry for the drive */
4142N/Astatic int Numcyl; /* number of cylinders */
4142N/Astatic int heads; /* number of heads */
4142N/Astatic int sectors; /* number of sectors per track */
4142N/Astatic int acyl; /* number of alternate sectors */
0N/A
0N/A/* HBA (virtual) geometry for the drive */
0N/Astatic int hba_Numcyl; /* number of cylinders */
0N/Astatic int hba_heads; /* number of heads */
0N/Astatic int hba_sectors; /* number of sectors per track */
0N/A
0N/Astatic int sectsiz; /* sector size */
4134N/A
0N/A/* Load functions for fdisk table modification */
4134N/A#define LOADFILE 0 /* load fdisk from file */
0N/A#define LOADDEL 1 /* delete an fdisk entry */
0N/A#define LOADADD 2 /* add an fdisk entry */
0N/A
0N/A#define CBUFLEN 80
0N/Astatic char s[CBUFLEN];
0N/A
0N/Astatic void update_disk_and_exit(boolean_t table_changed);
0N/Aint main(int argc, char *argv[]);
4134N/Astatic int read_geom(char *sgeom);
4134N/Astatic void dev_mboot_read(void);
0N/Astatic void dev_mboot_write(off_t sect, char *buff, int bootsiz);
4134N/Astatic void mboot_read(void);
4134N/Astatic void fill_patt(void);
0N/Astatic void abs_read(void);
4134N/Astatic void abs_write(void);
3853N/Astatic void load(int funct, char *file);
0N/Astatic void Set_Table_CHS_Values(int ti);
0N/Astatic int insert_tbl(int id, int act,
0N/A int bhead, int bsect, int bcyl,
0N/A int ehead, int esect, int ecyl,
0N/A uint32_t rsect, uint32_t numsect);
0N/Astatic int entry_from_old_table(int id, int act,
0N/A int bhead, int bsect, int bcyl,
4134N/A int ehead, int esect, int ecyl,
4134N/A uint32_t rsect, uint32_t numsect);
0N/Astatic int verify_tbl(void);
4134N/Astatic int pars_fdisk(char *line,
4134N/A int *id, int *act,
4134N/A int *bhead, int *bsect, int *bcyl,
0N/A int *ehead, int *esect, int *ecyl,
0N/A uint32_t *rsect, uint32_t *numsect);
0N/Astatic int validate_part(int id, uint32_t rsect, uint32_t numsect);
0N/Astatic void stage0(void);
0N/Astatic int pcreate(void);
0N/Astatic int specify(uchar_t tsystid);
0N/Astatic void dispmenu(void);
0N/Astatic int pchange(void);
0N/Astatic int ppartid(void);
4134N/Astatic char pdelete(void);
4134N/Astatic void rm_blanks(char *s);
0N/Astatic int getcyl(void);
4134N/Astatic void disptbl(void);
4134N/Astatic void print_Table(void);
0N/Astatic void copy_Table_to_Old_Table(void);
4170N/Astatic void nulltbl(void);
0N/Astatic void copy_Bootblk_to_Table(void);
0N/Astatic void fill_ipart(char *bootptr, struct ipart *partp);
0N/A#ifdef sparc
0N/Auchar_t getbyte(char **bp);
0N/Auint32_t getlong(char **bp);
0N/A#endif
0N/Astatic void copy_Table_to_Bootblk(void);
0N/Astatic int TableChanged(void);
4134N/Astatic void ffile_write(char *file);
4134N/Astatic void fix_slice(void);
0N/Astatic int yesno(void);
4134N/Astatic int readvtoc(void);
4134N/Astatic int writevtoc(void);
0N/Astatic int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
4134N/Astatic int clear_efi(void);
0N/Astatic void clear_vtoc(int table, int part);
0N/Astatic int lecture_and_query(char *warning, char *devname);
629N/Astatic void sanity_check_provided_device(char *devname, int fd);
0N/Astatic char *get_node(char *devname);
0N/A
0N/Astatic void
0N/Aupdate_disk_and_exit(boolean_t table_changed)
0N/A{
0N/A if (table_changed) {
0N/A /*
4134N/A * Copy the new table back to the sector buffer
0N/A * and write it to disk
4134N/A */
0N/A copy_Table_to_Bootblk();
0N/A dev_mboot_write(0, Bootsect, sectsiz);
0N/A }
0N/A
0N/A /* If the VTOC table is wrong fix it (truncation only) */
0N/A if (io_adjt)
0N/A fix_slice();
0N/A
0N/A exit(0);
0N/A}
4134N/A
0N/A
4134N/A
0N/A/*
0N/A * main
0N/A * Process command-line options.
0N/A */
0N/Aint
0N/Amain(int argc, char *argv[])
0N/A{
0N/A int c, i;
4134N/A extern int optind;
4134N/A extern char *optarg;
0N/A int errflg = 0;
4134N/A int diag_cnt = 0;
4134N/A int openmode;
0N/A
4134N/A setbuf(stderr, 0); /* so all output gets out on exit */
0N/A setbuf(stdout, 0);
0N/A
0N/A /* Process the options. */
0N/A while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
0N/A != EOF) {
0N/A switch (c) {
0N/A
0N/A case 'o':
0N/A io_offset = (off_t)strtoull(optarg, 0, 0);
0N/A continue;
4134N/A case 's':
0N/A io_size = (off_t)strtoull(optarg, 0, 0);
4134N/A continue;
0N/A case 'P':
0N/A diag_cnt++;
0N/A io_patt++;
0N/A io_fatt = optarg;
0N/A continue;
0N/A case 'w':
0N/A diag_cnt++;
0N/A io_wrt++;
4134N/A continue;
4134N/A case 'r':
0N/A diag_cnt++;
4134N/A io_rd++;
4134N/A continue;
4134N/A case 'd':
0N/A io_debug++;
4134N/A continue;
0N/A case 'I':
0N/A io_image++;
0N/A continue;
0N/A case 'R':
0N/A io_readonly++;
0N/A continue;
0N/A case 'S':
0N/A diag_cnt++;
4134N/A io_sgeom = optarg;
4134N/A continue;
0N/A case 'T':
4134N/A io_ADJT++;
4134N/A /* FALLTHRU */
4134N/A case 't':
4134N/A io_adjt++;
0N/A continue;
4134N/A case 'B':
0N/A io_wholedisk++;
0N/A io_fdisk++;
0N/A continue;
0N/A case 'E':
0N/A io_EFIdisk++;
0N/A io_fdisk++;
0N/A continue;
0N/A case 'g':
4134N/A diag_cnt++;
4134N/A io_lgeom++;
4134N/A continue;
4134N/A case 'G':
4134N/A diag_cnt++;
4134N/A io_pgeom++;
0N/A continue;
4134N/A case 'n':
4134N/A io_nifdisk++;
4134N/A io_fdisk++;
0N/A continue;
4134N/A case 'F':
0N/A io_fdisk++;
0N/A io_ffdisk = optarg;
4134N/A continue;
4134N/A case 'b':
4134N/A io_mboot = optarg;
4134N/A continue;
0N/A case 'W':
0N/A /*
0N/A * If '-' is the -W argument, then write
0N/A * to standard output, otherwise write
0N/A * to the specified file.
4134N/A */
4134N/A if (strncmp(optarg, "-", 1) == 0)
0N/A stdo_flag = 1;
4134N/A else
4134N/A io_Wfdisk = optarg;
0N/A io_fdisk++;
0N/A continue;
0N/A case 'A':
0N/A io_fdisk++;
0N/A io_Afdisk = optarg;
0N/A continue;
0N/A case 'D':
0N/A io_fdisk++;
0N/A io_Dfdisk = optarg;
4134N/A continue;
4134N/A case 'h':
0N/A (void) fprintf(stderr, "%s\n", Usage);
4134N/A (void) fprintf(stderr, "%s\n", Usage1);
4134N/A exit(0);
0N/A /* FALLTHRU */
4134N/A case 'v':
0N/A v_flag = 1;
0N/A continue;
4134N/A case '?':
4134N/A errflg++;
4134N/A break;
4134N/A }
4134N/A break;
1827N/A }
1827N/A
309N/A if (io_image && io_sgeom && diag_cnt == 1) {
309N/A diag_cnt = 0;
309N/A }
4134N/A
309N/A /* User option checking */
309N/A
309N/A /* By default, run in interactive mode */
309N/A if (!io_fdisk && !diag_cnt && !io_nifdisk) {
309N/A io_ifdisk++;
309N/A io_fdisk++;
4134N/A }
4134N/A if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
309N/A errflg++;
4134N/A }
4134N/A
4134N/A /* Was any error detected? */
4134N/A if (errflg || argc == optind) {
309N/A (void) fprintf(stderr, "%s\n", Usage);
309N/A (void) fprintf(stderr,
309N/A "\nDetailed help is available with the -h option.\n");
0N/A exit(2);
0N/A }
0N/A
4134N/A
4134N/A /* Figure out the correct device node to open */
4134N/A Dfltdev = get_node(argv[optind]);
4134N/A
4134N/A if (io_readonly)
4134N/A openmode = O_RDONLY;
0N/A else
0N/A openmode = O_RDWR|O_CREAT;
0N/A
2086N/A if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
4134N/A (void) fprintf(stderr,
0N/A "fdisk: Cannot open device %s.\n",
0N/A Dfltdev);
0N/A exit(1);
0N/A }
0N/A /*
0N/A * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
0N/A * in that case leave the minfo structure zeroed
0N/A */
0N/A if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
0N/A (void) memset(&minfo, 0, sizeof (minfo));
2086N/A }
0N/A
0N/A /* Get the disk geometry */
0N/A if (!io_image) {
4134N/A /* Get disk's HBA (virtual) geometry */
0N/A errno = 0;
0N/A if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
0N/A
0N/A /*
0N/A * If ioctl isn't implemented on this platform, then
0N/A * turn off flag to print out virtual geometry (-v),
0N/A * otherwise use the virtual geometry.
0N/A */
0N/A
0N/A if (errno == ENOTTY) {
0N/A v_flag = 0;
4134N/A no_virtgeom_ioctl = 1;
4134N/A } else if (errno == EINVAL) {
0N/A /*
0N/A * This means that the ioctl exists, but
0N/A * is invalid for this disk, meaning the
0N/A * disk doesn't have an HBA geometry
0N/A * (like, say, it's larger than 8GB).
0N/A */
0N/A v_flag = 0;
0N/A hba_Numcyl = hba_heads = hba_sectors = 0;
0N/A } else {
0N/A (void) fprintf(stderr,
4134N/A "%s: Cannot get virtual disk geometry.\n",
4134N/A argv[optind]);
4134N/A exit(1);
4134N/A }
4134N/A } else {
4134N/A /* save virtual geometry values obtained by ioctl */
4134N/A hba_Numcyl = disk_geom.dkg_ncyl;
4134N/A hba_heads = disk_geom.dkg_nhead;
4134N/A hba_sectors = disk_geom.dkg_nsect;
4134N/A }
4134N/A
4134N/A errno = 0;
4134N/A if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
4134N/A if (errno == ENOTTY) {
4134N/A no_physgeom_ioctl = 1;
4134N/A } else {
4134N/A (void) fprintf(stderr,
4134N/A "%s: Cannot get physical disk geometry.\n",
4134N/A argv[optind]);
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A }
4134N/A /*
4134N/A * Call DKIOCGGEOM if the ioctls for physical and virtual
4134N/A * geometry fail. Get both from this generic call.
4134N/A */
4134N/A if (no_virtgeom_ioctl && no_physgeom_ioctl) {
4134N/A errno = 0;
4134N/A if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
4134N/A (void) fprintf(stderr,
4134N/A "%s: Cannot get disk label geometry.\n",
0N/A argv[optind]);
4134N/A exit(1);
4134N/A }
4134N/A }
4134N/A
4134N/A Numcyl = disk_geom.dkg_ncyl;
4134N/A heads = disk_geom.dkg_nhead;
4134N/A sectors = disk_geom.dkg_nsect;
4134N/A
4134N/A if (minfo.dki_lbsize != 0)
4134N/A sectsiz = minfo.dki_lbsize;
4134N/A else
4134N/A sectsiz = 512;
4134N/A
4134N/A acyl = disk_geom.dkg_acyl;
4134N/A
4134N/A /*
4134N/A * if hba geometry was not set by DKIOC_VIRTGEOM
4134N/A * or we got an invalid hba geometry
4134N/A * then set hba geometry based on max values
4134N/A */
4134N/A if (no_virtgeom_ioctl ||
4134N/A disk_geom.dkg_ncyl == 0 ||
4134N/A disk_geom.dkg_nhead == 0 ||
4134N/A disk_geom.dkg_nsect == 0 ||
4134N/A disk_geom.dkg_ncyl > MAX_CYL ||
4134N/A disk_geom.dkg_nhead > MAX_HEAD ||
4134N/A disk_geom.dkg_nsect > MAX_SECT) {
4134N/A
0N/A /*
0N/A * turn off flag to print out virtual geometry (-v)
0N/A */
4134N/A v_flag = 0;
0N/A hba_sectors = MAX_SECT;
0N/A hba_heads = MAX_HEAD + 1;
0N/A hba_Numcyl = (Numcyl * heads * sectors) /
0N/A (hba_sectors * hba_heads);
0N/A }
0N/A
4134N/A if (io_debug) {
0N/A (void) fprintf(stderr, "Physical Geometry:\n");
0N/A (void) fprintf(stderr,
4134N/A " cylinders[%d] heads[%d] sectors[%d]\n"
4134N/A " sector size[%d] blocks[%d] mbytes[%d]\n",
0N/A Numcyl,
0N/A heads,
0N/A sectors,
0N/A sectsiz,
0N/A Numcyl * heads * sectors,
0N/A (Numcyl * heads * sectors * sectsiz) / 1048576);
0N/A (void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
4134N/A (void) fprintf(stderr,
4134N/A " cylinders[%d] heads[%d] sectors[%d]\n"
4134N/A " sector size[%d] blocks[%d] mbytes[%d]\n",
4134N/A hba_Numcyl,
0N/A hba_heads,
4134N/A hba_sectors,
0N/A sectsiz,
4134N/A hba_Numcyl * hba_heads * hba_sectors,
0N/A (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
0N/A 1048576);
4134N/A }
0N/A }
4134N/A
4134N/A /* If user has requested a geometry report just do it and exit */
0N/A if (io_lgeom) {
0N/A if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
0N/A (void) fprintf(stderr,
0N/A "%s: Cannot get disk label geometry.\n",
0N/A argv[optind]);
0N/A exit(1);
0N/A }
4134N/A Numcyl = disk_geom.dkg_ncyl;
4134N/A heads = disk_geom.dkg_nhead;
4134N/A sectors = disk_geom.dkg_nsect;
4134N/A if (minfo.dki_lbsize != 0)
4134N/A sectsiz = minfo.dki_lbsize;
4134N/A else
4134N/A sectsiz = 512;
4134N/A
4134N/A acyl = disk_geom.dkg_acyl;
0N/A (void) printf("* Label geometry for device %s\n", Dfltdev);
4134N/A (void) printf(
0N/A "* PCYL NCYL ACYL BCYL NHEAD NSECT"
4134N/A " SECSIZ\n");
0N/A (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
4134N/A Numcyl,
4134N/A disk_geom.dkg_ncyl,
4134N/A disk_geom.dkg_acyl,
4134N/A disk_geom.dkg_bcyl,
0N/A heads,
0N/A sectors,
4134N/A sectsiz);
4134N/A exit(0);
4134N/A } else if (io_pgeom) {
4134N/A if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
2086N/A (void) fprintf(stderr,
0N/A "%s: Cannot get physical disk geometry.\n",
0N/A argv[optind]);
0N/A exit(1);
0N/A }
4134N/A (void) printf("* Physical geometry for device %s\n", Dfltdev);
0N/A (void) printf(
4134N/A "* PCYL NCYL ACYL BCYL NHEAD NSECT"
4134N/A " SECSIZ\n");
0N/A (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
0N/A disk_geom.dkg_pcyl,
0N/A disk_geom.dkg_ncyl,
0N/A disk_geom.dkg_acyl,
0N/A disk_geom.dkg_bcyl,
0N/A disk_geom.dkg_nhead,
0N/A disk_geom.dkg_nsect,
0N/A sectsiz);
4134N/A exit(0);
4134N/A } else if (io_sgeom) {
4134N/A if (read_geom(io_sgeom)) {
4134N/A exit(1);
0N/A } else if (!io_image) {
4134N/A exit(0);
0N/A }
4134N/A }
0N/A
0N/A /*
4134N/A * some drivers may not support DKIOCGMEDIAINFO
4134N/A * in that case use CHS
0N/A */
0N/A chs_capacity = (diskaddr_t)Numcyl * heads * sectors;
0N/A dev_capacity = chs_capacity;
4134N/A Numcyl_usable = Numcyl;
4134N/A
4134N/A if (chs_capacity > DK_MAX_2TB) {
4134N/A /* limit to 2TB */
0N/A Numcyl_usable = DK_MAX_2TB / (heads * sectors);
4134N/A chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors;
4134N/A }
0N/A
0N/A if (minfo.dki_capacity > 0)
0N/A dev_capacity = minfo.dki_capacity;
0N/A
0N/A /* Allocate memory to hold three complete sectors */
0N/A Bootsect = (char *)calloc(3 * sectsiz, 1);
0N/A if (Bootsect == NULL) {
0N/A (void) fprintf(stderr,
4134N/A "fdisk: Unable to obtain enough buffer memory"
4134N/A " (%d bytes).\n",
0N/A 3 * sectsiz);
4134N/A exit(1);
0N/A }
4134N/A
4134N/A Nullsect = Bootsect + sectsiz;
4134N/A /* Zero out the "NULL" sector */
0N/A for (i = 0; i < sectsiz; i++) {
4134N/A Nullsect[i] = 0;
4134N/A }
0N/A
4134N/A /* Find out what the user wants done */
4134N/A if (io_rd) { /* abs disk read */
4134N/A abs_read(); /* will not return */
4134N/A } else if (io_wrt && !io_readonly) {
4170N/A abs_write(); /* will not return */
4170N/A } else if (io_patt && !io_readonly) {
4134N/A fill_patt(); /* will not return */
4134N/A }
4134N/A
4134N/A
4134N/A /* This is the fdisk edit, the real reason for the program. */
4134N/A
4134N/A sanity_check_provided_device(Dfltdev, Dev);
4134N/A
4134N/A /* Get the new BOOT program in case we write a new fdisk table */
4134N/A mboot_read();
4134N/A
4134N/A /* Read from disk master boot */
0N/A dev_mboot_read();
4134N/A
4134N/A /*
4134N/A * Verify and copy the device's fdisk table. This will be used
2464N/A * as the prototype mboot if the device's mboot looks invalid.
4134N/A */
2464N/A Bootblk = (struct mboot *)Bootsect;
4170N/A copy_Bootblk_to_Table();
4134N/A
4134N/A /* save away a copy of Table in Old_Table for sensing changes */
0N/A copy_Table_to_Old_Table();
0N/A
4134N/A /* Load fdisk table from specified file (-F fdisk_file) */
4134N/A if (io_ffdisk) {
4134N/A /* Load and verify user-specified table parameters */
4134N/A load(LOADFILE, io_ffdisk);
4134N/A }
0N/A
4134N/A /* Does user want to delete or add an entry? */
4134N/A if (io_Dfdisk) {
0N/A load(LOADDEL, io_Dfdisk);
4134N/A }
4134N/A if (io_Afdisk) {
4134N/A load(LOADADD, io_Afdisk);
0N/A }
0N/A
0N/A if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
0N/A /* Check if there is no fdisk table */
0N/A if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
0N/A if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
4134N/A (void) printf(
4134N/A "No fdisk table exists. The default"
4134N/A " partition for the disk is:\n\n"
4134N/A " a 100%% \"SOLARIS System\" "
0N/A "partition\n\n"
4134N/A "Type \"y\" to accept the default "
4134N/A "partition, otherwise type \"n\" to "
4134N/A "edit the\n partition table.\n");
4134N/A
4134N/A if (Numcyl > Numcyl_usable)
4134N/A (void) printf("WARNING: Disk is larger"
4134N/A " than 2TB. Solaris partition will"
4134N/A " be limited to 2 TB.\n");
4134N/A }
4134N/A
0N/A /* Edit the partition table as directed */
4134N/A if (io_wholedisk ||(io_ifdisk && yesno())) {
0N/A
4134N/A /* Default scenario */
0N/A nulltbl();
4134N/A /* now set up UNIX System partition */
4134N/A Table[0].bootid = ACTIVE;
4134N/A Table[0].relsect = lel(heads * sectors);
3109N/A
0N/A Table[0].numsect =
3109N/A lel((ulong_t)((Numcyl_usable - 1) *
3109N/A heads * sectors));
3109N/A
3109N/A Table[0].systid = SUNIXOS2; /* Solaris */
3109N/A
3109N/A /* calculate CHS values for table entry 0 */
3109N/A Set_Table_CHS_Values(0);
3109N/A update_disk_and_exit(B_TRUE);
0N/A } else if (io_EFIdisk) {
0N/A /* create an EFI partition for the whole disk */
0N/A nulltbl();
0N/A i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
0N/A (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB :
0N/A (dev_capacity - 1));
0N/A if (i != 0) {
0N/A (void) fprintf(stderr,
0N/A "Error creating EFI partition\n");
0N/A exit(1);
0N/A }
0N/A update_disk_and_exit(B_TRUE);
0N/A }
0N/A }
0N/A }
4134N/A
3679N/A /* Display complete fdisk table entries for debugging purposes */
3679N/A if (io_debug) {
3679N/A (void) fprintf(stderr, "Partition Table Entry Values:\n");
3679N/A print_Table();
3679N/A if (io_ifdisk) {
4134N/A (void) fprintf(stderr, "\n");
4134N/A (void) fprintf(stderr, "Press Enter to continue.\n");
3679N/A (void) fgets(s, sizeof (s), stdin);
3679N/A }
3679N/A }
4134N/A
4134N/A /* Interactive fdisk mode */
3679N/A if (io_ifdisk) {
773N/A (void) printf(CLR_SCR);
0N/A disptbl();
2366N/A for (;;) {
2366N/A stage0();
2366N/A copy_Bootblk_to_Table();
2366N/A disptbl();
2366N/A }
2366N/A }
2366N/A
4134N/A /* If user wants to write the table to a file, do it */
4134N/A if (io_Wfdisk)
4134N/A ffile_write(io_Wfdisk);
4134N/A else if (stdo_flag)
2366N/A ffile_write((char *)stdout);
2366N/A
4134N/A update_disk_and_exit(TableChanged() == 1);
4134N/A return (0);
4134N/A}
4134N/A
0N/A/*
0N/A * read_geom
0N/A * Read geometry from specified file (-S).
0N/A */
0N/A
0N/Astatic int
0N/Aread_geom(char *sgeom)
4134N/A{
4134N/A char line[256];
4134N/A FILE *fp;
4134N/A
4134N/A /* open the prototype file */
4134N/A if ((fp = fopen(sgeom, "r")) == NULL) {
4134N/A (void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
4134N/A io_sgeom);
4134N/A return (1);
4134N/A }
4134N/A
4134N/A /* Read a line from the file */
4134N/A while (fgets(line, sizeof (line) - 1, fp)) {
4134N/A if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
4134N/A continue;
4134N/A else {
4134N/A line[strlen(line)] = '\0';
4134N/A if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
4134N/A &disk_geom.dkg_pcyl,
4134N/A &disk_geom.dkg_ncyl,
4134N/A &disk_geom.dkg_acyl,
4134N/A &disk_geom.dkg_bcyl,
0N/A &disk_geom.dkg_nhead,
0N/A &disk_geom.dkg_nsect,
2086N/A &sectsiz) != 7) {
0N/A (void) fprintf(stderr,
0N/A "Syntax error:\n \"%s\".\n",
4134N/A line);
4134N/A return (1);
0N/A }
0N/A break;
0N/A } /* else */
0N/A } /* while (fgets(line, sizeof (line) - 1, fp)) */
0N/A
0N/A if (!io_image) {
4134N/A if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Cannot set label geometry.\n");
0N/A return (1);
4134N/A }
0N/A } else {
0N/A Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
0N/A heads = hba_heads = disk_geom.dkg_nhead;
4134N/A sectors = hba_sectors = disk_geom.dkg_nsect;
4134N/A acyl = disk_geom.dkg_acyl;
0N/A }
0N/A
0N/A (void) fclose(fp);
0N/A return (0);
0N/A}
0N/A
4134N/A/*
0N/A * dev_mboot_read
0N/A * Read the master boot sector from the device.
0N/A */
4134N/Astatic void
4134N/Adev_mboot_read(void)
868N/A{
868N/A if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
1400N/A perror("Error in ioctl DKIOCGMBOOT");
868N/A }
0N/A if (errno == 0)
0N/A return;
0N/A if (lseek(Dev, 0, SEEK_SET) == -1) {
0N/A (void) fprintf(stderr,
0N/A "fdisk: Error seeking to partition table on %s.\n",
0N/A Dfltdev);
0N/A if (!io_image)
0N/A exit(1);
4134N/A }
4134N/A if (read(Dev, Bootsect, sectsiz) != sectsiz) {
868N/A (void) fprintf(stderr,
868N/A "fdisk: Error reading partition table from %s.\n",
1400N/A Dfltdev);
868N/A if (!io_image)
0N/A exit(1);
0N/A }
4134N/A}
4134N/A
0N/A/*
0N/A * dev_mboot_write
0N/A * Write the master boot sector to the device.
0N/A */
0N/Astatic void
2086N/Adev_mboot_write(off_t sect, char *buff, int bootsiz)
0N/A{
320N/A int new_pt, old_pt, error;
320N/A int clr_efi = -1;
320N/A
4134N/A if (io_readonly)
320N/A return;
4134N/A
320N/A if (io_debug) {
320N/A (void) fprintf(stderr, "About to write fdisk table:\n");
320N/A print_Table();
868N/A if (io_ifdisk) {
868N/A (void) fprintf(stderr, "Press Enter to continue.\n");
1400N/A (void) fgets(s, sizeof (s), stdin);
868N/A }
320N/A }
0N/A
0N/A /*
0N/A * If the new table has any Solaris partitions and the old
0N/A * table does not have an entry that describes it
0N/A * exactly then clear the old vtoc (if any).
4134N/A */
4134N/A for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
0N/A
4134N/A /* We only care about potential Solaris parts. */
4134N/A if (Table[new_pt].systid != SUNIXOS &&
0N/A Table[new_pt].systid != SUNIXOS2)
4134N/A continue;
3902N/A
0N/A /* Does the old table have an exact entry for the new entry? */
0N/A for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
0N/A
0N/A /* We only care about old Solaris partitions. */
0N/A if ((Old_Table[old_pt].systid == SUNIXOS) ||
0N/A (Old_Table[old_pt].systid == SUNIXOS2)) {
0N/A
0N/A /* Is this old one the same as a new one? */
0N/A if ((Old_Table[old_pt].relsect ==
4134N/A Table[new_pt].relsect) &&
4134N/A (Old_Table[old_pt].numsect ==
4134N/A Table[new_pt].numsect))
4134N/A break; /* Yes */
0N/A }
4134N/A }
3902N/A
0N/A /* Did a solaris partition change location or size? */
0N/A if (old_pt >= FD_NUMPART) {
0N/A /* Yes clear old vtoc */
0N/A if (io_debug) {
0N/A (void) fprintf(stderr,
0N/A "Clearing VTOC labels from NEW"
0N/A " table\n");
4134N/A }
4134N/A clear_vtoc(NEW, new_pt);
0N/A }
4134N/A }
4134N/A
4134N/A
4134N/A /* see if the old table had EFI */
4134N/A for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
4134N/A if (Old_Table[old_pt].systid == EFI_PMBR) {
4134N/A clr_efi = old_pt;
0N/A }
1689N/A }
4134N/A
0N/A /* look to see if a EFI partition changed in relsect/numsect */
0N/A for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
0N/A if (Table[new_pt].systid != EFI_PMBR)
4134N/A continue;
4134N/A for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
4134N/A if ((Old_Table[old_pt].systid ==
2464N/A Table[new_pt].systid) &&
0N/A (Old_Table[old_pt].relsect ==
2464N/A Table[new_pt].relsect) &&
0N/A (Old_Table[old_pt].numsect ==
4134N/A Table[new_pt].numsect))
4134N/A break;
2464N/A }
2464N/A
4134N/A /*
4134N/A * if EFI partition changed, set the flag to clear
2464N/A * the EFI GPT
4134N/A */
2464N/A if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
2464N/A clr_efi = 0;
4134N/A }
4134N/A break;
3902N/A }
2464N/A
2464N/A /* clear labels if necessary */
2464N/A if (clr_efi >= 0) {
4134N/A if (io_debug) {
4134N/A (void) fprintf(stderr, "Clearing EFI labels\n");
4134N/A }
2464N/A if ((error = clear_efi()) != 0) {
2464N/A if (io_debug) {
4134N/A (void) fprintf(stderr,
4134N/A "\tError %d clearing EFI labels"
2464N/A " (probably no EFI labels exist)\n",
2464N/A error);
3853N/A }
4134N/A }
4134N/A }
4134N/A
4134N/A if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
0N/A (void) fprintf(stderr,
2464N/A "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
868N/A Dfltdev);
2464N/A }
2464N/A if (errno == 0)
2464N/A return;
2464N/A
2464N/A /* write to disk drive */
2464N/A if (lseek(Dev, sect, SEEK_SET) == -1) {
2464N/A (void) fprintf(stderr,
2464N/A "fdisk: Error seeking to master boot record on %s.\n",
2464N/A Dfltdev);
868N/A exit(1);
2464N/A }
868N/A if (write(Dev, buff, bootsiz) != bootsiz) {
2464N/A (void) fprintf(stderr,
2464N/A "fdisk: Error writing master boot record to %s.\n",
2464N/A Dfltdev);
2464N/A exit(1);
2464N/A }
2464N/A}
2464N/A
4134N/A/*
4134N/A * mboot_read
868N/A * Read the prototype boot records from the files.
0N/A */
0N/Astatic void
0N/Amboot_read(void)
0N/A{
0N/A int mDev, i;
0N/A struct ipart *part;
4134N/A
4134N/A#if defined(i386) || defined(sparc)
4134N/A /*
4134N/A * If the master boot file hasn't been specified, use the
0N/A * implementation architecture name to generate the default one.
4134N/A */
4134N/A if (io_mboot == (char *)0) {
4134N/A /*
4134N/A * Bug ID 1249035:
4134N/A * The mboot file must be delivered on all platforms
4134N/A * and installed in a non-platform-dependent
0N/A * directory; i.e., /usr/lib/fs/ufs.
4134N/A */
0N/A io_mboot = "/usr/lib/fs/ufs/mboot";
0N/A }
3902N/A
1963N/A /* First read in the master boot record */
1963N/A
1963N/A /* Open the master boot proto file */
1963N/A if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1963N/A (void) fprintf(stderr,
1963N/A "fdisk: Cannot open master boot file %s.\n",
1963N/A io_mboot);
1963N/A exit(1);
1963N/A }
0N/A
0N/A /* Read the master boot program */
0N/A if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
0N/A (struct mboot)) {
0N/A (void) fprintf(stderr,
0N/A "fdisk: Cannot read master boot file %s.\n",
0N/A io_mboot);
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A /* Is this really a master boot record? */
4134N/A if (les(BootCod.signature) != MBB_MAGIC) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Invalid master boot file %s.\n", io_mboot);
0N/A (void) fprintf(stderr,
4134N/A "Bad magic number: is %x, but should be %x.\n",
0N/A les(BootCod.signature), MBB_MAGIC);
4134N/A exit(1);
0N/A }
3902N/A
0N/A (void) close(mDev);
0N/A#else
0N/A#error fdisk needs to be ported to new architecture
0N/A#endif
0N/A
3902N/A /* Zero out the partitions part of this record */
0N/A part = (struct ipart *)BootCod.parts;
3916N/A for (i = 0; i < FD_NUMPART; i++, part++) {
3916N/A (void) memset(part, 0, sizeof (struct ipart));
3916N/A }
3916N/A
3902N/A}
650N/A
650N/A/*
0N/A * fill_patt
0N/A * Fill the disk with user/sector number pattern.
0N/A */
3352N/Astatic void
0N/Afill_patt(void)
0N/A{
0N/A int *buff_ptr, i;
650N/A off_t *off_ptr;
3948N/A int io_fpatt = 0;
3948N/A int io_ipatt = 0;
0N/A
0N/A if (strncmp(io_fatt, "#", 1) != 0) {
0N/A io_fpatt++;
0N/A io_ipatt = strtoul(io_fatt, 0, 0);
0N/A buff_ptr = (int *)Bootsect;
0N/A for (i = 0; i < sectsiz; i += 4, buff_ptr++)
0N/A *buff_ptr = io_ipatt;
0N/A }
4134N/A
4134N/A /*
4134N/A * Fill disk with pattern based on block number.
0N/A * Write to the disk at absolute relative block io_offset
4134N/A * for io_size blocks.
0N/A */
0N/A while (io_size--) {
0N/A off_ptr = (off_t *)Bootsect;
2464N/A if (!io_fpatt) {
0N/A for (i = 0; i < sectsiz;
2464N/A i += sizeof (off_t), off_ptr++)
0N/A *off_ptr = io_offset;
3902N/A }
0N/A /* Write the data to disk */
2464N/A if (lseek(Dev, (off_t)(sectsiz * io_offset++),
650N/A SEEK_SET) == -1) {
3902N/A (void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
3352N/A Dfltdev);
3352N/A exit(1);
3352N/A }
2464N/A if (write(Dev, Bootsect, sectsiz) != sectsiz) {
2464N/A (void) fprintf(stderr, "fdisk: Error writing %s.\n",
2464N/A Dfltdev);
2464N/A exit(1);
2464N/A }
2464N/A } /* while (--io_size); */
2464N/A}
2464N/A
2464N/A/*
2464N/A * abs_read
650N/A * Read from the disk at absolute relative block io_offset for
0N/A * io_size blocks. Write the data to standard ouput (-r).
2464N/A */
4134N/Astatic void
4134N/Aabs_read(void)
0N/A{
2464N/A int c;
2464N/A
2464N/A while (io_size--) {
2464N/A if (lseek(Dev, (off_t)(sectsiz * io_offset++),
2464N/A SEEK_SET) == -1) {
2464N/A (void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
2464N/A Dfltdev);
3902N/A exit(1);
0N/A }
0N/A if (read(Dev, Bootsect, sectsiz) != sectsiz) {
2464N/A (void) fprintf(stderr, "fdisk: Error reading %s.\n",
0N/A Dfltdev);
2464N/A exit(1);
2464N/A }
2464N/A
2464N/A /* Write to standard ouptut */
0N/A if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
0N/A if (c >= 0) {
0N/A if (io_debug)
0N/A (void) fprintf(stderr,
0N/A "fdisk: Output warning: %d of %d"
0N/A " characters written.\n",
0N/A c, sectsiz);
4134N/A exit(2);
4134N/A } else {
0N/A perror("write error on output file.");
4134N/A exit(2);
4134N/A }
4134N/A } /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
4134N/A /* != sectsiz) */
4134N/A } /* while (--io_size); */
4134N/A exit(0);
0N/A}
4134N/A
0N/A/*
4134N/A * abs_write
0N/A * Read the data from standard input. Write to the disk at
0N/A * absolute relative block io_offset for io_size blocks (-w).
2464N/A */
0N/Astatic void
2464N/Aabs_write(void)
0N/A{
2464N/A int c, i;
0N/A
2464N/A while (io_size--) {
2464N/A int part_exit = 0;
2464N/A /* Read from standard input */
2464N/A if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
2464N/A if (c >= 0) {
3902N/A if (io_debug)
2464N/A (void) fprintf(stderr,
0N/A "fdisk: WARNING: Incomplete read (%d of"
2464N/A " %d characters read) on input file.\n",
650N/A c, sectsiz);
3352N/A /* Fill pattern to mark partial sector in buf */
3352N/A for (i = c; i < sectsiz; ) {
3352N/A Bootsect[i++] = 0x41;
3352N/A Bootsect[i++] = 0x62;
2464N/A Bootsect[i++] = 0x65;
2464N/A Bootsect[i++] = 0;
2464N/A }
2464N/A part_exit++;
2464N/A } else {
2464N/A perror("read error on input file.");
2464N/A exit(2);
2464N/A }
2464N/A
2464N/A }
650N/A /* Write to disk drive */
0N/A if (lseek(Dev, (off_t)(sectsiz * io_offset++),
2464N/A SEEK_SET) == -1) {
2464N/A (void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
2464N/A Dfltdev);
0N/A exit(1);
0N/A }
2464N/A if (write(Dev, Bootsect, sectsiz) != sectsiz) {
2464N/A (void) fprintf(stderr, "fdisk: Error writing %s.\n",
3916N/A Dfltdev);
3916N/A exit(1);
3916N/A }
3916N/A if (part_exit)
3916N/A exit(0);
3902N/A } /* while (--io_size); */
2464N/A exit(1);
2464N/A}
0N/A
2464N/A
0N/A/*
2464N/A * load
2464N/A * Load will either read the fdisk table from a file or add or
2464N/A * delete an entry (-A, -D, -F).
2464N/A */
0N/A
0N/Astatic void
0N/Aload(int funct, char *file)
0N/A{
0N/A int id;
0N/A int act;
0N/A int bhead;
2366N/A int bsect;
2366N/A int bcyl;
2366N/A int ehead;
2366N/A int esect;
2366N/A int ecyl;
2366N/A uint32_t rsect;
2366N/A uint32_t numsect;
2366N/A char line[256];
2366N/A int i = 0;
2366N/A int j;
2366N/A FILE *fp;
4134N/A
2366N/A switch (funct) {
2366N/A
2366N/A case LOADFILE:
2366N/A
2366N/A /*
2366N/A * Zero out the table before loading it, which will
2366N/A * force it to be updated on disk later (-F
2366N/A * fdisk_file).
2366N/A */
2366N/A nulltbl();
2366N/A
2366N/A /* Open the prototype file */
2366N/A if ((fp = fopen(file, "r")) == NULL) {
2366N/A (void) fprintf(stderr,
2366N/A "fdisk: Cannot open prototype partition file %s.\n",
2366N/A file);
2366N/A exit(1);
2366N/A }
2366N/A
2366N/A /* Read a line from the file */
2366N/A while (fgets(line, sizeof (line) - 1, fp)) {
2366N/A if (pars_fdisk(line, &id, &act, &bhead, &bsect,
2366N/A &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
2366N/A continue;
2366N/A }
2366N/A
2366N/A /*
2366N/A * Validate the partition. It cannot start at sector
2366N/A * 0 unless it is UNUSED or already exists
2366N/A */
2366N/A if (validate_part(id, rsect, numsect) < 0) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Error on entry \"%s\".\n",
3853N/A line);
3853N/A exit(1);
3853N/A }
4134N/A
4134N/A if (entry_from_old_table(id, act, bhead, bsect,
4134N/A bcyl, ehead, esect, ecyl, rsect, numsect)) {
3853N/A /*
4134N/A * If we got here it means we copied an
4134N/A * unmodified entry. So there is no need
3853N/A * to insert it in the table or do any
3853N/A * checks against disk size.
3853N/A *
3853N/A * This is a work around on the following
3853N/A * situation (for IDE disks, at least):
3853N/A * Different operation systems calculate
4134N/A * disk size different ways, of which there
3853N/A * are two main ways.
4134N/A *
0N/A * The first, rounds the disk size to modulo
4134N/A * cylinder size (virtual made-up cylinder
0N/A * usually based on maximum number of heads
4134N/A * and sectors in partition table fields).
0N/A * Our OS's (for IDE) and most other "Unix"
4134N/A * type OS's do this.
868N/A *
4134N/A * The second, uses every single block
868N/A * on the disk (to maximize available space).
4134N/A * Since disk manufactures do not know about
0N/A * "virtual cylinders", there are some number
4134N/A * of blocks that make up a partial cylinder
0N/A * at the end of the disk.
4134N/A *
4134N/A * The difference between these two methods
4134N/A * is where the problem is. When one
4134N/A * tries to install Solaris/OpenSolaris on
4134N/A * a disk that has another OS using that
4134N/A * "partial cylinder", install fails. It fails
4134N/A * since fdisk thinks its asked to create a
4134N/A * partition with the -F option that contains
4134N/A * a partition that runs off the end of the
4134N/A * disk.
4134N/A */
4134N/A continue;
4134N/A }
4134N/A
4134N/A /*
4134N/A * Find an unused entry to use and put the entry
4134N/A * in table
4134N/A */
4134N/A if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
4134N/A esect, ecyl, rsect, numsect) < 0) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Error on entry \"%s\".\n",
4134N/A line);
4134N/A exit(1);
4134N/A }
4134N/A } /* while (fgets(line, sizeof (line) - 1, fp)) */
4134N/A
4134N/A if (verify_tbl() < 0) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Cannot create partition table\n");
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A (void) fclose(fp);
4134N/A return;
4134N/A
4134N/A case LOADDEL:
4134N/A
0N/A /* Parse the user-supplied deletion line (-D) */
0N/A if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
0N/A &ehead, &esect, &ecyl, &rsect, &numsect)) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Syntax error \"%s\"\n", file);
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A /* Find the exact entry in the table */
4134N/A for (i = 0; i < FD_NUMPART; i++) {
4134N/A if (Table[i].systid == id &&
4134N/A Table[i].bootid == act &&
4134N/A Table[i].beghead == bhead &&
4134N/A Table[i].begsect == ((bsect & 0x3f) |
4134N/A (uchar_t)((bcyl>>2) & 0xc0)) &&
4134N/A Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
4134N/A Table[i].endhead == ehead &&
4134N/A Table[i].endsect == ((esect & 0x3f) |
4134N/A (uchar_t)((ecyl>>2) & 0xc0)) &&
4134N/A Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
4134N/A Table[i].relsect == lel(rsect) &&
4134N/A Table[i].numsect == lel(numsect)) {
4134N/A
4134N/A /*
4134N/A * Found the entry. Now move rest of
4134N/A * entries up toward the top of the
4134N/A * table, leaving available entries at
4134N/A * the end of the fdisk table.
4134N/A */
4134N/A for (j = i; j < FD_NUMPART - 1; j++) {
4134N/A Table[j].systid = Table[j + 1].systid;
4134N/A Table[j].bootid = Table[j + 1].bootid;
0N/A Table[j].beghead = Table[j + 1].beghead;
0N/A Table[j].begsect = Table[j + 1].begsect;
0N/A Table[j].begcyl = Table[j + 1].begcyl;
0N/A Table[j].endhead = Table[j + 1].endhead;
0N/A Table[j].endsect = Table[j + 1].endsect;
0N/A Table[j].endcyl = Table[j + 1].endcyl;
0N/A Table[j].relsect = Table[j + 1].relsect;
4134N/A Table[j].numsect = Table[j + 1].numsect;
4134N/A }
4134N/A
0N/A /*
4134N/A * Mark the last entry as unused in case
4134N/A * all table entries were in use prior
4134N/A * to the deletion.
4134N/A */
4134N/A
4134N/A Table[FD_NUMPART - 1].systid = UNUSED;
0N/A Table[FD_NUMPART - 1].bootid = 0;
0N/A return;
0N/A }
0N/A }
0N/A (void) fprintf(stderr,
0N/A "fdisk: Entry does not match any existing partition:\n"
3895N/A " \"%s\"\n",
0N/A file);
4134N/A exit(1);
4134N/A /* FALLTHRU */
3853N/A
3853N/A case LOADADD:
0N/A
4134N/A /* Parse the user-supplied addition line (-A) */
4134N/A if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
4134N/A &esect, &ecyl, &rsect, &numsect)) {
4134N/A (void) fprintf(stderr,
0N/A "fdisk: Syntax error \"%s\"\n", file);
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A /* Validate the partition. It cannot start at sector 0 */
4134N/A if (rsect == 0) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: New partition cannot start at sector 0:\n"
0N/A " \"%s\".\n",
0N/A file);
3999N/A exit(1);
0N/A }
0N/A
4134N/A /*
4134N/A * if the user wishes to add an EFI partition, we need
4134N/A * more extensive validation. rsect should be 1, and
4134N/A * numsect should equal the entire disk capacity - 1
4134N/A */
4134N/A
4134N/A if (id == EFI_PMBR) {
4134N/A if (rsect != 1) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: EFI partitions must start at sector"
4134N/A " 1 (input rsect = %d)\n", rsect);
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A
4134N/A if (dev_capacity > DK_MAX_2TB) {
4134N/A if (numsect != DK_MAX_2TB) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: EFI partitions must "
4134N/A "encompass the entire maximum 2 TB "
4134N/A "(input numsect: %u - max: %llu)\n",
4134N/A numsect, (diskaddr_t)DK_MAX_2TB);
4134N/A exit(1);
4134N/A }
4134N/A } else if (numsect != dev_capacity - 1) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: EFI partitions must encompass the "
4134N/A "entire disk\n"
4134N/A "(input numsect: %u - avail: %llu)\n",
4134N/A numsect,
4134N/A dev_capacity - 1);
4134N/A exit(1);
4134N/A }
4134N/A }
4134N/A
4134N/A /* Find unused entry for use and put entry in table */
4134N/A if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
4134N/A ecyl, rsect, numsect) < 0) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Invalid entry could not be inserted:\n"
4134N/A " \"%s\"\n",
4134N/A file);
4134N/A exit(1);
4134N/A }
4134N/A
4134N/A /* Make sure new entry does not overlap existing entry */
4134N/A if (verify_tbl() < 0) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Cannot create partition \"%s\"\n", file);
4134N/A exit(1);
4134N/A }
4134N/A } /* switch funct */
4134N/A}
4134N/A
4134N/A/*
4134N/A * Set_Table_CHS_Values
4134N/A *
4134N/A * This will calculate the CHS values for beginning and ending CHS
4134N/A * for a single partition table entry (ti) based on the relsect
4134N/A * and numsect values contained in the partion table entry.
4134N/A *
4134N/A * hba_heads and hba_sectors contain the number of heads and sectors.
4134N/A *
4134N/A * If the number of cylinders exceeds the MAX_CYL,
4134N/A * then maximum values will be placed in the corresponding chs entry.
4134N/A */
4134N/Astatic void
4134N/ASet_Table_CHS_Values(int ti)
4134N/A{
4134N/A uint32_t lba, cy, hd, sc;
4134N/A
4134N/A lba = (uint32_t)Table[ti].relsect;
4134N/A if (lba >= hba_heads * hba_sectors * MAX_CYL) {
4134N/A /*
4134N/A * the lba address cannot be expressed in CHS value
4134N/A * so store the maximum CHS field values in the CHS fields.
4134N/A */
4134N/A cy = MAX_CYL + 1;
4134N/A hd = MAX_HEAD;
4134N/A sc = MAX_SECT;
4134N/A } else {
4134N/A cy = lba / hba_sectors / hba_heads;
4134N/A hd = lba / hba_sectors % hba_heads;
4134N/A sc = lba % hba_sectors + 1;
4134N/A }
4134N/A Table[ti].begcyl = cy & 0xff;
4134N/A Table[ti].beghead = (uchar_t)hd;
4134N/A Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
4134N/A
4134N/A /*
4134N/A * This code is identical to the code above
4134N/A * except that it works on ending CHS values
4134N/A */
4134N/A lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
4134N/A if (lba >= hba_heads * hba_sectors * MAX_CYL) {
4134N/A cy = MAX_CYL + 1;
0N/A hd = MAX_HEAD;
0N/A sc = MAX_SECT;
0N/A } else {
0N/A cy = lba / hba_sectors / hba_heads;
868N/A hd = lba / hba_sectors % hba_heads;
868N/A sc = lba % hba_sectors + 1;
1400N/A }
868N/A Table[ti].endcyl = cy & 0xff;
0N/A Table[ti].endhead = (uchar_t)hd;
4134N/A Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
4134N/A}
4134N/A
4134N/A/*
2086N/A * insert_tbl
0N/A * Insert entry into fdisk table. Check all user-supplied values
0N/A * for the entry, but not the validity relative to other table
0N/A * entries!
0N/A */
0N/Astatic int
0N/Ainsert_tbl(
0N/A int id, int act,
0N/A int bhead, int bsect, int bcyl,
0N/A int ehead, int esect, int ecyl,
4134N/A uint32_t rsect, uint32_t numsect)
4134N/A{
4134N/A int i;
4134N/A
4134N/A /* validate partition size */
4134N/A if (((diskaddr_t)rsect + numsect) > dev_capacity) {
4134N/A (void) fprintf(stderr,
4134N/A "fdisk: Partition table exceeds the size of the disk.\n");
4134N/A return (-1);
4134N/A }
0N/A
0N/A
4134N/A /* find UNUSED partition table entry */
0N/A for (i = 0; i < FD_NUMPART; i++) {
4134N/A if (Table[i].systid == UNUSED) {
4134N/A break;
1857N/A }
4134N/A }
4134N/A if (i >= FD_NUMPART) {
4134N/A (void) fprintf(stderr, "fdisk: Partition table is full.\n");
0N/A return (-1);
0N/A }
650N/A
650N/A
650N/A Table[i].systid = (uchar_t)id;
650N/A Table[i].bootid = (uchar_t)act;
0N/A Table[i].numsect = lel(numsect);
0N/A Table[i].relsect = lel(rsect);
0N/A
0N/A /*
0N/A * If we have been called with a valid geometry, use it
0N/A * valid means non-zero values that fit in the BIOS fields
0N/A */
0N/A if (0 < bsect && bsect <= MAX_SECT &&
0N/A 0 <= bhead && bhead <= MAX_HEAD &&
4134N/A 0 < esect && esect <= MAX_SECT &&
4134N/A 0 <= ehead && ehead <= MAX_HEAD) {
4134N/A if (bcyl > MAX_CYL)
4134N/A bcyl = MAX_CYL + 1;
4134N/A if (ecyl > MAX_CYL)
4134N/A ecyl = MAX_CYL + 1;
4134N/A Table[i].begcyl = bcyl & 0xff;
4134N/A Table[i].endcyl = ecyl & 0xff;
4134N/A Table[i].beghead = (uchar_t)bhead;
0N/A Table[i].endhead = (uchar_t)ehead;
0N/A Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
4134N/A Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
0N/A } else {
4134N/A
4134N/A /*
1755N/A * The specified values are invalid,
1755N/A * so calculate the values based on hba_heads, hba_sectors
1755N/A */
4134N/A Set_Table_CHS_Values(i);
4134N/A }
4134N/A
4134N/A /*
1755N/A * return partition index
4134N/A */
1755N/A return (i);
1755N/A}
1755N/A
0N/A/*
0N/A * entry_from_old_table
1689N/A * If the specified entry is in the old table and is not a Solaris entry
4134N/A * then insert same entry into new fdisk table. If we do this then
4134N/A * all checks are skipped for that entry!
4134N/A */
0N/Astatic int
0N/Aentry_from_old_table(
0N/A int id, int act,
0N/A int bhead, int bsect, int bcyl,
0N/A int ehead, int esect, int ecyl,
0N/A uint32_t rsect, uint32_t numsect)
0N/A{
868N/A uint32_t i, j;
868N/A
1400N/A if (id == SUNIXOS || id == SUNIXOS2)
868N/A return (0);
0N/A for (i = 0; i < FD_NUMPART; i++) {
0N/A if (Old_Table[i].systid == id &&
4134N/A Old_Table[i].bootid == act &&
4134N/A Old_Table[i].beghead == bhead &&
4134N/A Old_Table[i].begsect == ((bsect & 0x3f) |
1886N/A (uchar_t)((bcyl>>2) & 0xc0)) &&
4134N/A Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
4134N/A Old_Table[i].endhead == ehead &&
0N/A Old_Table[i].endsect == ((esect & 0x3f) |
0N/A (uchar_t)((ecyl>>2) & 0xc0)) &&
0N/A Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
0N/A Old_Table[i].relsect == lel(rsect) &&
0N/A Old_Table[i].numsect == lel(numsect)) {
0N/A /* find UNUSED partition table entry */
0N/A for (j = 0; j < FD_NUMPART; j++) {
0N/A if (Table[j].systid == UNUSED) {
0N/A (void) memcpy(&Table[j], &Old_Table[i],
0N/A sizeof (Table[0]));
4134N/A skip_verify[j] = 1;
4134N/A return (1);
4134N/A
4134N/A }
4134N/A }
4134N/A return (0);
4134N/A }
4134N/A
4134N/A }
0N/A return (0);
0N/A}
4134N/A
0N/A/*
4134N/A * verify_tbl
4134N/A * Verify that no partition entries overlap or exceed the size of
0N/A * the disk.
4134N/A */
4134N/Astatic int
1079N/Averify_tbl(void)
1079N/A{
0N/A uint32_t i, j, rsect, numsect;
4134N/A int noMoreParts = 0;
4134N/A int numParts = 0;
1079N/A
4134N/A /* Make sure new entry does not overlap an existing entry */
4134N/A for (i = 0; i < FD_NUMPART - 1; i++) {
4134N/A if (Table[i].systid != UNUSED) {
4134N/A numParts++;
4134N/A /*
4134N/A * No valid partitions allowed after an UNUSED or
4134N/A * EFI_PMBR part
4134N/A */
4134N/A if (noMoreParts) {
4134N/A return (-1);
4134N/A }
4134N/A
1755N/A /*
4134N/A * EFI_PMBR partitions must be the only partition
4134N/A * and must be Table entry 0
4134N/A */
4134N/A if (Table[i].systid == EFI_PMBR) {
4134N/A if (i == 0) {
4134N/A noMoreParts = 1;
4134N/A } else {
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A if (Table[i].relsect != 1) {
4134N/A (void) fprintf(stderr, "ERROR: "
1079N/A "Invalid starting sector "
4134N/A "for EFI_PMBR partition:\n"
4134N/A "relsect %d "
4134N/A "(should be 1)\n",
4134N/A Table[i].relsect);
4134N/A
4134N/A return (-1);
4134N/A }
0N/A
0N/A if (Table[i].numsect != dev_capacity - 1) {
4134N/A (void) fprintf(stderr, "ERROR: "
0N/A "EFI_PMBR partition must "
1689N/A "encompass the entire "
0N/A "disk.\n numsect %d - "
0N/A "actual %llu\n",
4134N/A Table[i].numsect,
4134N/A dev_capacity - 1);
4134N/A
4134N/A return (-1);
4134N/A }
4134N/A }
4134N/A
4134N/A /* make sure the partition isn't larger than the disk */
4134N/A rsect = lel(Table[i].relsect);
4134N/A numsect = lel(Table[i].numsect);
4134N/A
4134N/A if ((((diskaddr_t)rsect + numsect) > dev_capacity) ||
4134N/A (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) {
4134N/A if (!skip_verify[i])
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A for (j = i + 1; j < FD_NUMPART; j++) {
4134N/A if (Table[j].systid != UNUSED) {
4134N/A uint32_t t_relsect =
4134N/A lel(Table[j].relsect);
4134N/A uint32_t t_numsect =
0N/A lel(Table[j].numsect);
0N/A
0N/A if (noMoreParts) {
0N/A (void) fprintf(stderr,
0N/A "Cannot add partition to "
0N/A "table; no more partitions "
0N/A "allowed\n");
0N/A
0N/A if (io_debug) {
868N/A (void) fprintf(stderr,
868N/A "DEBUG: Current "
1400N/A "partition:\t"
868N/A "%d:%d:%d:%d:%d:"
0N/A "%d:%d:%d:%d:%d\n"
0N/A " Next "
4134N/A "partition:\t\t"
4134N/A "%d:%d:%d:%d:%d:"
4134N/A "%d:%d:%d:%d:%d\n",
1886N/A Table[i].systid,
4134N/A Table[i].bootid,
4134N/A Table[i].begcyl,
0N/A Table[i].beghead,
0N/A Table[i].begsect,
0N/A Table[i].endcyl,
0N/A Table[i].endhead,
4134N/A Table[i].endsect,
4134N/A Table[i].relsect,
4134N/A Table[i].numsect,
2086N/A Table[j].systid,
0N/A Table[j].bootid,
0N/A Table[j].begcyl,
0N/A Table[j].beghead,
0N/A Table[j].begsect,
0N/A Table[j].endcyl,
0N/A Table[j].endhead,
0N/A Table[j].endsect,
0N/A Table[j].relsect,
0N/A Table[j].numsect);
0N/A }
0N/A
4134N/A return (-1);
4134N/A }
4134N/A if ((rsect >=
4134N/A (t_relsect + t_numsect)) ||
4134N/A ((rsect + numsect) <= t_relsect)) {
4134N/A continue;
4134N/A } else {
4134N/A (void) fprintf(stderr, "ERROR: "
4134N/A "current partition overlaps"
4134N/A " following partition\n");
0N/A
0N/A return (-1);
4134N/A }
0N/A }
4134N/A }
4134N/A } else {
1755N/A noMoreParts = 1;
1755N/A }
1755N/A }
4134N/A if (Table[i].systid != UNUSED) {
4134N/A if (noMoreParts)
4134N/A return (-1);
4134N/A if (!skip_verify[i] &&
1755N/A ((((diskaddr_t)lel(Table[i].relsect) +
4134N/A lel(Table[i].numsect)) > dev_capacity) ||
1755N/A (((diskaddr_t)lel(Table[i].relsect) +
1755N/A lel(Table[i].numsect)) > DK_MAX_2TB))) {
1755N/A return (-1);
4134N/A }
4134N/A }
1842N/A
4134N/A return (numParts);
4134N/A}
4134N/A
4134N/A/*
0N/A * pars_fdisk
0N/A * Parse user-supplied data to set up fdisk partitions
0N/A * (-A, -D, -F).
0N/A */
0N/Astatic int
0N/Apars_fdisk(
0N/A char *line,
0N/A int *id, int *act,
868N/A int *bhead, int *bsect, int *bcyl,
868N/A int *ehead, int *esect, int *ecyl,
1400N/A uint32_t *rsect, uint32_t *numsect)
868N/A{
0N/A int i;
0N/A if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
4134N/A return (1);
4134N/A line[strlen(line)] = '\0';
4134N/A for (i = 0; i < strlen(line); i++) {
1886N/A if (line[i] == '\0') {
4134N/A break;
4134N/A } else if (line[i] == ':') {
0N/A line[i] = ' ';
0N/A }
0N/A }
0N/A if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u",
0N/A id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
0N/A rsect, numsect) != 10) {
0N/A (void) fprintf(stderr, "Syntax error:\n \"%s\".\n", line);
0N/A exit(1);
0N/A }
0N/A return (0);
4134N/A}
4134N/A
4134N/A/*
4134N/A * validate_part
4134N/A * Validate that a new partition does not start at sector 0. Only UNUSED
4134N/A * partitions and previously existing partitions are allowed to start at 0.
4134N/A */
4134N/Astatic int
4134N/Avalidate_part(int id, uint32_t rsect, uint32_t numsect)
0N/A{
0N/A int i;
4134N/A if ((id != UNUSED) && (rsect == 0)) {
0N/A for (i = 0; i < FD_NUMPART; i++) {
4134N/A if ((Old_Table[i].systid == id) &&
4134N/A (Old_Table[i].relsect == lel(rsect)) &&
1755N/A (Old_Table[i].numsect == lel(numsect)))
1755N/A return (0);
1755N/A }
4134N/A (void) fprintf(stderr,
4134N/A "New partition cannot start at sector 0\n");
4134N/A return (-1);
4134N/A }
1755N/A return (0);
4134N/A}
1755N/A
1755N/A/*
1755N/A * stage0
4134N/A * Print out interactive menu and process user input.
4134N/A */
1689N/Astatic void
4134N/Astage0(void)
4134N/A{
4134N/A dispmenu();
0N/A for (;;) {
0N/A (void) printf(Q_LINE);
0N/A (void) printf("Enter Selection: ");
0N/A (void) fgets(s, sizeof (s), stdin);
0N/A rm_blanks(s);
0N/A while (!((s[0] > '0') && (s[0] < '7') &&
0N/A ((s[1] == '\0') || (s[1] == '\n')))) {
0N/A (void) printf(E_LINE); /* Clear any previous error */
868N/A (void) printf(
868N/A "Enter a one-digit number between 1 and 6.");
1400N/A (void) printf(Q_LINE);
868N/A (void) printf("Enter Selection: ");
0N/A (void) fgets(s, sizeof (s), stdin);
0N/A rm_blanks(s);
4134N/A }
4134N/A (void) printf(E_LINE);
4134N/A switch (s[0]) {
1886N/A case '1':
4134N/A if (pcreate() == -1)
4134N/A return;
0N/A break;
0N/A case '2':
0N/A if (pchange() == -1)
0N/A return;
0N/A break;
0N/A case '3':
0N/A if (pdelete() == -1)
0N/A return;
0N/A break;
0N/A case '4':
4134N/A if (ppartid() == -1)
4134N/A return;
4134N/A break;
4134N/A case '5':
4134N/A /* update disk partition table, if changed */
4134N/A if (TableChanged() == 1) {
4134N/A copy_Table_to_Bootblk();
4134N/A dev_mboot_write(0, Bootsect, sectsiz);
4134N/A }
4134N/A /*
0N/A * If the VTOC table is wrong fix it
0N/A * (truncate only)
4134N/A */
0N/A if (io_adjt) {
4134N/A fix_slice();
4134N/A }
4134N/A (void) close(Dev);
4134N/A exit(0);
0N/A /* FALLTHRU */
0N/A case '6':
4134N/A /*
4134N/A * If the VTOC table is wrong fix it
4134N/A * (truncate only)
2086N/A */
2086N/A if (io_adjt) {
0N/A fix_slice();
0N/A }
0N/A (void) close(Dev);
0N/A exit(0);
0N/A /* FALLTHRU */
0N/A default:
0N/A break;
0N/A }
4134N/A copy_Table_to_Bootblk();
1864N/A disptbl();
4134N/A dispmenu();
4134N/A }
4134N/A}
0N/A
0N/A/*
0N/A * pcreate
0N/A * Create partition entry in the table (interactive mode).
0N/A */
0N/Astatic int
0N/Apcreate(void)
0N/A{
868N/A uchar_t tsystid = 'z';
868N/A int i, j;
1400N/A uint32_t numsect;
868N/A int retCode = 0;
0N/A
0N/A i = 0;
4134N/A for (;;) {
4134N/A if (i == FD_NUMPART) {
4134N/A (void) printf(E_LINE);
1886N/A (void) printf(
4134N/A "The partition table is full!\n"
4134N/A "You must delete a partition before creating"
0N/A " a new one.\n");
0N/A return (-1);
0N/A }
0N/A if (Table[i].systid == UNUSED) {
0N/A break;
0N/A }
0N/A i++;
0N/A }
0N/A
0N/A numsect = 0;
4134N/A for (i = 0; i < FD_NUMPART; i++) {
4134N/A if (Table[i].systid != UNUSED) {
4134N/A numsect += lel(Table[i].numsect);
4134N/A }
4134N/A if (numsect >= chs_capacity) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf("There is no more room on the disk for"
4134N/A " another partition.\n");
4134N/A (void) printf(
0N/A "You must delete a partition before creating"
0N/A " a new one.\n");
4134N/A return (-1);
0N/A }
4134N/A }
4134N/A while (tsystid == 'z') {
1755N/A
1755N/A /*
1755N/A * The question here is expanding to more than what is
4134N/A * allocated for question lines (Q_LINE) which garbles
4134N/A * at least warning line. Clearing warning line as workaround
4134N/A * for now.
4134N/A */
1755N/A
4134N/A (void) printf(W_LINE);
1755N/A (void) printf(Q_LINE);
1755N/A (void) printf(
1755N/A "Select the partition type to create:\n"
4134N/A " 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n"
4134N/A " 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n"
1689N/A " 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n"
4134N/A " D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? ");
4134N/A (void) fgets(s, sizeof (s), stdin);
4134N/A rm_blanks(s);
0N/A if ((s[1] != '\0') && (s[1] != '\n')) {
0N/A (void) printf(E_LINE);
0N/A (void) printf("Invalid selection, try again.");
0N/A continue;
0N/A }
0N/A switch (s[0]) {
0N/A case '0': /* exit */
0N/A (void) printf(E_LINE);
868N/A return (-1);
868N/A case '1': /* Solaris partition */
1400N/A tsystid = SUNIXOS2;
868N/A break;
0N/A case '2': /* UNIX partition */
0N/A tsystid = UNIXOS;
4134N/A break;
4134N/A case '3': /* PCIXOS partition */
4134N/A tsystid = PCIXOS;
1886N/A break;
4134N/A case '4': /* OTHEROS System partition */
4134N/A tsystid = OTHEROS;
0N/A break;
0N/A case '5':
0N/A tsystid = DOSOS12; /* DOS 12 bit fat */
0N/A break;
0N/A case '6':
0N/A tsystid = DOSOS16; /* DOS 16 bit fat */
0N/A break;
0N/A case '7':
0N/A tsystid = EXTDOS;
0N/A break;
4134N/A case '8':
4134N/A tsystid = DOSHUGE;
4134N/A break;
4134N/A case '9':
4134N/A tsystid = FDISK_FAT95; /* FAT16, need extended int13 */
4134N/A break;
4134N/A case 'a': /* x86 Boot partition */
4134N/A case 'A':
4134N/A tsystid = X86BOOT;
4134N/A break;
0N/A case 'b': /* Diagnostic boot partition */
0N/A case 'B':
4134N/A tsystid = DIAGPART;
0N/A break;
4134N/A case 'c': /* FAT32 */
4134N/A case 'C':
1755N/A tsystid = FDISK_WINDOWS;
1755N/A break;
1755N/A case 'd': /* FAT32 and need extended int13 */
4134N/A case 'D':
4134N/A tsystid = FDISK_EXT_WIN;
4134N/A break;
4134N/A case 'e': /* Extended partition, need extended int13 */
1755N/A case 'E':
4134N/A tsystid = FDISK_EXTLBA;
1755N/A break;
1755N/A case 'f':
1755N/A case 'F':
0N/A tsystid = EFI_PMBR;
4134N/A break;
1858N/A default:
4134N/A (void) printf(E_LINE);
4134N/A (void) printf("Invalid selection, try again.");
4134N/A continue;
4134N/A }
0N/A }
0N/A
0N/A (void) printf(E_LINE);
0N/A
0N/A if (tsystid != EFI_PMBR) {
0N/A (void) printf(W_LINE);
0N/A if ((dev_capacity > DK_MAX_2TB))
0N/A (void) printf("WARNING: Disk is larger than 2 TB. "
868N/A "Upper limit is 2 TB for non-EFI partition ID\n");
868N/A
1400N/A /* create the new partition */
868N/A i = specify(tsystid);
0N/A
0N/A if (i != -1) {
4134N/A /* see if it should be the active partition */
4134N/A (void) printf(E_LINE);
4134N/A (void) printf(Q_LINE);
1886N/A
4134N/A (void) printf(
4134N/A "Should this become the active partition? If "
0N/A "yes, it will be activated\n"
0N/A "each time the computer is reset or turned on.\n"
0N/A "Please type \"y\" or \"n\". ");
0N/A
0N/A if (yesno()) {
0N/A (void) printf(E_LINE);
0N/A for (j = 0; j < FD_NUMPART; j++) {
0N/A if (j == i) {
0N/A Table[j].bootid = ACTIVE;
0N/A (void) printf(E_LINE);
4134N/A (void) printf(
4134N/A "Partition %d is now "
4134N/A "the active partition.",
4134N/A j + 1);
4134N/A } else {
4134N/A Table[j].bootid = 0;
4134N/A }
4134N/A }
4134N/A } else {
0N/A Table[i].bootid = 0;
0N/A }
4134N/A
0N/A /* set up the return code */
4134N/A i = 1;
4134N/A }
1755N/A } else {
1755N/A /*
1755N/A * partitions of type EFI_PMBR must be the only partitions in
4134N/A * the table
4134N/A *
4134N/A * First, make sure there were no errors the table is
4134N/A * empty
1755N/A */
4134N/A retCode = verify_tbl();
1755N/A
1755N/A if (retCode < 0) {
1755N/A (void) fprintf(stderr,
4134N/A "fdisk: Cannot create EFI partition table; \n"
4134N/A "current partition table is invalid.\n");
1689N/A return (-1);
4134N/A } else if (retCode > 0) {
4134N/A (void) printf(
4134N/A "An EFI partition must be the only partition on "
4134N/A "disk. You may manually delete existing\n"
4134N/A "partitions, or fdisk can do it.\n"
4134N/A "Do you want fdisk to destroy existing "
0N/A "partitions?\n"
0N/A "Please type \"y\" or \"n\". ");
0N/A
0N/A if (yesno()) {
0N/A nulltbl();
0N/A } else {
0N/A return (-1);
0N/A }
868N/A }
868N/A
1400N/A /* create the table entry - i should be 0 */
868N/A i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1,
0N/A (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
0N/A (dev_capacity - 1));
4134N/A
4134N/A if (i != 0) {
4134N/A (void) printf("Error creating EFI partition!!!\n");
1886N/A i = -1;
4134N/A } else {
4134N/A
0N/A /* EFI partitions are currently never active */
0N/A Table[i].bootid = 0;
0N/A
0N/A /* set up the return code */
0N/A i = 1;
0N/A }
0N/A }
0N/A
0N/A return (i);
0N/A}
4134N/A
4134N/A/*
4134N/A * specify
4134N/A * Query the user to specify the size of the new partition in
4134N/A * terms of percentage of the disk or by specifying the starting
4134N/A * cylinder and length in cylinders.
4134N/A */
4134N/Astatic int
4134N/Aspecify(uchar_t tsystid)
0N/A{
0N/A int i, j, percent = -1;
4134N/A int cyl, cylen;
0N/A diskaddr_t first_free, size_free;
1859N/A diskaddr_t max_free;
4134N/A int cyl_size;
4134N/A struct ipart *partition[FD_NUMPART];
0N/A
0N/A cyl_size = heads * sectors;
0N/A
0N/A /*
0N/A * make a local copy of the partition table
0N/A * and sort it into relsect order
0N/A */
0N/A for (i = 0; i < FD_NUMPART; i++)
0N/A partition[i] = &Table[i];
0N/A
629N/A for (i = 0; i < FD_NUMPART - 1; i++) {
629N/A if (partition[i]->systid == UNUSED)
4134N/A break;
629N/A for (j = i + 1; j < FD_NUMPART; j++) {
629N/A if (partition[j]->systid == UNUSED)
629N/A break;
629N/A if (lel(partition[j]->relsect) <
629N/A lel(partition[i]->relsect)) {
629N/A struct ipart *temp = partition[i];
629N/A partition[i] = partition[j];
629N/A partition[j] = temp;
629N/A }
629N/A }
629N/A }
629N/A
629N/A
629N/A (void) printf(Q_LINE);
629N/A (void) printf(
629N/A "Specify the percentage of disk to use for this partition\n"
629N/A "(or type \"c\" to specify the size in cylinders). ");
629N/A (void) fgets(s, sizeof (s), stdin);
629N/A rm_blanks(s);
629N/A if (s[0] != 'c') { /* Specify size in percentage of disk */
629N/A i = 0;
629N/A while ((s[i] != '\0') && (s[i] != '\n')) {
629N/A if (s[i] < '0' || s[i] > '9') {
629N/A (void) printf(E_LINE);
629N/A (void) printf("Invalid percentage value "
629N/A "specified; retry the operation.");
4134N/A return (-1);
629N/A }
4134N/A i++;
629N/A if (i > 3) {
629N/A (void) printf(E_LINE);
629N/A (void) printf("Invalid percentage value "
629N/A "specified; retry the operation.");
629N/A return (-1);
629N/A }
629N/A }
629N/A if ((percent = atoi(s)) > 100) {
629N/A (void) printf(E_LINE);
629N/A (void) printf(
629N/A "Percentage value is too large. The value must be"
629N/A " between 1 and 100;\nretry the operation.\n");
629N/A return (-1);
629N/A }
629N/A if (percent < 1) {
629N/A (void) printf(E_LINE);
4134N/A (void) printf(
4134N/A "Percentage value is too small. The value must be"
0N/A " between 1 and 100;\nretry the operation.\n");
4134N/A return (-1);
4134N/A }
0N/A
4134N/A if (percent == 100)
0N/A cylen = Numcyl_usable - 1;
0N/A else
0N/A cylen = (Numcyl_usable * percent) / 100;
0N/A
0N/A /* Verify DOS12 partition doesn't exceed max size of 32MB. */
0N/A if ((tsystid == DOSOS12) &&
0N/A ((long)((long)cylen * cyl_size) > MAXDOS)) {
0N/A int n;
0N/A n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable;
0N/A (void) printf(E_LINE);
0N/A (void) printf("Maximum size for a DOS partition "
0N/A "is %d%%; retry the operation.",
0N/A n <= 100 ? n : 100);
0N/A return (-1);
0N/A }
4134N/A
4134N/A
4134N/A max_free = 0;
4134N/A for (i = 0; i < FD_NUMPART; i++) {
0N/A
4134N/A /*
4134N/A * check for free space before partition i
4134N/A * where i varies from 0 to 3
4134N/A *
4134N/A * freespace after partition 3 is unusable
0N/A * because there are no free partitions
4134N/A *
0N/A * freespace begins at the end of previous partition
4134N/A * or cylinder 1
0N/A */
4134N/A if (i) {
4134N/A /* Not an empty table */
0N/A first_free = lel(partition[i - 1]->relsect) +
0N/A lel(partition[i - 1]->numsect);
4134N/A } else {
4134N/A first_free = cyl_size;
4134N/A }
0N/A
2086N/A /*
0N/A * freespace ends before the current partition
0N/A * or the end of the disk (chs end)
4134N/A */
0N/A if (partition[i]->systid == UNUSED) {
4134N/A size_free = chs_capacity - first_free;
4134N/A } else {
4134N/A /*
4134N/A * Partition might start before cylinder 1.
4134N/A * Make sure free space is not negative.
4134N/A */
4134N/A size_free =
0N/A (lel(partition[i]->relsect > first_free)) ?
4134N/A (lel(partition[i]->relsect) - first_free) :
0N/A 0;
4134N/A }
4134N/A
4134N/A /* save largest free space */
0N/A if (max_free < size_free)
0N/A max_free = size_free;
0N/A
0N/A if (((uint64_t)cylen * cyl_size) <= size_free) {
0N/A /* We found a place to use */
0N/A break;
0N/A }
4134N/A if (partition[i]->systid == UNUSED) {
4134N/A (void) printf(E_LINE);
4134N/A max_free /= (cyl_size);
4134N/A (void) fprintf(stderr, "fdisk: "
0N/A "Maximum percentage available is %lld\n",
4134N/A 100 * max_free / Numcyl_usable);
4134N/A return (-1);
4134N/A }
4134N/A }
4134N/A
309N/A (void) printf(E_LINE);
309N/A if (i >= FD_NUMPART) {
4134N/A (void) fprintf(stderr,
824N/A "fdisk: Partition table is full.\n");
4134N/A return (-1);
867N/A }
1963N/A
1963N/A if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
1963N/A first_free, cylen * cyl_size)) >= 0) {
1963N/A return (i);
1963N/A }
4134N/A return (-1);
4134N/A } else {
1963N/A
4134N/A /* Specifying size in cylinders */
4134N/A (void) printf(E_LINE);
1963N/A (void) printf(Q_LINE);
4134N/A (void) printf("Enter starting cylinder number: ");
4134N/A if ((cyl = getcyl()) == -1) {
1963N/A (void) printf(E_LINE);
3853N/A (void) printf("Invalid number; retry the operation.");
1963N/A return (-1);
1963N/A }
4134N/A if (cyl == 0) {
4134N/A (void) printf(E_LINE);
1963N/A (void) printf(
1963N/A "New partition cannot start at cylinder 0.\n");
1963N/A return (-1);
1963N/A }
1963N/A
1963N/A
1963N/A if (cyl >= Numcyl_usable) {
1963N/A (void) printf(E_LINE);
1963N/A (void) printf(
3999N/A "Cylinder %d is out of bounds, "
4134N/A "the maximum is %d.\n",
4134N/A cyl, Numcyl_usable - 1);
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A (void) printf(Q_LINE);
4134N/A (void) printf("Enter partition size in cylinders: ");
4134N/A if ((cylen = getcyl()) == -1) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf("Invalid number, retry the operation.");
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A for (i = 0; i < FD_NUMPART; i++) {
4134N/A uint32_t t_relsect, t_numsect;
4134N/A
4134N/A if (partition[i]->systid == UNUSED)
4134N/A break;
4134N/A t_relsect = lel(partition[i]->relsect);
4134N/A t_numsect = lel(partition[i]->numsect);
4134N/A
4134N/A if (cyl * cyl_size >= t_relsect &&
4134N/A cyl * cyl_size < t_relsect + t_numsect) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf(
4134N/A "Cylinder %d is already allocated"
4134N/A "\nretry the operation.",
4134N/A cyl);
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A if (cyl * cyl_size < t_relsect &&
4134N/A (cyl + cylen - 1) * cyl_size > t_relsect) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf(
4134N/A "Maximum size for partition is %u cylinders"
4134N/A "\nretry the operation.",
4134N/A (t_relsect - cyl * cyl_size) / cyl_size);
4134N/A return (-1);
4134N/A }
4134N/A }
4134N/A
4134N/A /* Verify partition doesn't exceed disk size or 2 TB */
4134N/A if (cyl + cylen > Numcyl_usable) {
4134N/A (void) printf(E_LINE);
4134N/A if (Numcyl > Numcyl_usable) {
4134N/A (void) printf(
4134N/A "Maximum size for partition is %d "
4134N/A "cylinders; \nretry the operation.",
4134N/A Numcyl_usable - cyl);
4134N/A } else {
4134N/A (void) printf(
4134N/A "Maximum size for partition is %d "
4134N/A "cylinders; \nretry the operation.",
4134N/A Numcyl_usable - cyl);
4134N/A }
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A /* Verify DOS12 partition doesn't exceed max size of 32MB. */
4134N/A if ((tsystid == DOSOS12) &&
4134N/A ((long)((long)cylen * cyl_size) > MAXDOS)) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf(
4134N/A "Maximum size for a %s partition is %ld cylinders;"
4134N/A "\nretry the operation.",
4134N/A Dstr, MAXDOS / (int)(cyl_size));
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A (void) printf(E_LINE);
4134N/A i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
4134N/A cyl * cyl_size, cylen * cyl_size);
4134N/A if (i < 0)
4134N/A return (-1);
4134N/A
4134N/A if (verify_tbl() < 0) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf("fdisk: Cannot create partition table\n");
4134N/A return (-1);
4134N/A }
4134N/A
4134N/A return (i);
4134N/A }
4134N/A}
4134N/A
4134N/A/*
4134N/A * dispmenu
4134N/A * Display command menu (interactive mode).
4134N/A */
4134N/Astatic void
4134N/Adispmenu(void)
4134N/A{
4134N/A (void) printf(M_LINE);
4134N/A (void) printf(
4134N/A "SELECT ONE OF THE FOLLOWING:\n"
4134N/A " 1. Create a partition\n"
4134N/A " 2. Specify the active partition\n"
4134N/A " 3. Delete a partition\n"
4134N/A " 4. Change between Solaris and Solaris2 Partition IDs\n"
4170N/A " 5. Exit (update disk configuration and exit)\n"
4170N/A " 6. Cancel (exit without updating disk configuration)\n");
4170N/A}
4170N/A
4170N/A/*
4170N/A * pchange
4170N/A * Change the ACTIVE designation of a partition.
4170N/A */
4170N/Astatic int
4170N/Apchange(void)
4170N/A{
4170N/A char s[80];
4170N/A int i, j;
4134N/A
4134N/A for (;;) {
4134N/A (void) printf(Q_LINE);
4134N/A {
4134N/A (void) printf(
4134N/A "Specify the partition number to boot from"
4134N/A " (or specify 0 for none): ");
4134N/A }
4134N/A (void) fgets(s, sizeof (s), stdin);
4134N/A rm_blanks(s);
4134N/A if (((s[1] != '\0') && (s[1] != '\n')) ||
4134N/A (s[0] < '0') || (s[0] > '4')) {
4134N/A (void) printf(E_LINE);
4170N/A (void) printf(
4170N/A "Invalid response, please specify a number"
4170N/A " between 0 and 4.\n");
4170N/A } else {
4170N/A break;
4170N/A }
4170N/A }
4170N/A if (s[0] == '0') { /* No active partitions */
4170N/A for (i = 0; i < FD_NUMPART; i++) {
4170N/A if (Table[i].systid != UNUSED &&
4170N/A Table[i].bootid == ACTIVE)
4170N/A Table[i].bootid = 0;
4170N/A }
4170N/A (void) printf(E_LINE);
4170N/A (void) printf(
4170N/A "No partition is currently marked as active.");
4170N/A return (0);
4134N/A } else { /* User has selected a partition to be active */
4134N/A i = s[0] - '1';
4134N/A if (Table[i].systid == UNUSED) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf("Partition does not exist.");
4134N/A return (-1);
4134N/A }
4134N/A /* a DOS-DATA or EXT-DOS partition cannot be active */
4134N/A else if ((Table[i].systid == DOSDATA) ||
4134N/A (Table[i].systid == EXTDOS) ||
4134N/A (Table[i].systid == FDISK_EXTLBA)) {
4134N/A (void) printf(E_LINE);
4134N/A (void) printf(
3999N/A "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
0N/A "cannot be made active.\n");
(void) printf("Select another partition.");
return (-1);
}
Table[i].bootid = ACTIVE;
for (j = 0; j < FD_NUMPART; j++) {
if (j != i)
Table[j].bootid = 0;
}
}
(void) printf(E_LINE);
{
(void) printf(
"Partition %d is now active. The system will start up"
" from this\n", i + 1);
(void) printf("partition after the next reboot.");
}
return (1);
}
/*
* Change between SOLARIS and SOLARIS2 partition id
*/
static int
ppartid(void)
{
char *p, s[80];
int i;
for (;;) {
(void) printf(Q_LINE);
(void) printf("Specify the partition number to change"
" (or enter 0 to exit): ");
if (!fgets(s, sizeof (s), stdin))
return (1);
i = strtol(s, &p, 10);
if (*p != '\n' || i < 0 || i > FD_NUMPART) {
(void) printf(E_LINE);
(void) printf(
"Invalid response, retry the operation.\n");
continue;
}
if (i == 0) {
/* exit delete command */
(void) printf(E_LINE); /* clear error message */
return (1);
}
i -= 1;
if (Table[i].systid == SUNIXOS) {
Table[i].systid = SUNIXOS2;
} else if (Table[i].systid == SUNIXOS2) {
Table[i].systid = SUNIXOS;
} else {
(void) printf(E_LINE);
(void) printf(
"Partition %d is not a Solaris partition.",
i + 1);
continue;
}
(void) printf(E_LINE);
(void) printf("Partition %d has been changed.", i + 1);
return (1);
}
}
/*
* pdelete
* Remove partition entry from the table (interactive mode).
*/
static char
pdelete(void)
{
char s[80];
int i, j;
char pactive;
DEL1: (void) printf(Q_LINE);
(void) printf("Specify the partition number to delete"
" (or enter 0 to exit): ");
(void) fgets(s, sizeof (s), stdin);
rm_blanks(s);
if ((s[0] == '0')) { /* exit delete command */
(void) printf(E_LINE); /* clear error message */
return (1);
}
/* Accept only a single digit between 1 and 4 */
if (((s[1] != '\0') && (s[1] != '\n')) ||
(i = atoi(s)) < 1 || i > FD_NUMPART) {
(void) printf(E_LINE);
(void) printf("Invalid response, retry the operation.\n");
goto DEL1;
} else { /* Found a digit between 1 and 4 */
--i; /* Structure begins with element 0 */
}
if (Table[i].systid == UNUSED) {
(void) printf(E_LINE);
(void) printf("Partition %d does not exist.", i + 1);
return (-1);
}
(void) printf(Q_LINE);
(void) printf("Are you sure you want to delete partition %d?"
" This will make all files and \n", i + 1);
(void) printf("programs in this partition inaccessible (type"
" \"y\" or \"n\"). ");
(void) printf(E_LINE);
if (! yesno()) {
return (1);
}
if (Table[i].bootid == ACTIVE) {
pactive = 1;
} else {
pactive = 0;
}
for (j = i; j < FD_NUMPART - 1; j++) {
Table[j] = Table[j + 1];
}
Table[j].systid = UNUSED;
Table[j].numsect = 0;
Table[j].relsect = 0;
Table[j].bootid = 0;
(void) printf(E_LINE);
(void) printf("Partition %d has been deleted.", i + 1);
if (pactive) {
(void) printf(" This was the active partition.");
}
return (1);
}
/*
* rm_blanks
* Remove blanks from strings of user responses.
*/
static void
rm_blanks(char *s)
{
register int i, j;
for (i = 0; i < CBUFLEN; i++) {
if ((s[i] == ' ') || (s[i] == '\t'))
continue;
else
/* Found first non-blank character of the string */
break;
}
for (j = 0; i < CBUFLEN; j++, i++) {
if ((s[j] = s[i]) == '\0') {
/* Reached end of string */
return;
}
}
}
/*
* getcyl
* Take the user-specified cylinder number and convert it from a
* string to a decimal value.
*/
static int
getcyl(void)
{
int slen, i, j;
unsigned int cyl;
(void) fgets(s, sizeof (s), stdin);
rm_blanks(s);
slen = strlen(s);
if (s[slen - 1] == '\n')
slen--;
j = 1;
cyl = 0;
for (i = slen - 1; i >= 0; i--) {
if (s[i] < '0' || s[i] > '9') {
return (-1);
}
cyl += (j * (s[i] - '0'));
j *= 10;
}
return (cyl);
}
/*
* disptbl
* Display the current fdisk table; determine percentage
* of the disk used for each partition.
*/
static void
disptbl(void)
{
int i;
unsigned int startcyl, endcyl, length, percent, remainder;
char *stat, *type;
int is_pmbr = 0;
if ((heads == 0) || (sectors == 0)) {
(void) printf("WARNING: critical disk geometry information"
" missing!\n");
(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
exit(1);
}
(void) printf(HOME);
(void) printf(T_LINE);
(void) printf(" Total disk size is %d cylinders\n", Numcyl);
(void) printf(" Cylinder size is %d (%d byte) blocks\n\n",
heads * sectors, sectsiz);
(void) printf(
" Cylinders\n");
(void) printf(
" Partition Status Type Start End Length"
" %%\n");
(void) printf(
" ========= ====== ============ ===== === ======"
" ===");
for (i = 0; i < FD_NUMPART; i++) {
if (Table[i].systid == UNUSED) {
(void) printf("\n");
(void) printf(CLR_LIN);
continue;
}
if (Table[i].bootid == ACTIVE)
stat = Actvstr;
else
stat = NAstr;
switch (Table[i].systid) {
case UNIXOS:
type = Ustr;
break;
case SUNIXOS:
type = SUstr;
break;
case SUNIXOS2:
type = SU2str;
break;
case X86BOOT:
type = X86str;
break;
case DOSOS12:
type = Dstr;
break;
case DOSOS16:
type = D16str;
break;
case EXTDOS:
type = EDstr;
break;
case DOSDATA:
type = DDstr;
break;
case DOSHUGE:
type = DBstr;
break;
case PCIXOS:
type = PCstr;
break;
case DIAGPART:
type = DIAGstr;
break;
case FDISK_IFS:
type = IFSstr;
break;
case FDISK_AIXBOOT:
type = AIXstr;
break;
case FDISK_AIXDATA:
type = AIXDstr;
break;
case FDISK_OS2BOOT:
type = OS2str;
break;
case FDISK_WINDOWS:
type = WINstr;
break;
case FDISK_EXT_WIN:
type = EWINstr;
break;
case FDISK_FAT95:
type = FAT95str;
break;
case FDISK_EXTLBA:
type = EXTLstr;
break;
case FDISK_LINUX:
type = LINUXstr;
break;
case FDISK_CPM:
type = CPMstr;
break;
case FDISK_NOVELL3:
type = NOVstr;
break;
case FDISK_QNX4:
type = QNXstr;
break;
case FDISK_QNX42:
type = QNX2str;
break;
case FDISK_QNX43:
type = QNX3str;
break;
case FDISK_LINUXNAT:
type = LINNATstr;
break;
case FDISK_NTFSVOL1:
type = NTFSVOL1str;
break;
case FDISK_NTFSVOL2:
type = NTFSVOL2str;
break;
case FDISK_BSD:
type = BSDstr;
break;
case FDISK_NEXTSTEP:
type = NEXTSTEPstr;
break;
case FDISK_BSDIFS:
type = BSDIFSstr;
break;
case FDISK_BSDISWAP:
type = BSDISWAPstr;
break;
case EFI_PMBR:
type = EFIstr;
if (lel(Table[i].numsect) == DK_MAX_2TB)
is_pmbr = 1;
break;
default:
type = Ostr;
break;
}
startcyl = lel(Table[i].relsect) /
(unsigned long)(heads * sectors);
if (lel(Table[i].numsect) == DK_MAX_2TB) {
endcyl = Numcyl - 1;
length = endcyl - startcyl + 1;
} else {
length = lel(Table[i].numsect) /
(unsigned long)(heads * sectors);
if (lel(Table[i].numsect) %
(unsigned long)(heads * sectors))
length++;
endcyl = startcyl + length - 1;
}
percent = length * 100 / Numcyl_usable;
if ((remainder = (length * 100 % Numcyl_usable)) != 0) {
if ((remainder * 100 / Numcyl_usable) > 50) {
/* round up */
percent++;
}
/* Else leave the percent as is since it's already */
/* rounded down */
}
if (percent > 100)
percent = 100;
(void) printf(
"\n %d %s %-12.12s %4d %4d %4d"
" %3d",
i + 1, stat, type, startcyl, endcyl, length, percent);
}
/* Print warning message if table is empty */
if (Table[0].systid == UNUSED) {
(void) printf(W_LINE);
(void) printf("WARNING: no partitions are defined!");
} else {
/* Clear the warning line */
(void) printf(W_LINE);
/* Print warning if disk > 2TB and is not EFI PMBR */
if (!is_pmbr && (dev_capacity > DK_MAX_2TB))
(void) printf("WARNING: Disk is larger than 2 TB. "
"Upper limit is 2 TB for non-EFI partition ID\n");
}
}
/*
* print_Table
* Write the detailed fdisk table to standard error for
* the selected disk device.
*/
static void
print_Table(void)
{
int i;
(void) fprintf(stderr,
" SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT"
" NUMSECT\n");
for (i = 0; i < FD_NUMPART; i++) {
(void) fprintf(stderr, " %-5d ", Table[i].systid);
(void) fprintf(stderr, "%-3d ", Table[i].bootid);
(void) fprintf(stderr, "%-5d ", Table[i].beghead);
(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
(void) fprintf(stderr, "%-8d ",
(((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
(void) fprintf(stderr, "%-5d ", Table[i].endhead);
(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
(void) fprintf(stderr, "%-8d ",
(((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
(void) fprintf(stderr, "%-10u ", lel(Table[i].relsect));
(void) fprintf(stderr, "%-10u\n", lel(Table[i].numsect));
}
}
/*
* copy_Table_to_Old_Table
* Copy Table into Old_Table. The function only copies the systid,
* numsect, relsect, and bootid values because they are the only
* ones compared when determining if Table has changed.
*/
static void
copy_Table_to_Old_Table(void)
{
int i;
for (i = 0; i < FD_NUMPART; i++) {
(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
}
}
/*
* nulltbl
* Zero out the systid, numsect, relsect, and bootid values in the
* fdisk table.
*/
static void
nulltbl(void)
{
int i;
for (i = 0; i < FD_NUMPART; i++) {
Table[i].systid = UNUSED;
Table[i].numsect = lel(UNUSED);
Table[i].relsect = lel(UNUSED);
Table[i].bootid = 0;
skip_verify[i] = 0;
}
}
/*
* copy_Bootblk_to_Table
* Copy the bytes from the boot record to an internal "Table".
* All unused are padded with zeros starting at offset 446.
*/
static void
copy_Bootblk_to_Table(void)
{
int i, j;
char *bootptr;
struct ipart iparts[FD_NUMPART];
/* Get an aligned copy of the partition tables */
(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
bootptr = (char *)iparts; /* Points to start of partition table */
if (les(Bootblk->signature) != MBB_MAGIC) {
/* Signature is missing */
nulltbl();
(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
return;
}
/*
* When the DOS fdisk command deletes a partition, it is not
* recognized by the old algorithm. The algorithm that
* follows looks at each entry in the Bootrec and copies all
* those that are valid.
*/
j = 0;
for (i = 0; i < FD_NUMPART; i++) {
if (iparts[i].systid == 0) {
/* Null entry */
bootptr += sizeof (struct ipart);
} else {
fill_ipart(bootptr, &Table[j]);
j++;
bootptr += sizeof (struct ipart);
}
}
for (i = j; i < FD_NUMPART; i++) {
Table[i].systid = UNUSED;
Table[i].numsect = lel(UNUSED);
Table[i].relsect = lel(UNUSED);
Table[i].bootid = 0;
}
/* For now, always replace the bootcode with ours */
(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
copy_Table_to_Bootblk();
}
/*
* fill_ipart
* Initialize ipart structure values.
*/
static void
fill_ipart(char *bootptr, struct ipart *partp)
{
#ifdef sparc
/* Packing struct ipart for Sparc */
partp->bootid = getbyte(&bootptr);
partp->beghead = getbyte(&bootptr);
partp->begsect = getbyte(&bootptr);
partp->begcyl = getbyte(&bootptr);
partp->systid = getbyte(&bootptr);
partp->endhead = getbyte(&bootptr);
partp->endsect = getbyte(&bootptr);
partp->endcyl = getbyte(&bootptr);
partp->relsect = (int32_t)getlong(&bootptr);
partp->numsect = (int32_t)getlong(&bootptr);
#else
*partp = *(struct ipart *)bootptr;
#endif
}
/*
* getbyte, getlong
* Get a byte, a short, or a long (SPARC only).
*/
#ifdef sparc
uchar_t
getbyte(char **bp)
{
uchar_t b;
b = (uchar_t)**bp;
*bp = *bp + 1;
return (b);
}
uint32_t
getlong(char **bp)
{
int32_t b, bh, bl;
bh = ((**bp) << 8) | *(*bp + 1);
*bp += 2;
bl = ((**bp) << 8) | *(*bp + 1);
*bp += 2;
b = (bh << 16) | bl;
return ((uint32_t)b);
}
#endif
/*
* copy_Table_to_Bootblk
* Copy the table into the boot record. Note that the unused
* entries will always be the last ones in the table and they are
* marked with 100 in sysind. The the unused portion of the table
* is padded with zeros in the bytes after the used entries.
*/
static void
copy_Table_to_Bootblk(void)
{
struct ipart *boot_ptr, *tbl_ptr;
boot_ptr = (struct ipart *)Bootblk->parts;
tbl_ptr = (struct ipart *)&Table[0].bootid;
for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
tbl_ptr++, boot_ptr++) {
if (tbl_ptr->systid == UNUSED)
(void) memset(boot_ptr, 0, sizeof (struct ipart));
else
(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
}
Bootblk->signature = les(MBB_MAGIC);
}
/*
* TableChanged
* Check for any changes in the partition table.
*/
static int
TableChanged(void)
{
int i, changed;
changed = 0;
for (i = 0; i < FD_NUMPART; i++) {
if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
/* Partition table changed, write back to disk */
changed = 1;
}
}
return (changed);
}
/*
* ffile_write
* Display contents of partition table to standard output or
* another file name without writing it to the disk (-W file).
*/
static void
ffile_write(char *file)
{
register int i;
FILE *fp;
/*
* If file isn't standard output, then it's a file name.
* Open file and write it.
*/
if (file != (char *)stdout) {
if ((fp = fopen(file, "w")) == NULL) {
(void) fprintf(stderr,
"fdisk: Cannot open output file %s.\n",
file);
exit(1);
}
}
else
fp = stdout;
/*
* Write the fdisk table information
*/
(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
(void) fprintf(fp, "* Dimensions:\n");
(void) fprintf(fp, "* %4d bytes/sector\n", sectsiz);
(void) fprintf(fp, "* %4d sectors/track\n", sectors);
(void) fprintf(fp, "* %4d tracks/cylinder\n", heads);
(void) fprintf(fp, "* %4d cylinders\n", Numcyl);
(void) fprintf(fp, "*\n");
/* Write virtual (HBA) geometry, if required */
if (v_flag) {
(void) fprintf(fp, "* HBA Dimensions:\n");
(void) fprintf(fp, "* %4d bytes/sector\n", sectsiz);
(void) fprintf(fp, "* %4d sectors/track\n", hba_sectors);
(void) fprintf(fp, "* %4d tracks/cylinder\n", hba_heads);
(void) fprintf(fp, "* %4d cylinders\n", hba_Numcyl);
(void) fprintf(fp, "*\n");
}
(void) fprintf(fp, "* systid:\n");
(void) fprintf(fp, "* 1: DOSOS12\n");
(void) fprintf(fp, "* 2: PCIXOS\n");
(void) fprintf(fp, "* 4: DOSOS16\n");
(void) fprintf(fp, "* 5: EXTDOS\n");
(void) fprintf(fp, "* 6: DOSBIG\n");
(void) fprintf(fp, "* 7: FDISK_IFS\n");
(void) fprintf(fp, "* 8: FDISK_AIXBOOT\n");
(void) fprintf(fp, "* 9: FDISK_AIXDATA\n");
(void) fprintf(fp, "* 10: FDISK_0S2BOOT\n");
(void) fprintf(fp, "* 11: FDISK_WINDOWS\n");
(void) fprintf(fp, "* 12: FDISK_EXT_WIN\n");
(void) fprintf(fp, "* 14: FDISK_FAT95\n");
(void) fprintf(fp, "* 15: FDISK_EXTLBA\n");
(void) fprintf(fp, "* 18: DIAGPART\n");
(void) fprintf(fp, "* 65: FDISK_LINUX\n");
(void) fprintf(fp, "* 82: FDISK_CPM\n");
(void) fprintf(fp, "* 86: DOSDATA\n");
(void) fprintf(fp, "* 98: OTHEROS\n");
(void) fprintf(fp, "* 99: UNIXOS\n");
(void) fprintf(fp, "* 101: FDISK_NOVELL3\n");
(void) fprintf(fp, "* 119: FDISK_QNX4\n");
(void) fprintf(fp, "* 120: FDISK_QNX42\n");
(void) fprintf(fp, "* 121: FDISK_QNX43\n");
(void) fprintf(fp, "* 130: SUNIXOS\n");
(void) fprintf(fp, "* 131: FDISK_LINUXNAT\n");
(void) fprintf(fp, "* 134: FDISK_NTFSVOL1\n");
(void) fprintf(fp, "* 135: FDISK_NTFSVOL2\n");
(void) fprintf(fp, "* 165: FDISK_BSD\n");
(void) fprintf(fp, "* 167: FDISK_NEXTSTEP\n");
(void) fprintf(fp, "* 183: FDISK_BSDIFS\n");
(void) fprintf(fp, "* 184: FDISK_BSDISWAP\n");
(void) fprintf(fp, "* 190: X86BOOT\n");
(void) fprintf(fp, "* 191: SUNIXOS2\n");
(void) fprintf(fp, "* 238: EFI_PMBR\n");
(void) fprintf(fp, "* 239: EFI_FS\n");
(void) fprintf(fp, "*\n");
(void) fprintf(fp,
"\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl"
" Rsect Numsect\n");
for (i = 0; i < FD_NUMPART; i++) {
if (Table[i].systid != UNUSED)
(void) fprintf(fp,
" %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
" %-10u\n",
Table[i].systid,
Table[i].bootid,
Table[i].beghead,
Table[i].begsect & 0x3f,
((Table[i].begcyl & 0xff) | ((Table[i].begsect &
0xc0) << 2)),
Table[i].endhead,
Table[i].endsect & 0x3f,
((Table[i].endcyl & 0xff) | ((Table[i].endsect &
0xc0) << 2)),
lel(Table[i].relsect),
lel(Table[i].numsect));
}
if (fp != stdout)
(void) fclose(fp);
}
/*
* fix_slice
* Read the VTOC table on the Solaris partition and check that no
* slices exist that extend past the end of the Solaris partition.
* If no Solaris partition exists, nothing is done.
*/
static void
fix_slice(void)
{
int i;
uint32_t numsect;
if (io_image) {
return;
}
for (i = 0; i < FD_NUMPART; i++) {
if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
/*
* Only the size matters (not starting point), since
* VTOC entries are relative to the start of
* the partition.
*/
numsect = lel(Table[i].numsect);
break;
}
}
if (i >= FD_NUMPART) {
if (!io_nifdisk) {
(void) fprintf(stderr,
"fdisk: No Solaris partition found - VTOC not"
" checked.\n");
}
return;
}
if (readvtoc() != VTOC_OK) {
exit(1); /* Failed to read the VTOC */
}
for (i = 0; i < V_NUMPAR; i++) {
/* Special case for slice two (entire disk) */
if (i == 2) {
if (disk_vtoc.v_part[i].p_start != 0) {
(void) fprintf(stderr,
"slice %d starts at %llu, is not at"
" start of partition",
i, disk_vtoc.v_part[i].p_start);
if (!io_nifdisk) {
(void) printf(" adjust ?:");
if (yesno())
disk_vtoc.v_part[i].p_start = 0;
} else {
disk_vtoc.v_part[i].p_start = 0;
(void) fprintf(stderr, " adjusted!\n");
}
}
if (disk_vtoc.v_part[i].p_size != numsect) {
(void) fprintf(stderr,
"slice %d size %llu does not cover"
" complete partition",
i, disk_vtoc.v_part[i].p_size);
if (!io_nifdisk) {
(void) printf(" adjust ?:");
if (yesno())
disk_vtoc.v_part[i].p_size =
numsect;
} else {
disk_vtoc.v_part[i].p_size = numsect;
(void) fprintf(stderr, " adjusted!\n");
}
}
if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
(void) fprintf(stderr,
"slice %d tag was %d should be %d",
i, disk_vtoc.v_part[i].p_tag,
V_BACKUP);
if (!io_nifdisk) {
(void) printf(" fix ?:");
if (yesno())
disk_vtoc.v_part[i].p_tag =
V_BACKUP;
} else {
disk_vtoc.v_part[i].p_tag = V_BACKUP;
(void) fprintf(stderr, " fixed!\n");
}
}
continue;
}
if (io_ADJT) {
if (disk_vtoc.v_part[i].p_start > numsect ||
disk_vtoc.v_part[i].p_start +
disk_vtoc.v_part[i].p_size > numsect) {
(void) fprintf(stderr,
"slice %d (start %llu, end %llu)"
" is larger than the partition",
i, disk_vtoc.v_part[i].p_start,
disk_vtoc.v_part[i].p_start +
disk_vtoc.v_part[i].p_size);
if (!io_nifdisk) {
(void) printf(" remove ?:");
if (yesno()) {
disk_vtoc.v_part[i].p_size = 0;
disk_vtoc.v_part[i].p_start = 0;
disk_vtoc.v_part[i].p_tag = 0;
disk_vtoc.v_part[i].p_flag = 0;
}
} else {
disk_vtoc.v_part[i].p_size = 0;
disk_vtoc.v_part[i].p_start = 0;
disk_vtoc.v_part[i].p_tag = 0;
disk_vtoc.v_part[i].p_flag = 0;
(void) fprintf(stderr,
" removed!\n");
}
}
continue;
}
if (disk_vtoc.v_part[i].p_start > numsect) {
(void) fprintf(stderr,
"slice %d (start %llu) is larger than the "
"partition", i, disk_vtoc.v_part[i].p_start);
if (!io_nifdisk) {
(void) printf(" remove ?:");
if (yesno()) {
disk_vtoc.v_part[i].p_size = 0;
disk_vtoc.v_part[i].p_start = 0;
disk_vtoc.v_part[i].p_tag = 0;
disk_vtoc.v_part[i].p_flag = 0;
}
} else {
disk_vtoc.v_part[i].p_size = 0;
disk_vtoc.v_part[i].p_start = 0;
disk_vtoc.v_part[i].p_tag = 0;
disk_vtoc.v_part[i].p_flag = 0;
(void) fprintf(stderr,
" removed!\n");
}
} else if (disk_vtoc.v_part[i].p_start
+ disk_vtoc.v_part[i].p_size > numsect) {
(void) fprintf(stderr,
"slice %d (end %llu) is larger"
" than the partition",
i,
disk_vtoc.v_part[i].p_start +
disk_vtoc.v_part[i].p_size);
if (!io_nifdisk) {
(void) printf(" adjust ?:");
if (yesno()) {
disk_vtoc.v_part[i].p_size = numsect;
}
} else {
disk_vtoc.v_part[i].p_size = numsect;
(void) fprintf(stderr, " adjusted!\n");
}
}
}
#if 1 /* bh for now */
/* Make the VTOC look sane - ha ha */
disk_vtoc.v_version = V_VERSION;
disk_vtoc.v_sanity = VTOC_SANE;
disk_vtoc.v_nparts = V_NUMPAR;
if (disk_vtoc.v_sectorsz == 0)
disk_vtoc.v_sectorsz = NBPSCTR;
#endif
/* Write the VTOC back to the disk */
if (!io_readonly)
(void) writevtoc();
}
/*
* yesno
* Get yes or no answer. Return 1 for yes and 0 for no.
*/
static int
yesno(void)
{
char s[80];
for (;;) {
(void) fgets(s, sizeof (s), stdin);
rm_blanks(s);
if (((s[1] != '\0') && (s[1] != '\n')) ||
((s[0] != 'y') && (s[0] != 'n'))) {
(void) printf(E_LINE);
(void) printf("Please answer with \"y\" or \"n\": ");
continue;
}
if (s[0] == 'y')
return (1);
else
return (0);
}
}
/*
* readvtoc
* Read the VTOC from the Solaris partition of the device.
*/
static int
readvtoc(void)
{
int i;
int retval = VTOC_OK;
if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) {
if (i == VT_EINVAL) {
(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
vt_inval++;
retval = VTOC_INVAL;
} else if (i == VT_ENOTSUP) {
(void) fprintf(stderr, "fdisk: partition may have EFI "
"GPT\n");
retval = VTOC_NOTSUP;
} else {
(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
retval = VTOC_RWERR;
}
}
return (retval);
}
/*
* writevtoc
* Write the VTOC to the Solaris partition on the device.
*/
static int
writevtoc(void)
{
int i;
int retval = 0;
if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) {
if (i == VT_EINVAL) {
(void) fprintf(stderr,
"fdisk: Invalid entry exists in VTOC.\n");
retval = VTOC_INVAL;
} else if (i == VT_ENOTSUP) {
(void) fprintf(stderr, "fdisk: partition may have EFI "
"GPT\n");
retval = VTOC_NOTSUP;
} else {
(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
retval = VTOC_RWERR;
}
}
return (retval);
}
/*
* efi_ioctl
* issues DKIOCSETEFI IOCTL
* (duplicate of private efi_ioctl() in rdwr_efi.c
*/
static int
efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
{
void *data = dk_ioc->dki_data;
int error;
dk_ioc->dki_data_64 = (uintptr_t)data;
error = ioctl(fd, cmd, (void *)dk_ioc);
return (error);
}
/*
* clear_efi
* Clear EFI labels from the EFI_PMBR partition on the device
* This function is modeled on the libefi(3LIB) call efi_write()
*/
static int
clear_efi(void)
{
struct dk_gpt *efi_vtoc;
dk_efi_t dk_ioc;
/*
* see if we can read the EFI label
*/
if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
return (VT_ERROR);
}
/*
* set up the dk_ioc structure for writing
*/
dk_ioc.dki_lba = 1;
dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
return (VT_ERROR);
}
/*
* clear the primary label
*/
if (io_debug) {
(void) fprintf(stderr,
"\tClearing primary EFI label at block %lld\n",
dk_ioc.dki_lba);
}
if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
free(dk_ioc.dki_data);
switch (errno) {
case EIO:
return (VT_EIO);
case EINVAL:
return (VT_EINVAL);
default:
return (VT_ERROR);
}
}
/*
* clear the backup partition table
*/
dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
efi_vtoc->efi_lbasize);
if (io_debug) {
(void) fprintf(stderr,
"\tClearing backup partition table at block %lld\n",
dk_ioc.dki_lba);
}
if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
"block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
errno);
}
/*
* clear the backup label
*/
dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
dk_ioc.dki_length = efi_vtoc->efi_lbasize;
dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
efi_vtoc->efi_lbasize);
if (io_debug) {
(void) fprintf(stderr, "\tClearing backup label at block "
"%lld\n", dk_ioc.dki_lba);
}
if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
(void) fprintf(stderr,
"\tUnable to clear backup EFI label at "
"block %llu; errno %d\n",
efi_vtoc->efi_last_lba,
errno);
}
free(dk_ioc.dki_data);
efi_free(efi_vtoc);
return (0);
}
/*
* clear_vtoc
* Clear the VTOC from the current or previous Solaris partition on the
* device.
*/
static void
clear_vtoc(int table, int part)
{
struct ipart *clr_table;
char *disk_label;
uint32_t pcyl, ncyl, count;
diskaddr_t backup_block, solaris_offset;
ssize_t bytes;
off_t seek_byte;
#ifdef DEBUG
char *read_label;
#endif /* DEBUG */
if (table == OLD) {
clr_table = &Old_Table[part];
} else {
clr_table = &Table[part];
}
disk_label = (char *)calloc(sectsiz, 1);
if (disk_label == NULL) {
return;
}
seek_byte = (off_t)(lel(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
if (io_debug) {
(void) fprintf(stderr,
"\tClearing primary VTOC at byte %llu (block %llu)\n",
(uint64_t)seek_byte,
(uint64_t)(lel(clr_table->relsect) + VTOC_OFFSET));
}
if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
(void) fprintf(stderr,
"\tError seeking to primary label at byte %llu\n",
(uint64_t)seek_byte);
free(disk_label);
return;
}
bytes = write(Dev, disk_label, sectsiz);
if (bytes != sectsiz) {
(void) fprintf(stderr,
"\tWarning: only %d bytes written to clear primary"
" VTOC!\n", bytes);
}
#ifdef DEBUG
if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
(void) fprintf(stderr,
"DEBUG: Error seeking to primary label at byte %llu\n",
(uint64_t)seek_byte);
free(disk_label);
return;
} else {
(void) fprintf(stderr,
"DEBUG: Successful lseek() to byte %llu\n",
(uint64_t)seek_byte);
}
read_label = (char *)calloc(sectsiz, 1);
if (read_label == NULL) {
free(disk_label);
return;
}
bytes = read(Dev, read_label, sectsiz);
if (bytes != sectsiz) {
(void) fprintf(stderr,
"DEBUG: Warning: only %d bytes read of label\n",
bytes);
}
if (memcmp(disk_label, read_label, sectsiz) != 0) {
(void) fprintf(stderr,
"DEBUG: Warning: disk_label and read_label differ!!!\n");
} else {
(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
"read_label\n");
}
#endif /* DEBUG */
/* Clear backup label */
pcyl = lel(clr_table->numsect) / (heads * sectors);
solaris_offset = lel(clr_table->relsect);
ncyl = pcyl - acyl;
backup_block = ((ncyl + acyl - 1) *
(heads * sectors)) + ((heads - 1) * sectors) + 1;
for (count = 1; count < 6; count++) {
seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz;
if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
(void) fprintf(stderr,
"\tError seeking to backup label at byte %llu on "
"%s.\n", (uint64_t)seek_byte, Dfltdev);
free(disk_label);
#ifdef DEBUG
free(read_label);
#endif /* DEBUG */
return;
}
if (io_debug) {
(void) fprintf(stderr, "\tClearing backup VTOC at"
" byte %llu (block %llu)\n",
(uint64_t)seek_byte,
(uint64_t)(solaris_offset + backup_block));
}
bytes = write(Dev, disk_label, sectsiz);
if (bytes != sectsiz) {
(void) fprintf(stderr,
"\t\tWarning: only %d bytes written to "
"clear backup VTOC at block %llu!\n", bytes,
(uint64_t)(solaris_offset + backup_block));
}
#ifdef DEBUG
if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
(void) fprintf(stderr,
"DEBUG: Error seeking to backup label at byte %llu\n",
(uint64_t)seek_byte);
free(disk_label);
free(read_label);
return;
} else {
(void) fprintf(stderr,
"DEBUG: Successful lseek() to byte %llu\n",
(uint64_t)seek_byte);
}
bytes = read(Dev, read_label, sectsiz);
if (bytes != sectsiz) {
(void) fprintf(stderr,
"DEBUG: Warning: only %d bytes read of backup label\n",
bytes);
}
if (memcmp(disk_label, read_label, sectsiz) != 0) {
(void) fprintf(stderr,
"DEBUG: Warning: disk_label and read_label differ!!!\n");
} else {
(void) fprintf(stderr,
"DEBUG: Good compare of disk_label and backup "
"read_label\n");
}
#endif /* DEBUG */
backup_block += 2;
}
#ifdef DEBUG
free(read_label);
#endif /* DEBUG */
free(disk_label);
}
#define FDISK_STANDARD_LECTURE \
"Fdisk is normally used with the device that " \
"represents the entire fixed disk.\n" \
"(For example, /dev/rdsk/c0d0p0 on x86 or " \
"/dev/rdsk/c0t5d0s2 on sparc).\n"
#define FDISK_LECTURE_NOT_SECTOR_ZERO \
"The device does not appear to include absolute\n" \
"sector 0 of the PHYSICAL disk " \
"(the normal location for an fdisk table).\n"
#define FDISK_LECTURE_NOT_FULL \
"The device does not appear to encompass the entire PHYSICAL disk.\n"
#define FDISK_LECTURE_NO_VTOC \
"Unable to find a volume table of contents.\n" \
"Cannot verify the device encompasses the full PHYSICAL disk.\n"
#define FDISK_LECTURE_NO_GEOM \
"Unable to get geometry from device.\n" \
"Cannot verify the device encompasses the full PHYSICAL disk.\n"
#define FDISK_SHALL_I_CONTINUE \
"Are you sure you want to continue? (y/n) "
/*
* lecture_and_query
* Called when a sanity check fails. This routine gives a warning
* specific to the check that fails, followed by a generic lecture
* about the "right" device to supply as input. Then, if appropriate,
* it will prompt the user on whether or not they want to continue.
* Inappropriate times for prompting are when the user has selected
* non-interactive mode or read-only mode.
*/
static int
lecture_and_query(char *warning, char *devname)
{
if (io_nifdisk)
return (0);
(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
(void) fprintf(stderr, "%s", warning);
(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
return (yesno());
}
static void
sanity_check_provided_device(char *devname, int fd)
{
struct extvtoc v;
struct dk_geom d;
struct part_info pi;
struct extpart_info extpi;
diskaddr_t totsize;
int idx = -1;
/*
* First try the PARTINFO ioctl. If it works, we will be able
* to tell if they've specified the full disk partition by checking
* to see if they've specified a partition that starts at sector 0.
*/
if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) {
if (extpi.p_start != 0) {
if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
devname)) {
(void) close(fd);
exit(1);
}
}
} else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
if (pi.p_start != 0) {
if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
devname)) {
(void) close(fd);
exit(1);
}
}
} else {
if ((idx = read_extvtoc(fd, &v)) < 0) {
if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
devname)) {
(void) close(fd);
exit(1);
}
return;
}
if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
perror(devname);
if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
devname)) {
(void) close(fd);
exit(1);
}
return;
}
totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
if (v.v_part[idx].p_size != totsize) {
if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
devname)) {
(void) close(fd);
exit(1);
}
}
}
}
/*
* get_node
* Called from main to construct the name of the device node to open.
* Initially tries to stat the node exactly as provided, if that fails
* we prepend the default path (/dev/rdsk/).
*/
static char *
get_node(char *devname)
{
char *node;
struct stat statbuf;
size_t space;
/* Don't do anything if we are skipping device checks */
if (io_image)
return (devname);
node = devname;
/* Try the node as provided first */
if (stat(node, (struct stat *)&statbuf) == -1) {
/*
* Copy the passed in string to a new buffer, prepend the
* default path and try again.
*/
space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
if ((node = malloc(space)) == NULL) {
(void) fprintf(stderr, "fdisk: Unable to obtain memory "
"for device node.\n");
exit(1);
}
/* Copy over the default path and the provided node */
(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
space -= strlen(DEFAULT_PATH);
(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
/* Try to stat it again */
if (stat(node, (struct stat *)&statbuf) == -1) {
/* Failed all options, give up */
(void) fprintf(stderr,
"fdisk: Cannot stat device %s.\n",
devname);
exit(1);
}
}
/* Make sure the device specified is the raw device */
if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
(void) fprintf(stderr,
"fdisk: %s must be a raw device.\n", node);
exit(1);
}
return (node);
}