fdisk.c revision 2f8ec71967eea9b7dd13e4a224a56156ff9d3c4f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
/* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/*
* PROGRAM: fdisk(1M)
* This program reads the partition table on the specified device and
* also reads the drive parameters. The user can perform various
* operations from a supplied menu or from the command line. Diagnostic
* options are also available.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <limits.h>
#include <sys/systeminfo.h>
#include <sys/efi_partition.h>
#include <sys/byteorder.h>
#include <sys/systeminfo.h>
#define CLR_SCR "[1;1H[0J"
#define CLR_LIN "[0K"
#define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \
"[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H"
#define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K"
#define W_LINE "[12;1H[0K[11;1H[0K"
#define E_LINE "[24;1H[0K[23;1H[0K"
#define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \
"[0K[18;1H[0K[19;1H[0K[13;1H"
#define T_LINE "[1;1H[0K"
#define DEFAULT_PATH "/dev/rdsk/"
/* XXX - should be in fdisk.h, used by sd as well */
/*
* the MAX values are the maximum usable values for BIOS chs values
* The MAX_CYL value of 1022 is the maximum usable value
* the value of 1023 is a fence value,
* indicating no CHS geometry exists for the corresponding LBA value.
* HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
* SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
*/
#define MAX_SECT (63)
#define MAX_CYL (1022)
#define MAX_HEAD (254)
/* for clear_vtoc() */
#define OLD 0
#define NEW 1
#define VTOC_OK 0 /* Good VTOC */
/*
* Support for fdisk(1M) on the SPARC platform
* In order to convert little endian values to big endian for SPARC,
* These swapping macros will be used to access information in the
* mboot and ipart structures.
*/
#ifdef sparc
#else
#endif
#if defined(_SUNOS_VTOC_16)
#define VTOC_OFFSET 1
#elif defined(_SUNOS_VTOC_8)
#define VTOC_OFFSET 0
#else
#endif
static char Usage[] = "Usage: fdisk\n"
"[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
"[ -b masterboot ]\n"
"[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
"[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
"[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
"[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
static char Usage1[] = " Partition options:\n"
" -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
" Create a partition with specific attributes:\n"
" id = system id number (fdisk.h) for the partition type\n"
" act = active partition flag (0 is off and 128 is on)\n"
" bhead = beginning head for start of partition\n"
" bsect = beginning sector for start of partition\n"
" bcyl = beginning cylinder for start of partition\n"
" ehead = ending head for end of partition\n"
" esect = ending sector for end of partition\n"
" ecyl = ending cylinder for end of partition\n"
" rsect = sector number from start of disk for\n"
" start of partition\n"
" numsect = partition size in sectors\n"
" -b master_boot\n"
" Use master_boot as the master boot file.\n"
" -B Create one Solaris partition that uses the entire disk.\n"
" -E Create one EFI partition that uses the entire disk.\n"
" -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
" Delete a partition. See attribute definitions for -A.\n"
" -F fdisk_file\n"
" Use fdisk_file to initialize on-line fdisk table.\n"
" -I Forego device checks. Generate a file image of what would go\n"
" on a disk using the geometry specified with the -S option.\n"
" -n Do not run in interactive mode.\n"
" -R Open the disk device as read-only.\n"
" -t Check and adjust VTOC to be consistent with fdisk table.\n"
" VTOC slices exceeding the partition size will be truncated.\n"
" -T Check and adjust VTOC to be consistent with fdisk table.\n"
" VTOC slices exceeding the partition size will be removed.\n"
" -W fdisk_file\n"
" Write on-disk table to fdisk_file.\n"
" -W - Write on-disk table to standard output.\n"
" -v Display virtual geometry. Must be used with the -W option.\n"
" Diagnostic options:\n"
" -d Activate debug information about progress.\n"
" -g Write label geometry to standard output:\n"
" PCYL number of physical cylinders\n"
" NCYL number of usable cylinders\n"
" ACYL number of alternate cylinders\n"
" BCYL cylinder offset\n"
" NHEADS number of heads\n"
" NSECTORS number of sectors per track\n"
" SECTSIZ size of a sector in bytes\n"
" -G Write physical geometry to standard output (see -g).\n"
" -h Issue this verbose help message.\n"
" -o offset\n"
" Block offset from start of disk (default 0). Ignored if\n"
" -P # specified.\n"
" -P fill_patt\n"
" Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
" hexadecimal and is used as number for constant long word\n"
" pattern. If fill_patt is \"#\" then pattern of block #\n"
" for each block. Pattern is put in each block as long words\n"
" and fills each block (see -o and -s).\n"
" -r Read from a disk to stdout (see -o and -s).\n"
" -s size Number of blocks on which to perform operation (see -o).\n"
" -S geom_file\n"
" Use geom_file to set the label geometry (see -g).\n"
" -w Write to a disk from stdin (see -o and -s).";
static char Ostr[] = "Other OS";
static char Dstr[] = "DOS12";
static char D16str[] = "DOS16";
static char DDstr[] = "DOS-DATA";
static char EDstr[] = "EXT-DOS";
static char DBstr[] = "DOS-BIG";
static char PCstr[] = "PCIX";
static char Ustr[] = "UNIX System";
static char SUstr[] = "Solaris";
static char SU2str[] = "Solaris2";
static char X86str[] = "x86 Boot";
static char DIAGstr[] = "Diagnostic";
static char IFSstr[] = "IFS: NTFS";
static char AIXstr[] = "AIX Boot";
static char AIXDstr[] = "AIX Data";
static char OS2str[] = "OS/2 Boot";
static char WINstr[] = "Win95 FAT32";
static char EWINstr[] = "Ext Win95";
static char FAT95str[] = "FAT16 LBA";
static char EXTLstr[] = "EXT LBA";
static char LINUXstr[] = "Linux";
static char CPMstr[] = "CP/M";
static char NOVstr[] = "Netware 3.x+";
static char QNXstr[] = "QNX 4.x";
static char QNX2str[] = "QNX part 2";
static char QNX3str[] = "QNX part 3";
static char LINNATstr[] = "Linux native";
static char NTFSVOL1str[] = "NT volset 1";
static char NTFSVOL2str[] = "NT volset 2";
static char BSDstr[] = "BSD OS";
static char NEXTSTEPstr[] = "NeXTSTEP";
static char BSDIFSstr[] = "BSDI FS";
static char BSDISWAPstr[] = "BSDI swap";
static char Actvstr[] = "Active";
static char EFIstr[] = "EFI";
static char NAstr[] = " ";
/* All the user options and flags */
static char *Dfltdev; /* name of fixed disk drive */
/* Diagnostic options */
static int io_wrt = 0; /* write stdin to disk (-w) */
static int io_rd = 0; /* read disk and write stdout (-r) */
static char *io_fatt; /* user supplied pattern (-P pattern) */
static int io_patt = 0; /* write pattern to disk (-P pattern) */
static int io_lgeom = 0; /* get label geometry (-g) */
static int io_pgeom = 0; /* get drive physical geometry (-G) */
static char *io_sgeom = 0; /* set label geometry (-S geom_file) */
static int io_readonly = 0; /* do not write to disk (-R) */
/* The -o offset and -s size options specify the area of the disk on */
/* which to perform the particular operation; i.e., -P, -r, or -w. */
/* Partition table flags */
static int v_flag = 0; /* virtual geometry-HBA flag (-v) */
static int stdo_flag = 0; /* stdout flag (-W -) */
static int io_fdisk = 0; /* do fdisk operation */
static int io_ifdisk = 0; /* interactive partition */
static int io_nifdisk = 0; /* non-interactive partition (-n) */
static char *io_ffdisk = 0; /* input fdisk file name (-F file) */
static char *io_Wfdisk = 0; /* output fdisk file name (-W file) */
static char *io_Afdisk = 0; /* add entry to partition table (-A) */
static char *io_Dfdisk = 0; /* delete entry from part. table (-D) */
static char *io_mboot = 0; /* master boot record (-b boot_file) */
static int io_wholedisk = 0; /* use whole disk for Solaris (-B) */
static int io_EFIdisk = 0; /* use whole disk for EFI (-E) */
static int io_debug = 0; /* activate verbose mode (-d) */
static int io_image = 0; /* create image using geometry (-I) */
static char *Bootsect; /* pointer to sector zero buffer */
static char *Nullsect;
static int vt_inval = 0;
static int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */
static int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */
/* Disk geometry information */
static int Dev; /* fd for open device */
static int Numcyl_usable; /* Number of usable cylinders */
/* used to limit fdisk to 2TB */
/* Physical geometry for the drive */
static int Numcyl; /* number of cylinders */
static int heads; /* number of heads */
static int sectors; /* number of sectors per track */
static int acyl; /* number of alternate sectors */
/* HBA (virtual) geometry for the drive */
static int hba_Numcyl; /* number of cylinders */
static int hba_heads; /* number of heads */
static int hba_sectors; /* number of sectors per track */
static int sectsiz; /* sector size */
/* Load functions for fdisk table modification */
#define LOADFILE 0 /* load fdisk from file */
#define CBUFLEN 80
static char s[CBUFLEN];
static void dev_mboot_read(void);
static void mboot_read(void);
static void fill_patt(void);
static void abs_read(void);
static void abs_write(void);
static void Set_Table_CHS_Values(int ti);
static int verify_tbl(void);
static int pars_fdisk(char *line,
static void stage0(void);
static int pcreate(void);
static void dispmenu(void);
static int pchange(void);
static int ppartid(void);
static char pdelete(void);
static void rm_blanks(char *s);
static int getcyl(void);
static void disptbl(void);
static void print_Table(void);
static void copy_Table_to_Old_Table(void);
static void nulltbl(void);
static void copy_Bootblk_to_Table(void);
#ifdef sparc
#endif
static void copy_Table_to_Bootblk(void);
static int TableChanged(void);
static void ffile_write(char *file);
static void fix_slice(void);
static int yesno(void);
static int readvtoc(void);
static int writevtoc(void);
static int clear_efi(void);
static void
{
if (table_changed) {
/*
* Copy the new table back to the sector buffer
* and write it to disk
*/
}
/* If the VTOC table is wrong fix it (truncation only) */
if (io_adjt)
fix_slice();
exit(0);
}
/*
* main
* Process command-line options.
*/
int
{
int c, i;
extern int optind;
extern char *optarg;
int errflg = 0;
int diag_cnt = 0;
int openmode;
/* Process the options. */
!= EOF) {
switch (c) {
case 'o':
continue;
case 's':
continue;
case 'P':
diag_cnt++;
io_patt++;
continue;
case 'w':
diag_cnt++;
io_wrt++;
continue;
case 'r':
diag_cnt++;
io_rd++;
continue;
case 'd':
io_debug++;
continue;
case 'I':
io_image++;
continue;
case 'R':
io_readonly++;
continue;
case 'S':
diag_cnt++;
continue;
case 'T':
io_ADJT++;
/* FALLTHRU */
case 't':
io_adjt++;
continue;
case 'B':
io_wholedisk++;
io_fdisk++;
continue;
case 'E':
io_EFIdisk++;
io_fdisk++;
continue;
case 'g':
diag_cnt++;
io_lgeom++;
continue;
case 'G':
diag_cnt++;
io_pgeom++;
continue;
case 'n':
io_nifdisk++;
io_fdisk++;
continue;
case 'F':
io_fdisk++;
continue;
case 'b':
continue;
case 'W':
/*
* If '-' is the -W argument, then write
* to standard output, otherwise write
* to the specified file.
*/
stdo_flag = 1;
else
io_fdisk++;
continue;
case 'A':
io_fdisk++;
continue;
case 'D':
io_fdisk++;
continue;
case 'h':
exit(0);
/* FALLTHRU */
case 'v':
v_flag = 1;
continue;
case '?':
errflg++;
break;
}
break;
}
diag_cnt = 0;
}
/* User option checking */
/* By default, run in interactive mode */
io_ifdisk++;
io_fdisk++;
}
errflg++;
}
/* Was any error detected? */
"\nDetailed help is available with the -h option.\n");
exit(2);
}
/* Figure out the correct device node to open */
if (io_readonly)
else
"fdisk: Cannot open device %s.\n",
Dfltdev);
exit(1);
}
/*
* not all disk (or disklike) drivers support DKIOCGMEDIAINFO
* in that case leave the minfo structure zeroed
*/
}
/* Get the disk geometry */
if (!io_image) {
/* Get disk's HBA (virtual) geometry */
errno = 0;
/*
* If ioctl isn't implemented on this platform, then
* turn off flag to print out virtual geometry (-v),
* otherwise use the virtual geometry.
*/
v_flag = 0;
no_virtgeom_ioctl = 1;
/*
* This means that the ioctl exists, but
* is invalid for this disk, meaning the
* disk doesn't have an HBA geometry
* (like, say, it's larger than 8GB).
*/
v_flag = 0;
} else {
"%s: Cannot get virtual disk geometry.\n",
exit(1);
}
} else {
/* save virtual geometry values obtained by ioctl */
}
errno = 0;
no_physgeom_ioctl = 1;
} else {
"%s: Cannot get physical disk geometry.\n",
exit(1);
}
}
/*
* Call DKIOCGGEOM if the ioctls for physical and virtual
* geometry fail. Get both from this generic call.
*/
if (no_virtgeom_ioctl && no_physgeom_ioctl) {
errno = 0;
"%s: Cannot get disk label geometry.\n",
exit(1);
}
}
sectsiz = 512;
/*
* if hba geometry was not set by DKIOC_VIRTGEOM
* or we got an invalid hba geometry
* then set hba geometry based on max values
*/
if (no_virtgeom_ioctl ||
/*
* turn off flag to print out virtual geometry (-v)
*/
v_flag = 0;
(hba_sectors * hba_heads);
}
if (io_debug) {
" cylinders[%d] heads[%d] sectors[%d]\n"
" sector size[%d] blocks[%d] mbytes[%d]\n",
" cylinders[%d] heads[%d] sectors[%d]\n"
" sector size[%d] blocks[%d] mbytes[%d]\n",
1048576);
}
}
/* If user has requested a geometry report just do it and exit */
if (io_lgeom) {
"%s: Cannot get disk label geometry.\n",
exit(1);
}
sectsiz = 512;
(void) printf(
"* PCYL NCYL ACYL BCYL NHEAD NSECT"
" SECSIZ\n");
(void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
sectsiz);
exit(0);
} else if (io_pgeom) {
"%s: Cannot get physical disk geometry.\n",
exit(1);
}
(void) printf(
"* PCYL NCYL ACYL BCYL NHEAD NSECT"
" SECSIZ\n");
(void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
sectsiz);
exit(0);
} else if (io_sgeom) {
exit(1);
} else if (!io_image) {
exit(0);
}
}
/*
* some drivers may not support DKIOCGMEDIAINFO
* in that case use CHS
*/
if (chs_capacity > DK_MAX_2TB) {
/* limit to 2TB */
}
if (minfo.dki_capacity > 0)
/* Allocate memory to hold three complete sectors */
"fdisk: Unable to obtain enough buffer memory"
" (%d bytes).\n",
3 * sectsiz);
exit(1);
}
/* Zero out the "NULL" sector */
for (i = 0; i < sectsiz; i++) {
Nullsect[i] = 0;
}
/* Find out what the user wants done */
if (io_rd) { /* abs disk read */
abs_read(); /* will not return */
} else if (io_wrt && !io_readonly) {
abs_write(); /* will not return */
} else if (io_patt && !io_readonly) {
fill_patt(); /* will not return */
}
/* This is the fdisk edit, the real reason for the program. */
/* Get the new BOOT program in case we write a new fdisk table */
mboot_read();
/* Read from disk master boot */
/*
* Verify and copy the device's fdisk table. This will be used
* as the prototype mboot if the device's mboot looks invalid.
*/
/* save away a copy of Table in Old_Table for sensing changes */
/* Load fdisk table from specified file (-F fdisk_file) */
if (io_ffdisk) {
/* Load and verify user-specified table parameters */
}
/* Does user want to delete or add an entry? */
if (io_Dfdisk) {
}
if (io_Afdisk) {
}
/* Check if there is no fdisk table */
(void) printf(
"No fdisk table exists. The default"
" partition for the disk is:\n\n"
" a 100%% \"SOLARIS System\" "
"partition\n\n"
"Type \"y\" to accept the default "
"partition, otherwise type \"n\" to "
"edit the\n partition table.\n");
if (Numcyl > Numcyl_usable)
(void) printf("WARNING: Disk is larger"
" than 2TB. Solaris partition will"
" be limited to 2 TB.\n");
}
/* Edit the partition table as directed */
/* Default scenario */
nulltbl();
/* now set up UNIX System partition */
/* calculate CHS values for table entry 0 */
} else if (io_EFIdisk) {
/* create an EFI partition for the whole disk */
nulltbl();
(dev_capacity - 1));
if (i != 0) {
"Error creating EFI partition\n");
exit(1);
}
}
}
}
/* Display complete fdisk table entries for debugging purposes */
if (io_debug) {
print_Table();
if (io_ifdisk) {
(void) gets(s);
}
}
/* Interactive fdisk mode */
if (io_ifdisk) {
disptbl();
for (;;) {
stage0();
disptbl();
}
}
/* If user wants to write the table to a file, do it */
if (io_Wfdisk)
else if (stdo_flag)
ffile_write((char *)stdout);
return (0);
}
/*
* read_geom
* Read geometry from specified file (-S).
*/
static int
{
char line[256];
/* open the prototype file */
io_sgeom);
return (1);
}
/* Read a line from the file */
continue;
else {
§siz) != 7) {
"Syntax error:\n \"%s\".\n",
line);
return (1);
}
break;
} /* else */
} /* while (fgets(line, sizeof (line) - 1, fp)) */
if (!io_image) {
"fdisk: Cannot set label geometry.\n");
return (1);
}
} else {
}
return (0);
}
/*
* dev_mboot_read
* Read the master boot sector from the device.
*/
static void
dev_mboot_read(void)
{
perror("Error in ioctl DKIOCGMBOOT");
}
if (errno == 0)
return;
"fdisk: Error seeking to partition table on %s.\n",
Dfltdev);
if (!io_image)
exit(1);
}
"fdisk: Error reading partition table from %s.\n",
Dfltdev);
if (!io_image)
exit(1);
}
}
/*
* dev_mboot_write
* Write the master boot sector to the device.
*/
static void
{
int clr_efi = -1;
if (io_readonly)
return;
if (io_debug) {
print_Table();
if (io_ifdisk) {
(void) gets(s);
}
}
/*
* If the new table has any Solaris partitions and the old
* table does not have an entry that describes it
* exactly then clear the old vtoc (if any).
*/
/* We only care about potential Solaris parts. */
continue;
/* Does the old table have an exact entry for the new entry? */
/* We only care about old Solaris partitions. */
/* Is this old one the same as a new one? */
break; /* Yes */
}
}
/* Did a solaris partition change location or size? */
if (old_pt >= FD_NUMPART) {
/* Yes clear old vtoc */
if (io_debug) {
"Clearing VTOC labels from NEW"
" table\n");
}
}
}
/* see if the old table had EFI */
}
}
continue;
break;
}
/*
* if EFI partition changed, set the flag to clear
* the EFI GPT
*/
clr_efi = 0;
}
break;
}
/* clear labels if necessary */
if (clr_efi >= 0) {
if (io_debug) {
}
if (io_debug) {
"\tError %d clearing EFI labels"
" (probably no EFI labels exist)\n",
error);
}
}
}
"fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
Dfltdev);
}
if (errno == 0)
return;
/* write to disk drive */
"fdisk: Error seeking to master boot record on %s.\n",
Dfltdev);
exit(1);
}
"fdisk: Error writing master boot record to %s.\n",
Dfltdev);
exit(1);
}
}
/*
* mboot_read
* Read the prototype boot records from the files.
*/
static void
mboot_read(void)
{
int mDev, i;
/*
* If the master boot file hasn't been specified, use the
* implementation architecture name to generate the default one.
*/
if (io_mboot == (char *)0) {
/*
* Bug ID 1249035:
* The mboot file must be delivered on all platforms
* and installed in a non-platform-dependent
*/
}
/* First read in the master boot record */
/* Open the master boot proto file */
"fdisk: Cannot open master boot file %s.\n",
io_mboot);
exit(1);
}
/* Read the master boot program */
(struct mboot)) {
"fdisk: Cannot read master boot file %s.\n",
io_mboot);
exit(1);
}
/* Is this really a master boot record? */
"fdisk: Invalid master boot file %s.\n", io_mboot);
"Bad magic number: is %x, but should be %x.\n",
exit(1);
}
#else
#endif
/* Zero out the partitions part of this record */
for (i = 0; i < FD_NUMPART; i++, part++) {
}
}
/*
* fill_patt
*/
static void
fill_patt(void)
{
int *buff_ptr, i;
int io_fpatt = 0;
int io_ipatt = 0;
io_fpatt++;
}
/*
* Fill disk with pattern based on block number.
* Write to the disk at absolute relative block io_offset
* for io_size blocks.
*/
while (io_size--) {
if (!io_fpatt) {
for (i = 0; i < sectsiz;
}
/* Write the data to disk */
SEEK_SET) == -1) {
Dfltdev);
exit(1);
}
Dfltdev);
exit(1);
}
} /* while (--io_size); */
}
/*
* abs_read
* Read from the disk at absolute relative block io_offset for
* io_size blocks. Write the data to standard ouput (-r).
*/
static void
abs_read(void)
{
int c;
while (io_size--) {
SEEK_SET) == -1) {
Dfltdev);
exit(1);
}
Dfltdev);
exit(1);
}
/* Write to standard ouptut */
if (c >= 0) {
if (io_debug)
"fdisk: Output warning: %d of %d"
" characters written.\n",
c, sectsiz);
exit(2);
} else {
perror("write error on output file.");
exit(2);
}
} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
/* != sectsiz) */
} /* while (--io_size); */
exit(0);
}
/*
* abs_write
* Read the data from standard input. Write to the disk at
* absolute relative block io_offset for io_size blocks (-w).
*/
static void
abs_write(void)
{
int c, i;
while (io_size--) {
int part_exit = 0;
/* Read from standard input */
if (c >= 0) {
if (io_debug)
"fdisk: WARNING: Incomplete read (%d of"
" %d characters read) on input file.\n",
c, sectsiz);
/* Fill pattern to mark partial sector in buf */
for (i = c; i < sectsiz; ) {
Bootsect[i++] = 0x41;
Bootsect[i++] = 0x62;
Bootsect[i++] = 0x65;
Bootsect[i++] = 0;
}
part_exit++;
} else {
perror("read error on input file.");
exit(2);
}
}
/* Write to disk drive */
SEEK_SET) == -1) {
Dfltdev);
exit(1);
}
Dfltdev);
exit(1);
}
if (part_exit)
exit(0);
} /* while (--io_size); */
exit(1);
}
/*
* load
* Load will either read the fdisk table from a file or add or
* delete an entry (-A, -D, -F).
*/
static void
{
int id;
int act;
int bhead;
int bsect;
int bcyl;
int ehead;
int esect;
int ecyl;
char line[256];
int i = 0;
int j;
switch (funct) {
case LOADFILE:
/*
* Zero out the table before loading it, which will
* force it to be updated on disk later (-F
* fdisk_file).
*/
nulltbl();
/* Open the prototype file */
"fdisk: Cannot open prototype partition file %s.\n",
file);
exit(1);
}
/* Read a line from the file */
continue;
}
/*
* Validate the partition. It cannot start at sector
* 0 unless it is UNUSED or already exists
*/
"fdisk: Error on entry \"%s\".\n",
line);
exit(1);
}
/*
* If we got here it means we copied an
* unmodified entry. So there is no need
* to insert it in the table or do any
* checks against disk size.
*
* This is a work around on the following
* situation (for IDE disks, at least):
* Different operation systems calculate
* disk size different ways, of which there
* are two main ways.
*
* The first, rounds the disk size to modulo
* cylinder size (virtual made-up cylinder
* usually based on maximum number of heads
* and sectors in partition table fields).
* Our OS's (for IDE) and most other "Unix"
* type OS's do this.
*
* The second, uses every single block
* on the disk (to maximize available space).
* Since disk manufactures do not know about
* "virtual cylinders", there are some number
* of blocks that make up a partial cylinder
* at the end of the disk.
*
* The difference between these two methods
* is where the problem is. When one
* tries to install Solaris/OpenSolaris on
* a disk that has another OS using that
* "partial cylinder", install fails. It fails
* since fdisk thinks its asked to create a
* partition with the -F option that contains
* a partition that runs off the end of the
* disk.
*/
continue;
}
/*
* Find an unused entry to use and put the entry
* in table
*/
"fdisk: Error on entry \"%s\".\n",
line);
exit(1);
}
} /* while (fgets(line, sizeof (line) - 1, fp)) */
if (verify_tbl() < 0) {
"fdisk: Cannot create partition table\n");
exit(1);
}
return;
case LOADDEL:
/* Parse the user-supplied deletion line (-D) */
"fdisk: Syntax error \"%s\"\n", file);
exit(1);
}
/* Find the exact entry in the table */
for (i = 0; i < FD_NUMPART; i++) {
/*
* Found the entry. Now move rest of
* entries up toward the top of the
* table, leaving available entries at
* the end of the fdisk table.
*/
for (j = i; j < FD_NUMPART - 1; j++) {
}
/*
* Mark the last entry as unused in case
* all table entries were in use prior
* to the deletion.
*/
return;
}
}
"fdisk: Entry does not match any existing partition:\n"
" \"%s\"\n",
file);
exit(1);
/* FALLTHRU */
case LOADADD:
/* Parse the user-supplied addition line (-A) */
"fdisk: Syntax error \"%s\"\n", file);
exit(1);
}
/* Validate the partition. It cannot start at sector 0 */
if (rsect == 0) {
"fdisk: New partition cannot start at sector 0:\n"
" \"%s\".\n",
file);
exit(1);
}
/*
* if the user wishes to add an EFI partition, we need
* more extensive validation. rsect should be 1, and
* numsect should equal the entire disk capacity - 1
*/
if (rsect != 1) {
"fdisk: EFI partitions must start at sector"
" 1 (input rsect = %d)\n", rsect);
exit(1);
}
if (dev_capacity > DK_MAX_2TB) {
if (numsect != DK_MAX_2TB) {
"fdisk: EFI partitions must "
"encompass the entire maximum 2 TB "
"(input numsect: %u - max: %llu)\n",
exit(1);
}
"fdisk: EFI partitions must encompass the "
"entire disk\n"
"(input numsect: %u - avail: %llu)\n",
dev_capacity - 1);
exit(1);
}
}
/* Find unused entry for use and put entry in table */
"fdisk: Invalid entry could not be inserted:\n"
" \"%s\"\n",
file);
exit(1);
}
/* Make sure new entry does not overlap existing entry */
if (verify_tbl() < 0) {
"fdisk: Cannot create partition \"%s\"\n", file);
exit(1);
}
} /* switch funct */
}
/*
* Set_Table_CHS_Values
*
* This will calculate the CHS values for beginning and ending CHS
* for a single partition table entry (ti) based on the relsect
* and numsect values contained in the partion table entry.
*
* hba_heads and hba_sectors contain the number of heads and sectors.
*
* If the number of cylinders exceeds the MAX_CYL,
* then maximum values will be placed in the corresponding chs entry.
*/
static void
Set_Table_CHS_Values(int ti)
{
/*
* the lba address cannot be expressed in CHS value
* so store the maximum CHS field values in the CHS fields.
*/
} else {
}
/*
* This code is identical to the code above
* except that it works on ending CHS values
*/
} else {
}
}
/*
* insert_tbl
* Insert entry into fdisk table. Check all user-supplied values
* for the entry, but not the validity relative to other table
* entries!
*/
static int
{
int i;
/* validate partition size */
"fdisk: Partition table exceeds the size of the disk.\n");
return (-1);
}
/* find UNUSED partition table entry */
for (i = 0; i < FD_NUMPART; i++) {
break;
}
}
if (i >= FD_NUMPART) {
return (-1);
}
/*
* If we have been called with a valid geometry, use it
* valid means non-zero values that fit in the BIOS fields
*/
} else {
/*
* The specified values are invalid,
* so calculate the values based on hba_heads, hba_sectors
*/
}
/*
* return partition index
*/
return (i);
}
/*
* entry_from_old_table
* If the specified entry is in the old table and is not a Solaris entry
* then insert same entry into new fdisk table. If we do this then
* all checks are skipped for that entry!
*/
static int
{
uint32_t i, j;
return (0);
for (i = 0; i < FD_NUMPART - 1; i++) {
/* find UNUSED partition table entry */
for (j = 0; j < FD_NUMPART; j++) {
sizeof (Table[0]));
skip_verify[j] = 1;
return (1);
}
}
return (0);
}
}
return (0);
}
/*
* verify_tbl
* Verify that no partition entries overlap or exceed the size of
* the disk.
*/
static int
verify_tbl(void)
{
int noMoreParts = 0;
int numParts = 0;
/* Make sure new entry does not overlap an existing entry */
for (i = 0; i < FD_NUMPART - 1; i++) {
numParts++;
/*
* No valid partitions allowed after an UNUSED or
* EFI_PMBR part
*/
if (noMoreParts) {
return (-1);
}
/*
* EFI_PMBR partitions must be the only partition
* and must be Table entry 0
*/
if (i == 0) {
noMoreParts = 1;
} else {
return (-1);
}
"Invalid starting sector "
"for EFI_PMBR partition:\n"
"relsect %d "
"(should be 1)\n",
return (-1);
}
"EFI_PMBR partition must "
"encompass the entire "
"disk.\n numsect %d - "
"actual %llu\n",
dev_capacity - 1);
return (-1);
}
}
/* make sure the partition isn't larger than the disk */
if (!skip_verify[i])
return (-1);
}
for (j = i + 1; j < FD_NUMPART; j++) {
if (noMoreParts) {
"Cannot add partition to "
"table; no more partitions "
"allowed\n");
if (io_debug) {
"DEBUG: Current "
"partition:\t"
"%d:%d:%d:%d:%d:"
"%d:%d:%d:%d:%d\n"
" Next "
"partition:\t\t"
"%d:%d:%d:%d:%d:"
"%d:%d:%d:%d:%d\n",
}
return (-1);
}
if ((rsect >=
continue;
} else {
"current partition overlaps"
" following partition\n");
return (-1);
}
}
}
} else {
noMoreParts = 1;
}
}
if (noMoreParts)
return (-1);
if (!skip_verify[i] &&
return (-1);
}
}
return (numParts);
}
/*
* pars_fdisk
* Parse user-supplied data to set up fdisk partitions
* (-A, -D, -F).
*/
static int
char *line,
{
int i;
return (1);
if (line[i] == '\0') {
break;
} else if (line[i] == ':') {
line[i] = ' ';
}
}
exit(1);
}
return (0);
}
/*
* validate_part
* Validate that a new partition does not start at sector 0. Only UNUSED
* partitions and previously existing partitions are allowed to start at 0.
*/
static int
{
int i;
for (i = 0; i < FD_NUMPART; i++) {
return (0);
}
"New partition cannot start at sector 0\n");
return (-1);
}
return (0);
}
/*
* stage0
* Print out interactive menu and process user input.
*/
static void
stage0(void)
{
dispmenu();
for (;;) {
(void) printf("Enter Selection: ");
(void) gets(s);
rm_blanks(s);
while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) {
(void) printf(
"Enter a one-digit number between 1 and 6.");
(void) printf("Enter Selection: ");
(void) gets(s);
rm_blanks(s);
}
switch (s[0]) {
case '1':
if (pcreate() == -1)
return;
break;
case '2':
if (pchange() == -1)
return;
break;
case '3':
if (pdelete() == -1)
return;
break;
case '4':
if (ppartid() == -1)
return;
break;
case '5':
/* update disk partition table, if changed */
if (TableChanged() == 1) {
}
/*
* If the VTOC table is wrong fix it
* (truncate only)
*/
if (io_adjt) {
fix_slice();
}
exit(0);
/* FALLTHRU */
case '6':
/*
* If the VTOC table is wrong fix it
* (truncate only)
*/
if (io_adjt) {
fix_slice();
}
exit(0);
/* FALLTHRU */
default:
break;
}
disptbl();
dispmenu();
}
}
/*
* pcreate
* Create partition entry in the table (interactive mode).
*/
static int
pcreate(void)
{
int i, j;
int retCode = 0;
i = 0;
for (;;) {
if (i == FD_NUMPART) {
(void) printf(
"The partition table is full!\n"
"You must delete a partition before creating"
" a new one.\n");
return (-1);
}
break;
}
i++;
}
numsect = 0;
for (i = 0; i < FD_NUMPART; i++) {
}
if (numsect >= chs_capacity) {
(void) printf("There is no more room on the disk for"
" another partition.\n");
(void) printf(
"You must delete a partition before creating"
" a new one.\n");
return (-1);
}
}
while (tsystid == 'z') {
/*
* The question here is expanding to more than what is
* allocated for question lines (Q_LINE) which garbles
* at least warning line. Clearing warning line as workaround
* for now.
*/
(void) printf(
"Select the partition type to create:\n"
" 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n"
" 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n"
" 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n"
" D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? ");
(void) gets(s);
rm_blanks(s);
if (s[1] != 0) {
(void) printf("Invalid selection, try again.");
continue;
}
switch (s[0]) {
case '0': /* exit */
return (-1);
case '1': /* Solaris partition */
break;
case '2': /* UNIX partition */
break;
case '3': /* PCIXOS partition */
break;
case '4': /* OTHEROS System partition */
break;
case '5':
break;
case '6':
break;
case '7':
break;
case '8':
break;
case '9':
break;
case 'a': /* x86 Boot partition */
case 'A':
break;
case 'b': /* Diagnostic boot partition */
case 'B':
break;
case 'c': /* FAT32 */
case 'C':
break;
case 'd': /* FAT32 and need extended int13 */
case 'D':
break;
case 'e': /* Extended partition, need extended int13 */
case 'E':
break;
case 'f':
case 'F':
break;
default:
(void) printf("Invalid selection, try again.");
continue;
}
}
if ((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");
/* create the new partition */
if (i != -1) {
/* see if it should be the active partition */
(void) printf(
"Should this become the active partition? If "
"yes, it will be activated\n"
"each time the computer is reset or turned on.\n"
"Please type \"y\" or \"n\". ");
if (yesno()) {
for (j = 0; j < FD_NUMPART; j++) {
if (j == i) {
(void) printf(
"Partition %d is now "
"the active partition.",
j + 1);
} else {
}
}
} else {
}
/* set up the return code */
i = 1;
}
} else {
/*
* partitions of type EFI_PMBR must be the only partitions in
* the table
*
* First, make sure there were no errors the table is
* empty
*/
retCode = verify_tbl();
if (retCode < 0) {
"fdisk: Cannot create EFI partition table; \n"
"current partition table is invalid.\n");
return (-1);
} else if (retCode > 0) {
(void) printf(
"An EFI partition must be the only partition on "
"disk. You may manually delete existing\n"
"partitions, or fdisk can do it.\n"
"Do you want fdisk to destroy existing "
"partitions?\n"
"Please type \"y\" or \"n\". ");
if (yesno()) {
nulltbl();
} else {
return (-1);
}
}
/* create the table entry - i should be 0 */
(dev_capacity - 1));
if (i != 0) {
(void) printf("Error creating EFI partition!!!\n");
i = -1;
} else {
/* EFI partitions are currently never active */
/* set up the return code */
i = 1;
}
}
return (i);
}
/*
* specify
* Query the user to specify the size of the new partition in
* terms of percentage of the disk or by specifying the starting
* cylinder and length in cylinders.
*/
static int
{
int i, j, percent = -1;
int cyl_size;
/*
* make a local copy of the partition table
* and sort it into relsect order
*/
for (i = 0; i < FD_NUMPART; i++)
for (i = 0; i < FD_NUMPART - 1; i++) {
break;
for (j = i + 1; j < FD_NUMPART; j++) {
break;
}
}
}
(void) printf(
"Specify the percentage of disk to use for this partition\n"
"(or type \"c\" to specify the size in cylinders). ");
(void) gets(s);
rm_blanks(s);
if (s[0] != 'c') { /* Specify size in percentage of disk */
i = 0;
while (s[i] != '\0') {
if (s[i] < '0' || s[i] > '9') {
(void) printf("Invalid percentage value "
"specified; retry the operation.");
return (-1);
}
i++;
if (i > 3) {
(void) printf("Invalid percentage value "
"specified; retry the operation.");
return (-1);
}
}
(void) printf(
"Percentage value is too large. The value must be"
" between 1 and 100;\nretry the operation.\n");
return (-1);
}
if (percent < 1) {
(void) printf(
"Percentage value is too small. The value must be"
" between 1 and 100;\nretry the operation.\n");
return (-1);
}
if (percent == 100)
else
/* Verify DOS12 partition doesn't exceed max size of 32MB. */
int n;
(void) printf("Maximum size for a DOS partition "
"is %d%%; retry the operation.",
n <= 100 ? n : 100);
return (-1);
}
max_free = 0;
for (i = 0; i < FD_NUMPART; i++) {
/*
* check for free space before partition i
* where i varies from 0 to 3
*
* freespace after partition 3 is unusable
* because there are no free partitions
*
* freespace begins at the end of previous partition
* or cylinder 1
*/
if (i) {
/* Not an empty table */
} else {
}
/*
* freespace ends before the current partition
* or the end of the disk (chs end)
*/
} else {
/*
* Partition might start before cylinder 1.
* Make sure free space is not negative.
*/
0;
}
/* save largest free space */
/* We found a place to use */
break;
}
"Maximum percentage available is %lld\n",
return (-1);
}
}
if (i >= FD_NUMPART) {
"fdisk: Partition table is full.\n");
return (-1);
}
if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
return (i);
}
return (-1);
} else {
/* Specifying size in cylinders */
(void) printf("Enter starting cylinder number: ");
(void) printf("Invalid number; retry the operation.");
return (-1);
}
if (cyl == 0) {
(void) printf(
"New partition cannot start at cylinder 0.\n");
return (-1);
}
if (cyl >= Numcyl_usable) {
(void) printf(
"Cylinder %d is out of bounds, "
"the maximum is %d.\n",
return (-1);
}
(void) printf("Enter partition size in cylinders: ");
(void) printf("Invalid number, retry the operation.");
return (-1);
}
for (i = 0; i < FD_NUMPART; i++) {
break;
(void) printf(
"Cylinder %d is already allocated"
"\nretry the operation.",
cyl);
return (-1);
}
(void) printf(
"Maximum size for partition is %u cylinders"
"\nretry the operation.",
return (-1);
}
}
/* Verify partition doesn't exceed disk size or 2 TB */
if (Numcyl > Numcyl_usable) {
(void) printf(
"Maximum size for partition is %d "
"cylinders; \nretry the operation.",
Numcyl_usable - cyl);
} else {
(void) printf(
"Maximum size for partition is %d "
"cylinders; \nretry the operation.",
Numcyl_usable - cyl);
}
return (-1);
}
/* Verify DOS12 partition doesn't exceed max size of 32MB. */
(void) printf(
"Maximum size for a %s partition is %ld cylinders;"
"\nretry the operation.",
return (-1);
}
i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
if (i < 0)
return (-1);
if (verify_tbl() < 0) {
(void) printf("fdisk: Cannot create partition table\n");
return (-1);
}
return (i);
}
}
/*
* dispmenu
* Display command menu (interactive mode).
*/
static void
dispmenu(void)
{
(void) printf(
"SELECT ONE OF THE FOLLOWING:\n"
" 1. Create a partition\n"
" 2. Specify the active partition\n"
" 3. Delete a partition\n"
" 4. Change between Solaris and Solaris2 Partition IDs\n"
" 5. Exit (update disk configuration and exit)\n"
" 6. Cancel (exit without updating disk configuration)\n");
}
/*
* pchange
* Change the ACTIVE designation of a partition.
*/
static int
pchange(void)
{
char s[80];
int i, j;
for (;;) {
{
(void) printf(
"Specify the partition number to boot from"
" (or specify 0 for none): ");
}
(void) gets(s);
rm_blanks(s);
if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) {
(void) printf(
"Invalid response, please specify a number"
" between 0 and 4.\n");
} else {
break;
}
}
if (s[0] == '0') { /* No active partitions */
for (i = 0; i < FD_NUMPART; i++) {
}
(void) printf(
"No partition is currently marked as active.");
return (0);
} else { /* User has selected a partition to be active */
i = s[0] - '1';
(void) printf("Partition does not exist.");
return (-1);
}
/* a DOS-DATA or EXT-DOS partition cannot be active */
(void) printf(
"DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
"cannot be made active.\n");
(void) printf("Select another partition.");
return (-1);
}
for (j = 0; j < FD_NUMPART; j++) {
if (j != i)
}
}
{
(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("Specify the partition number to change"
" (or enter 0 to exit): ");
return (1);
i = strtol(s, &p, 10);
if (*p != '\n' || i < 0 || i > FD_NUMPART) {
(void) printf(
"Invalid response, retry the operation.\n");
continue;
}
if (i == 0) {
/* exit delete command */
return (1);
}
i -= 1;
} else {
(void) printf(
"Partition %d is not a Solaris partition.",
i + 1);
continue;
}
return (1);
}
}
/*
* pdelete
* Remove partition entry from the table (interactive mode).
*/
static char
pdelete(void)
{
char s[80];
int i, j;
char pactive;
(void) printf("Specify the partition number to delete"
" (or enter 0 to exit): ");
(void) gets(s);
rm_blanks(s);
if ((s[0] == '0')) { /* exit delete command */
return (1);
}
/* Accept only a single digit between 1 and 4 */
(void) printf("Invalid response, retry the operation.\n");
goto DEL1;
} else { /* Found a digit between 1 and 4 */
--i; /* Structure begins with element 0 */
}
return (-1);
}
(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\"). ");
if (! yesno()) {
return (1);
}
pactive = 1;
} else {
pactive = 0;
}
for (j = i; j < FD_NUMPART - 1; j++) {
}
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) gets(s);
rm_blanks(s);
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;
int is_pmbr = 0;
(void) printf("WARNING: critical disk geometry information"
" missing!\n");
exit(1);
}
(void) printf(" Cylinder size is %d (512 byte) blocks\n\n",
(void) printf(
" Cylinders\n");
(void) printf(
" Partition Status Type Start End Length"
" %%\n");
(void) printf(
" ========= ====== ============ ===== === ======"
" ===");
for (i = 0; i < FD_NUMPART; i++) {
(void) printf("\n");
continue;
}
else
case UNIXOS:
break;
case SUNIXOS:
break;
case SUNIXOS2:
break;
case X86BOOT:
break;
case DOSOS12:
break;
case DOSOS16:
break;
case EXTDOS:
break;
case DOSDATA:
break;
case DOSHUGE:
break;
case PCIXOS:
break;
case DIAGPART:
break;
case FDISK_IFS:
break;
case FDISK_AIXBOOT:
break;
case FDISK_AIXDATA:
break;
case FDISK_OS2BOOT:
break;
case FDISK_WINDOWS:
break;
case FDISK_EXT_WIN:
break;
case FDISK_FAT95:
break;
case FDISK_EXTLBA:
break;
case FDISK_LINUX:
break;
case FDISK_CPM:
break;
case FDISK_NOVELL3:
break;
case FDISK_QNX4:
break;
case FDISK_QNX42:
break;
case FDISK_QNX43:
break;
case FDISK_LINUXNAT:
break;
case FDISK_NTFSVOL1:
type = NTFSVOL1str;
break;
case FDISK_NTFSVOL2:
type = NTFSVOL2str;
break;
case FDISK_BSD:
break;
case FDISK_NEXTSTEP:
type = NEXTSTEPstr;
break;
case FDISK_BSDIFS:
break;
case FDISK_BSDISWAP:
type = BSDISWAPstr;
break;
case EFI_PMBR:
is_pmbr = 1;
break;
default:
break;
}
} else {
length++;
}
/* 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",
}
/* Print warning message if table is empty */
(void) printf("WARNING: no partitions are defined!");
} else {
/* Clear the warning line */
/* Print warning if disk > 2TB and is not EFI PMBR */
(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;
" SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT"
" NUMSECT\n");
for (i = 0; i < FD_NUMPART; i++) {
}
}
/*
* 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++) {
}
}
/*
* 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++) {
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;
/* Get an aligned copy of the partition tables */
/* Signature is missing */
nulltbl();
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++) {
/* Null entry */
} else {
j++;
}
}
for (i = j; i < FD_NUMPART; i++) {
}
/* For now, always replace the bootcode with ours */
}
/*
* fill_ipart
* Initialize ipart structure values.
*/
static void
{
#ifdef sparc
/* Packing struct ipart for Sparc */
#else
#endif
}
/*
* getbyte, getlong
* Get a byte, a short, or a long (SPARC only).
*/
#ifdef sparc
{
uchar_t b;
return (b);
}
{
*bp += 2;
*bp += 2;
return ((uint32_t)b);
}
#endif
/*
* copy_Table_to_Bootblk
* Copy the table into the 512 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)
{
else
}
}
/*
* 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++) {
/* 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;
/*
* If file isn't standard output, then it's a file name.
* Open file and write it.
*/
"fdisk: Cannot open output file %s.\n",
file);
exit(1);
}
}
else
/*
* Write the fdisk table information
*/
/* Write virtual (HBA) geometry, if required */
if (v_flag) {
}
"\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl"
" Rsect Numsect\n");
for (i = 0; i < FD_NUMPART; i++) {
" %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
" %-10u\n",
0xc0) << 2)),
0xc0) << 2)),
}
}
/*
* 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;
if (io_image) {
return;
}
for (i = 0; i < FD_NUMPART; i++) {
/*
* Only the size matters (not starting point), since
* VTOC entries are relative to the start of
* the partition.
*/
break;
}
}
if (i >= FD_NUMPART) {
if (!io_nifdisk) {
"fdisk: No Solaris partition found - VTOC not"
" checked.\n");
}
return;
}
}
for (i = 0; i < V_NUMPAR; i++) {
/* Special case for slice two (entire disk) */
if (i == 2) {
"slice %d starts at %llu, is not at"
" start of partition",
if (!io_nifdisk) {
(void) printf(" adjust ?:");
if (yesno())
} else {
}
}
"slice %d size %llu does not cover"
" complete partition",
if (!io_nifdisk) {
(void) printf(" adjust ?:");
if (yesno())
} else {
}
}
"slice %d tag was %d should be %d",
V_BACKUP);
if (!io_nifdisk) {
(void) printf(" fix ?:");
if (yesno())
} else {
}
}
continue;
}
if (io_ADJT) {
"slice %d (start %llu, end %llu)"
" is larger than the partition",
if (!io_nifdisk) {
(void) printf(" remove ?:");
if (yesno()) {
}
} else {
" removed!\n");
}
}
continue;
}
"slice %d (start %llu) is larger than the "
if (!io_nifdisk) {
(void) printf(" remove ?:");
if (yesno()) {
}
} else {
" removed!\n");
}
"slice %d (end %llu) is larger"
" than the partition",
i,
if (!io_nifdisk) {
(void) printf(" adjust ?:");
if (yesno()) {
}
} else {
}
}
}
#if 1 /* bh for now */
/* Make the VTOC look sane - ha ha */
if (disk_vtoc.v_sectorsz == 0)
#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) gets(s);
rm_blanks(s);
if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) {
(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;
if (i == VT_EINVAL) {
vt_inval++;
retval = VTOC_INVAL;
} else if (i == VT_ENOTSUP) {
"GPT\n");
} else {
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 == VT_EINVAL) {
"fdisk: Invalid entry exists in VTOC.\n");
retval = VTOC_INVAL;
} else if (i == VT_ENOTSUP) {
"GPT\n");
} else {
retval = VTOC_RWERR;
}
}
return (retval);
}
/*
* efi_ioctl
* issues DKIOCSETEFI IOCTL
* (duplicate of private efi_ioctl() in rdwr_efi.c
*/
static int
{
int error;
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)
{
/*
* see if we can read the EFI label
*/
return (VT_ERROR);
}
/*
* set up the dk_ioc structure for writing
*/
return (VT_ERROR);
}
/*
* clear the primary label
*/
if (io_debug) {
"\tClearing primary EFI label at block %lld\n",
}
switch (errno) {
case EIO:
return (VT_EIO);
case EINVAL:
return (VT_EINVAL);
default:
return (VT_ERROR);
}
}
/*
* clear the backup partition table
*/
if (io_debug) {
"\tClearing backup partition table at block %lld\n",
}
errno);
}
/*
* clear the backup label
*/
if (io_debug) {
}
"\tUnable to clear backup EFI label at "
"block %llu; errno %d\n",
errno);
}
return (0);
}
/*
* clear_vtoc
* Clear the VTOC from the current or previous Solaris partition on the
* device.
*/
static void
{
struct dk_label disk_label;
#ifdef DEBUG
struct dk_label read_label;
#endif /* DEBUG */
} else {
}
if (io_debug) {
"\tClearing primary VTOC at byte %llu (block %llu)\n",
}
"\tError seeking to primary label at byte %llu\n",
return;
}
"\tWarning: only %d bytes written to clear primary"
" VTOC!\n", bytes);
}
#ifdef DEBUG
"DEBUG: Error seeking to primary label at byte %llu\n",
return;
} else {
"DEBUG: Successful lseek() to byte %llu\n",
}
"DEBUG: Warning: only %d bytes read of label\n",
bytes);
}
"DEBUG: Warning: disk_label and read_label differ!!!\n");
} else {
"read_label\n");
}
#endif /* DEBUG */
/* Clear backup label */
"\tError seeking to backup label at byte %llu on "
return;
}
if (io_debug) {
" byte %llu (block %llu)\n",
}
"\t\tWarning: only %d bytes written to "
"clear backup VTOC at block %llu!\n", bytes,
}
#ifdef DEBUG
"DEBUG: Error seeking to backup label at byte %llu\n",
return;
} else {
"DEBUG: Successful lseek() to byte %llu\n",
}
"DEBUG: Warning: only %d bytes read of backup label\n",
bytes);
}
"DEBUG: Warning: disk_label and read_label differ!!!\n");
} else {
"DEBUG: Good compare of disk_label and backup "
"read_label\n");
}
#endif /* DEBUG */
backup_block += 2;
}
}
#define FDISK_STANDARD_LECTURE \
"Fdisk is normally used with the device that " \
"represents the entire fixed disk.\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
{
if (io_nifdisk)
return (0);
return (yesno());
}
static void
{
struct extvtoc v;
struct dk_geom d;
struct extpart_info extpi;
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.
*/
devname)) {
exit(1);
}
}
devname)) {
exit(1);
}
}
} else {
devname)) {
exit(1);
}
return;
}
devname)) {
exit(1);
}
return;
}
devname)) {
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
*/
static char *
{
char *node;
/* Don't do anything if we are skipping device checks */
if (io_image)
return (devname);
/* Try the node as provided first */
/*
* Copy the passed in string to a new buffer, prepend the
* default path and try again.
*/
"for device node.\n");
exit(1);
}
/* Copy over the default path and the provided node */
/* Try to stat it again */
/* Failed all options, give up */
"fdisk: Cannot stat device %s.\n",
devname);
exit(1);
}
}
/* Make sure the device specified is the raw device */
"fdisk: %s must be a raw device.\n", node);
exit(1);
}
return (node);
}