17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53f2f09c1Sdp * Common Development and Distribution License (the "License").
63f2f09c1Sdp * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
227c478bd9Sstevel@tonic-gate /* All Rights Reserved */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate *
277c478bd9Sstevel@tonic-gate * Portions of this source code were provided by International
287c478bd9Sstevel@tonic-gate * Computers Limited (ICL) under a development agreement with AT&T.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
32*65908c77Syu, larry liu - Sun Microsystems - Beijing China * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
337c478bd9Sstevel@tonic-gate * Use is subject to license terms.
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * Sun Microsystems version of fmthard:
387c478bd9Sstevel@tonic-gate *
397c478bd9Sstevel@tonic-gate * Supports the following arguments:
407c478bd9Sstevel@tonic-gate *
417c478bd9Sstevel@tonic-gate * -i Writes VTOC to stdout, rather than disk
427c478bd9Sstevel@tonic-gate * -q Quick check: exit code 0 if VTOC ok
437c478bd9Sstevel@tonic-gate * -d <data> Incremental changes to the VTOC
447c478bd9Sstevel@tonic-gate * -n <vname> Change volume name to <vname>
457c478bd9Sstevel@tonic-gate * -s <file> Read VTOC information from <file>, or stdin ("-")
467c478bd9Sstevel@tonic-gate * -u <state> Reboot after writing VTOC, according to <state>:
477c478bd9Sstevel@tonic-gate * boot: AD_BOOT (standard reboot)
487c478bd9Sstevel@tonic-gate * firm: AD_IBOOT (interactive reboot)
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate * Note that fmthard cannot write a VTOC on an unlabeled disk.
517c478bd9Sstevel@tonic-gate * You must use format or SunInstall for this purpose.
527c478bd9Sstevel@tonic-gate * (NOTE: the above restriction only applies on Sparc systems).
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * The primary motivation for fmthard is to duplicate the
557c478bd9Sstevel@tonic-gate * partitioning from disk to disk:
567c478bd9Sstevel@tonic-gate *
577c478bd9Sstevel@tonic-gate * prtvtoc /dev/rdsk/c0t0d0s2 | fmthard -s - /dev/rdsk/c0t1d0s2
587c478bd9Sstevel@tonic-gate */
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #include <stdio.h>
617c478bd9Sstevel@tonic-gate #include <fcntl.h>
627c478bd9Sstevel@tonic-gate #include <errno.h>
637c478bd9Sstevel@tonic-gate #include <string.h>
647c478bd9Sstevel@tonic-gate #include <stdlib.h>
657c478bd9Sstevel@tonic-gate #include <unistd.h>
667c478bd9Sstevel@tonic-gate #include <sys/types.h>
677c478bd9Sstevel@tonic-gate #include <sys/param.h>
68342440ecSPrasad Singamsetty #include <sys/int_limits.h>
697c478bd9Sstevel@tonic-gate #include <sys/stat.h>
707c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
717c478bd9Sstevel@tonic-gate #include <sys/open.h>
727c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
737c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
747c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
757c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
787c478bd9Sstevel@tonic-gate #include <sys/dklabel.h>
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate #ifndef SECSIZE
847c478bd9Sstevel@tonic-gate #define SECSIZE DEV_BSIZE
857c478bd9Sstevel@tonic-gate #endif /* SECSIZE */
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * Internal functions.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate extern int main(int, char **);
91342440ecSPrasad Singamsetty static void display(struct dk_geom *, struct extvtoc *, char *);
927c478bd9Sstevel@tonic-gate static void display64(struct dk_gpt *, char *);
93342440ecSPrasad Singamsetty static void insert(char *, struct extvtoc *);
947c478bd9Sstevel@tonic-gate static void insert64(char *, struct dk_gpt *);
95342440ecSPrasad Singamsetty static void load(FILE *, struct dk_geom *, struct extvtoc *);
967c478bd9Sstevel@tonic-gate static void load64(FILE *, int fd, struct dk_gpt **);
977c478bd9Sstevel@tonic-gate static void usage(void);
98342440ecSPrasad Singamsetty static void validate(struct dk_geom *, struct extvtoc *);
997c478bd9Sstevel@tonic-gate static void validate64(struct dk_gpt *);
100342440ecSPrasad Singamsetty static int vread(int, struct extvtoc *, char *);
1017c478bd9Sstevel@tonic-gate static void vread64(int, struct dk_gpt **, char *);
102342440ecSPrasad Singamsetty static void vwrite(int, struct extvtoc *, char *);
1037c478bd9Sstevel@tonic-gate static void vwrite64(int, struct dk_gpt *, char *);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * Static variables.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate static char *delta; /* Incremental update */
1097c478bd9Sstevel@tonic-gate static short eflag; /* force write of an EFI label */
1107c478bd9Sstevel@tonic-gate static short iflag; /* Prints VTOC w/o updating */
1117c478bd9Sstevel@tonic-gate static short qflag; /* Check for a formatted disk */
1127c478bd9Sstevel@tonic-gate static short uflag; /* Exit to firmware after writing */
1137c478bd9Sstevel@tonic-gate /* new vtoc and reboot. Used during */
1147c478bd9Sstevel@tonic-gate /* installation of core floppies */
1157c478bd9Sstevel@tonic-gate static diskaddr_t lastlba = 0; /* last LBA on 64-bit VTOC */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate #if defined(sparc)
1187c478bd9Sstevel@tonic-gate static char *uboot = "boot";
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate #elif defined(i386)
1217c478bd9Sstevel@tonic-gate /* use installgrub(1M) to install boot blocks */
1227c478bd9Sstevel@tonic-gate static char *uboot = "";
1237c478bd9Sstevel@tonic-gate #else
1247c478bd9Sstevel@tonic-gate #error No platform defined.
1257c478bd9Sstevel@tonic-gate #endif /* various platform-specific definitions */
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate static char *ufirm = "firm";
1287c478bd9Sstevel@tonic-gate static int sectsiz;
129*65908c77Syu, larry liu - Sun Microsystems - Beijing China #if defined(_SUNOS_VTOC_16)
130342440ecSPrasad Singamsetty static struct extvtoc disk_vtoc;
1317c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1347c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate int fd;
1377c478bd9Sstevel@tonic-gate int c;
1387c478bd9Sstevel@tonic-gate char *dfile;
1397c478bd9Sstevel@tonic-gate char *vname;
1407c478bd9Sstevel@tonic-gate struct stat statbuf;
1417c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_8)
142342440ecSPrasad Singamsetty struct extvtoc disk_vtoc;
1437c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_8) */
1447c478bd9Sstevel@tonic-gate struct dk_gpt *disk_efi;
1457c478bd9Sstevel@tonic-gate struct dk_geom disk_geom;
146*65908c77Syu, larry liu - Sun Microsystems - Beijing China struct dk_minfo minfo;
1477c478bd9Sstevel@tonic-gate int n;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate
1502f7fafa7Szl149053 disk_efi = NULL;
1517c478bd9Sstevel@tonic-gate dfile = NULL;
1527c478bd9Sstevel@tonic-gate vname = NULL;
1537c478bd9Sstevel@tonic-gate #if defined(sparc)
1547c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ed:u:in:qs:")) != EOF)
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate #elif defined(i386)
1577c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ed:u:in:qb:p:s:")) != EOF)
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate #else
1607c478bd9Sstevel@tonic-gate #error No platform defined.
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate switch (c) {
1637c478bd9Sstevel@tonic-gate #if defined(i386)
1647c478bd9Sstevel@tonic-gate case 'p':
1657c478bd9Sstevel@tonic-gate case 'b':
1667c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1677c478bd9Sstevel@tonic-gate "fmthard: -p and -b no longer supported."
1687c478bd9Sstevel@tonic-gate " Use installgrub(1M) to install boot blocks\n");
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate #endif /* defined(i386) */
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate case 'd':
1737c478bd9Sstevel@tonic-gate delta = optarg;
1747c478bd9Sstevel@tonic-gate break;
1757c478bd9Sstevel@tonic-gate case 'e':
1767c478bd9Sstevel@tonic-gate ++eflag;
1777c478bd9Sstevel@tonic-gate break;
1787c478bd9Sstevel@tonic-gate case 'i':
1797c478bd9Sstevel@tonic-gate ++iflag;
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate case 'n':
1827c478bd9Sstevel@tonic-gate vname = optarg;
1837c478bd9Sstevel@tonic-gate break;
1847c478bd9Sstevel@tonic-gate case 'q':
1857c478bd9Sstevel@tonic-gate ++qflag;
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate case 's':
1887c478bd9Sstevel@tonic-gate dfile = optarg;
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate case 'u':
1917c478bd9Sstevel@tonic-gate if (strcmp(uboot, optarg) == 0)
1927c478bd9Sstevel@tonic-gate ++uflag;
1937c478bd9Sstevel@tonic-gate else if (strcmp(ufirm, optarg) == 0)
1947c478bd9Sstevel@tonic-gate uflag = 2;
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate default:
1987c478bd9Sstevel@tonic-gate usage();
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate if (argc - optind != 1)
2037c478bd9Sstevel@tonic-gate usage();
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate if (stat(argv[optind], (struct stat *)&statbuf) == -1) {
2067c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2077c478bd9Sstevel@tonic-gate "fmthard: Cannot stat device %s\n",
2087c478bd9Sstevel@tonic-gate argv[optind]);
2097c478bd9Sstevel@tonic-gate exit(1);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
2137c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2147c478bd9Sstevel@tonic-gate "fmthard: %s must be a raw device.\n",
2157c478bd9Sstevel@tonic-gate argv[optind]);
2167c478bd9Sstevel@tonic-gate exit(1);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate if ((fd = open(argv[optind], O_RDWR|O_NDELAY)) < 0) {
2207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "fmthard: Cannot open device %s - %s\n",
221ace1a5f1Sdp argv[optind], strerror(errno));
2227c478bd9Sstevel@tonic-gate exit(1);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
225*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (ioctl(fd, DKIOCGMEDIAINFO, &minfo) == 0) {
226*65908c77Syu, larry liu - Sun Microsystems - Beijing China sectsiz = minfo.dki_lbsize;
227*65908c77Syu, larry liu - Sun Microsystems - Beijing China }
228*65908c77Syu, larry liu - Sun Microsystems - Beijing China
229*65908c77Syu, larry liu - Sun Microsystems - Beijing China if (sectsiz == 0) {
230*65908c77Syu, larry liu - Sun Microsystems - Beijing China sectsiz = SECSIZE;
231*65908c77Syu, larry liu - Sun Microsystems - Beijing China }
232*65908c77Syu, larry liu - Sun Microsystems - Beijing China
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * Get the geometry information for this disk from the driver
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate if (!eflag && ioctl(fd, DKIOCGGEOM, &disk_geom)) {
2377c478bd9Sstevel@tonic-gate #ifdef DEBUG
2387c478bd9Sstevel@tonic-gate perror("DKIOCGGEOM failed");
2397c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2407c478bd9Sstevel@tonic-gate if (errno == ENOTSUP) {
2417c478bd9Sstevel@tonic-gate /* disk has EFI labels */
2427c478bd9Sstevel@tonic-gate eflag++;
2437c478bd9Sstevel@tonic-gate } else {
2447c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2457c478bd9Sstevel@tonic-gate "%s: Cannot get disk geometry\n", argv[optind]);
2467c478bd9Sstevel@tonic-gate (void) close(fd);
2477c478bd9Sstevel@tonic-gate exit(1);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * Read the vtoc on the disk
2537c478bd9Sstevel@tonic-gate */
2547c478bd9Sstevel@tonic-gate if (!eflag) {
2557c478bd9Sstevel@tonic-gate if (vread(fd, &disk_vtoc, argv[optind]) == 1)
2567c478bd9Sstevel@tonic-gate eflag++;
2577c478bd9Sstevel@tonic-gate }
2582f7fafa7Szl149053 if (eflag && ((dfile == NULL) || qflag)) {
2597c478bd9Sstevel@tonic-gate vread64(fd, &disk_efi, argv[optind]);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate * Quick check for valid disk: 0 if ok, 1 if not
2647c478bd9Sstevel@tonic-gate */
2657c478bd9Sstevel@tonic-gate if (qflag) {
2667c478bd9Sstevel@tonic-gate (void) close(fd);
2677c478bd9Sstevel@tonic-gate if (!eflag) {
2687c478bd9Sstevel@tonic-gate exit(disk_vtoc.v_sanity == VTOC_SANE ? 0 : 1);
2697c478bd9Sstevel@tonic-gate } else {
2707c478bd9Sstevel@tonic-gate exit(disk_efi->efi_version <= EFI_VERSION102 ? 0 : 1);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * Incremental changes to the VTOC
2767c478bd9Sstevel@tonic-gate */
2777c478bd9Sstevel@tonic-gate if (delta) {
2787c478bd9Sstevel@tonic-gate if (!eflag) {
2797c478bd9Sstevel@tonic-gate insert(delta, &disk_vtoc);
2807c478bd9Sstevel@tonic-gate validate(&disk_geom, &disk_vtoc);
2817c478bd9Sstevel@tonic-gate vwrite(fd, &disk_vtoc, argv[optind]);
2827c478bd9Sstevel@tonic-gate } else {
2837c478bd9Sstevel@tonic-gate insert64(delta, disk_efi);
2847c478bd9Sstevel@tonic-gate validate64(disk_efi);
2857c478bd9Sstevel@tonic-gate vwrite64(fd, disk_efi, argv[optind]);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate (void) close(fd);
2887c478bd9Sstevel@tonic-gate exit(0);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate if (!dfile && !vname)
2927c478bd9Sstevel@tonic-gate usage();
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate * Read new VTOC from stdin or data file
2967c478bd9Sstevel@tonic-gate */
2977c478bd9Sstevel@tonic-gate if (dfile) {
2987c478bd9Sstevel@tonic-gate if (strcmp(dfile, "-") == 0) {
2997c478bd9Sstevel@tonic-gate if (!eflag)
3007c478bd9Sstevel@tonic-gate load(stdin, &disk_geom, &disk_vtoc);
3017c478bd9Sstevel@tonic-gate else
3027c478bd9Sstevel@tonic-gate load64(stdin, fd, &disk_efi);
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate FILE *fp;
3057c478bd9Sstevel@tonic-gate if ((fp = fopen(dfile, "r")) == NULL) {
3067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Cannot open file %s\n",
3077c478bd9Sstevel@tonic-gate dfile);
3087c478bd9Sstevel@tonic-gate (void) close(fd);
3097c478bd9Sstevel@tonic-gate exit(1);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate if (!eflag)
3127c478bd9Sstevel@tonic-gate load(fp, &disk_geom, &disk_vtoc);
3137c478bd9Sstevel@tonic-gate else
3147c478bd9Sstevel@tonic-gate load64(fp, fd, &disk_efi);
3157c478bd9Sstevel@tonic-gate (void) fclose(fp);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate * Print the modified VTOC, rather than updating the disk
3217c478bd9Sstevel@tonic-gate */
3227c478bd9Sstevel@tonic-gate if (iflag) {
3237c478bd9Sstevel@tonic-gate if (!eflag)
3247c478bd9Sstevel@tonic-gate display(&disk_geom, &disk_vtoc, argv[optind]);
3257c478bd9Sstevel@tonic-gate else
3267c478bd9Sstevel@tonic-gate display64(disk_efi, argv[optind]);
3277c478bd9Sstevel@tonic-gate (void) close(fd);
3287c478bd9Sstevel@tonic-gate exit(0);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (vname) {
3327c478bd9Sstevel@tonic-gate n = MIN(strlen(vname) + 1, LEN_DKL_VVOL);
3337c478bd9Sstevel@tonic-gate if (!eflag) {
3347c478bd9Sstevel@tonic-gate (void) memcpy(disk_vtoc.v_volume, vname, n);
3357c478bd9Sstevel@tonic-gate } else {
3367c478bd9Sstevel@tonic-gate for (c = 0; c < disk_efi->efi_nparts; c++) {
3377c478bd9Sstevel@tonic-gate if (disk_efi->efi_parts[c].p_tag ==
3387c478bd9Sstevel@tonic-gate V_RESERVED) {
3397c478bd9Sstevel@tonic-gate (void) memcpy(&disk_efi->efi_parts[c].p_name,
3407c478bd9Sstevel@tonic-gate vname, n);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * Write the new VTOC on the disk
3487c478bd9Sstevel@tonic-gate */
3497c478bd9Sstevel@tonic-gate if (!eflag) {
3507c478bd9Sstevel@tonic-gate validate(&disk_geom, &disk_vtoc);
3517c478bd9Sstevel@tonic-gate vwrite(fd, &disk_vtoc, argv[optind]);
3527c478bd9Sstevel@tonic-gate } else {
3537c478bd9Sstevel@tonic-gate validate64(disk_efi);
3547c478bd9Sstevel@tonic-gate vwrite64(fd, disk_efi, argv[optind]);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * Shut system down after writing a new vtoc to disk
3597c478bd9Sstevel@tonic-gate * This is used during installation of core floppies.
3607c478bd9Sstevel@tonic-gate */
3617c478bd9Sstevel@tonic-gate if (uflag == 1)
3623f2f09c1Sdp (void) uadmin(A_REBOOT, AD_BOOT, 0);
3637c478bd9Sstevel@tonic-gate else if (uflag == 2)
3643f2f09c1Sdp (void) uadmin(A_REBOOT, AD_IBOOT, 0);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate (void) printf("fmthard: New volume table of contents now in place.\n");
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate return (0);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate * display ()
3757c478bd9Sstevel@tonic-gate *
3767c478bd9Sstevel@tonic-gate * display contents of VTOC without writing it to disk
3777c478bd9Sstevel@tonic-gate */
3787c478bd9Sstevel@tonic-gate static void
display(struct dk_geom * geom,struct extvtoc * vtoc,char * device)379342440ecSPrasad Singamsetty display(struct dk_geom *geom, struct extvtoc *vtoc, char *device)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate int i;
3827c478bd9Sstevel@tonic-gate int c;
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate * Print out the VTOC
3867c478bd9Sstevel@tonic-gate */
3877c478bd9Sstevel@tonic-gate (void) printf("* %s default partition map\n", device);
3887c478bd9Sstevel@tonic-gate if (*vtoc->v_volume) {
3897c478bd9Sstevel@tonic-gate (void) printf("* Volume Name: ");
3907c478bd9Sstevel@tonic-gate for (i = 0; i < LEN_DKL_VVOL; i++) {
3917c478bd9Sstevel@tonic-gate if ((c = vtoc->v_volume[i]) == 0)
3927c478bd9Sstevel@tonic-gate break;
3937c478bd9Sstevel@tonic-gate (void) printf("%c", c);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate (void) printf("\n");
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate (void) printf("*\n");
3987c478bd9Sstevel@tonic-gate (void) printf("* Dimensions:\n");
399*65908c77Syu, larry liu - Sun Microsystems - Beijing China (void) printf("* %d bytes/sector\n", sectsiz);
4007c478bd9Sstevel@tonic-gate (void) printf("* %d sectors/track\n", geom->dkg_nsect);
4017c478bd9Sstevel@tonic-gate (void) printf("* %d tracks/cylinder\n", geom->dkg_nhead);
4027c478bd9Sstevel@tonic-gate (void) printf("* %d cylinders\n", geom->dkg_pcyl);
4037c478bd9Sstevel@tonic-gate (void) printf("* %d accessible cylinders\n", geom->dkg_ncyl);
4047c478bd9Sstevel@tonic-gate (void) printf("*\n");
4057c478bd9Sstevel@tonic-gate (void) printf("* Flags:\n");
4067c478bd9Sstevel@tonic-gate (void) printf("* 1: unmountable\n");
4077c478bd9Sstevel@tonic-gate (void) printf("* 10: read-only\n");
4087c478bd9Sstevel@tonic-gate (void) printf("*\n");
4097c478bd9Sstevel@tonic-gate (void) printf(
4107c478bd9Sstevel@tonic-gate "\n* Partition Tag Flag First Sector Sector Count\n");
4117c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) {
4127c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size > 0)
4137c478bd9Sstevel@tonic-gate (void) printf(
414342440ecSPrasad Singamsetty " %d %d 0%x %llu %llu\n",
4157c478bd9Sstevel@tonic-gate i, vtoc->v_part[i].p_tag,
4167c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_flag,
4177c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start,
4187c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate exit(0);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate * display64 ()
4257c478bd9Sstevel@tonic-gate *
4267c478bd9Sstevel@tonic-gate * display64 contents of EFI partition without writing it to disk
4277c478bd9Sstevel@tonic-gate */
4287c478bd9Sstevel@tonic-gate static void
display64(struct dk_gpt * efi,char * device)4297c478bd9Sstevel@tonic-gate display64(struct dk_gpt *efi, char *device)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate int i;
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate * Print out the VTOC
4357c478bd9Sstevel@tonic-gate */
4367c478bd9Sstevel@tonic-gate (void) printf("* %s default partition map\n", device);
4377c478bd9Sstevel@tonic-gate (void) printf("*\n");
4387c478bd9Sstevel@tonic-gate (void) printf("* Dimensions:\n");
4397c478bd9Sstevel@tonic-gate (void) printf("* %d bytes/sector\n", efi->efi_lbasize);
4407c478bd9Sstevel@tonic-gate (void) printf("* N/A sectors/track\n");
4417c478bd9Sstevel@tonic-gate (void) printf("* N/A tracks/cylinder\n");
4427c478bd9Sstevel@tonic-gate (void) printf("* N/A cylinders\n");
4437c478bd9Sstevel@tonic-gate (void) printf("* N/A accessible cylinders\n");
4447c478bd9Sstevel@tonic-gate (void) printf("*\n");
4457c478bd9Sstevel@tonic-gate (void) printf("* Flags:\n");
4467c478bd9Sstevel@tonic-gate (void) printf("* 1: unmountable\n");
4477c478bd9Sstevel@tonic-gate (void) printf("* 10: read-only\n");
4487c478bd9Sstevel@tonic-gate (void) printf("*\n");
4497c478bd9Sstevel@tonic-gate (void) printf(
4507c478bd9Sstevel@tonic-gate "\n* Partition Tag Flag First Sector Sector Count\n");
4517c478bd9Sstevel@tonic-gate for (i = 0; i < efi->efi_nparts; i++) {
4527c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_size > 0)
4537c478bd9Sstevel@tonic-gate (void) printf(
4547c478bd9Sstevel@tonic-gate " %d %d 0%x %8lld %8lld\n",
4557c478bd9Sstevel@tonic-gate i, efi->efi_parts[i].p_tag,
4567c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_flag,
4577c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start,
4587c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate exit(0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate * insert()
4667c478bd9Sstevel@tonic-gate *
4677c478bd9Sstevel@tonic-gate * Insert a change into the VTOC.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate static void
insert(char * data,struct extvtoc * vtoc)470342440ecSPrasad Singamsetty insert(char *data, struct extvtoc *vtoc)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate int part;
4737c478bd9Sstevel@tonic-gate int tag;
4747c478bd9Sstevel@tonic-gate uint_t flag;
475342440ecSPrasad Singamsetty diskaddr_t start;
476342440ecSPrasad Singamsetty uint64_t size;
4777c478bd9Sstevel@tonic-gate
478342440ecSPrasad Singamsetty if (sscanf(data, "%d:%d:%x:%llu:%llu",
4797c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) {
4807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data);
4817c478bd9Sstevel@tonic-gate exit(1);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate if (part >= V_NUMPAR) {
4847c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
4857c478bd9Sstevel@tonic-gate "Error in data \"%s\": No such partition %x\n",
4867c478bd9Sstevel@tonic-gate data, part);
4877c478bd9Sstevel@tonic-gate exit(1);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_tag = (ushort_t)tag;
4907c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_flag = (ushort_t)flag;
4917c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_start = start;
4927c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_size = size;
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate * insert64()
4977c478bd9Sstevel@tonic-gate *
4987c478bd9Sstevel@tonic-gate * Insert a change into the VTOC.
4997c478bd9Sstevel@tonic-gate */
5007c478bd9Sstevel@tonic-gate static void
insert64(char * data,struct dk_gpt * efi)5017c478bd9Sstevel@tonic-gate insert64(char *data, struct dk_gpt *efi)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate int part;
5047c478bd9Sstevel@tonic-gate int tag;
5057c478bd9Sstevel@tonic-gate uint_t flag;
5067c478bd9Sstevel@tonic-gate diskaddr_t start;
5077c478bd9Sstevel@tonic-gate diskaddr_t size;
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate if (sscanf(data, "%d:%d:%x:%lld:%lld",
5107c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) {
5117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data);
5127c478bd9Sstevel@tonic-gate exit(1);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate if (part >= efi->efi_nparts) {
5157c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5167c478bd9Sstevel@tonic-gate "Error in data \"%s\": No such partition %x\n",
5177c478bd9Sstevel@tonic-gate data, part);
5187c478bd9Sstevel@tonic-gate exit(1);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_tag = (ushort_t)tag;
5217c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_flag = (ushort_t)flag;
5227c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_start = start;
5237c478bd9Sstevel@tonic-gate efi->efi_parts[part].p_size = size;
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate * load()
5287c478bd9Sstevel@tonic-gate *
5297c478bd9Sstevel@tonic-gate * Load VTOC information from a datafile.
5307c478bd9Sstevel@tonic-gate */
5317c478bd9Sstevel@tonic-gate static void
load(FILE * fp,struct dk_geom * geom,struct extvtoc * vtoc)532342440ecSPrasad Singamsetty load(FILE *fp, struct dk_geom *geom, struct extvtoc *vtoc)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate int part;
5357c478bd9Sstevel@tonic-gate int tag;
5367c478bd9Sstevel@tonic-gate uint_t flag;
537342440ecSPrasad Singamsetty diskaddr_t start;
538342440ecSPrasad Singamsetty uint64_t size;
5397c478bd9Sstevel@tonic-gate char line[256];
5407c478bd9Sstevel@tonic-gate int i;
541342440ecSPrasad Singamsetty uint64_t nblks;
542342440ecSPrasad Singamsetty uint64_t fullsz;
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; ++i) {
5457c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_tag = 0;
5467c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_flag = V_UNMNT;
5477c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start = 0;
5487c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size = 0;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate * initialize partition 2, by convention it corresponds to whole
5527c478bd9Sstevel@tonic-gate * disk. It will be overwritten, if specified in the input datafile
5537c478bd9Sstevel@tonic-gate */
554342440ecSPrasad Singamsetty fullsz = (uint64_t)geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead;
5557c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_tag = V_BACKUP;
5567c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_flag = V_UNMNT;
5577c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_start = 0;
5587c478bd9Sstevel@tonic-gate vtoc->v_part[2].p_size = fullsz;
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate nblks = geom->dkg_nsect * geom->dkg_nhead;
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) {
5637c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
5647c478bd9Sstevel@tonic-gate continue;
5657c478bd9Sstevel@tonic-gate line[strlen(line) - 1] = '\0';
566342440ecSPrasad Singamsetty if (sscanf(line, "%d %d %x %llu %llu",
5677c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) {
5687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n",
5697c478bd9Sstevel@tonic-gate line);
5707c478bd9Sstevel@tonic-gate exit(1);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate if (part >= V_NUMPAR) {
5737c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5747c478bd9Sstevel@tonic-gate "No such partition %x: \"%s\"\n",
5757c478bd9Sstevel@tonic-gate part, line);
5767c478bd9Sstevel@tonic-gate exit(1);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate if (!eflag && ((start % nblks) != 0 || (size % nblks) != 0)) {
5797c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5807c478bd9Sstevel@tonic-gate "Partition %d not aligned on cylinder boundary: \"%s\"\n",
5817c478bd9Sstevel@tonic-gate part, line);
5827c478bd9Sstevel@tonic-gate exit(1);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_tag = (ushort_t)tag;
5857c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_flag = (ushort_t)flag;
5867c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_start = start;
5877c478bd9Sstevel@tonic-gate vtoc->v_part[part].p_size = size;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate for (part = 0; part < V_NUMPAR; part++) {
5907c478bd9Sstevel@tonic-gate vtoc->timestamp[part] = (time_t)0;
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate * load64()
5967c478bd9Sstevel@tonic-gate *
5977c478bd9Sstevel@tonic-gate * Load VTOC information from a datafile.
5987c478bd9Sstevel@tonic-gate */
5997c478bd9Sstevel@tonic-gate static void
load64(FILE * fp,int fd,struct dk_gpt ** efi)6007c478bd9Sstevel@tonic-gate load64(FILE *fp, int fd, struct dk_gpt **efi)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate int part;
6037c478bd9Sstevel@tonic-gate int tag;
6047c478bd9Sstevel@tonic-gate uint_t flag;
6057c478bd9Sstevel@tonic-gate diskaddr_t start;
6067c478bd9Sstevel@tonic-gate diskaddr_t size;
6077c478bd9Sstevel@tonic-gate int nlines = 0;
6087c478bd9Sstevel@tonic-gate char line[256];
6097c478bd9Sstevel@tonic-gate int i;
6107c478bd9Sstevel@tonic-gate uint_t max_part = 0;
6117c478bd9Sstevel@tonic-gate char **mem = NULL;
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) {
6147c478bd9Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
6157c478bd9Sstevel@tonic-gate continue;
6167c478bd9Sstevel@tonic-gate line[strlen(line) - 1] = '\0';
6177c478bd9Sstevel@tonic-gate if (sscanf(line, "%d %d %x %lld %lld",
6187c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) {
6197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n",
6207c478bd9Sstevel@tonic-gate line);
6217c478bd9Sstevel@tonic-gate exit(1);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate mem = realloc(mem, sizeof (*mem) * (nlines + 1));
6247c478bd9Sstevel@tonic-gate if (mem == NULL) {
6257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "realloc failed\n");
6267c478bd9Sstevel@tonic-gate exit(1);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate mem[nlines] = strdup(line);
6297c478bd9Sstevel@tonic-gate if (mem[nlines] == NULL) {
6307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "strdup failed\n");
6317c478bd9Sstevel@tonic-gate exit(1);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate nlines++;
6347c478bd9Sstevel@tonic-gate if (part > max_part)
6357c478bd9Sstevel@tonic-gate max_part = part;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate max_part++;
6387c478bd9Sstevel@tonic-gate
6397c478bd9Sstevel@tonic-gate if ((i = efi_alloc_and_init(fd, max_part, efi)) < 0) {
6407c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6417c478bd9Sstevel@tonic-gate "efi_alloc_and_init failed: %d\n", i);
6427c478bd9Sstevel@tonic-gate exit(1);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate for (i = 0; i < (*efi)->efi_nparts; ++i) {
6457c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_tag = V_UNASSIGNED;
6467c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_flag = V_UNMNT;
6477c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_start = 0;
6487c478bd9Sstevel@tonic-gate (*efi)->efi_parts[i].p_size = 0;
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate lastlba = (*efi)->efi_last_u_lba;
6517c478bd9Sstevel@tonic-gate
6527c478bd9Sstevel@tonic-gate for (i = 0; i < nlines; i++) {
6537c478bd9Sstevel@tonic-gate if (sscanf(mem[i], "%d %d %x %lld %lld",
6547c478bd9Sstevel@tonic-gate &part, &tag, &flag, &start, &size) != 5) {
6557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error: \"%s\"\n",
6567c478bd9Sstevel@tonic-gate line);
6577c478bd9Sstevel@tonic-gate exit(1);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate free(mem[i]);
6607c478bd9Sstevel@tonic-gate if (part >= (*efi)->efi_nparts) {
6617c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
6627c478bd9Sstevel@tonic-gate "No such partition %x: \"%s\"\n",
6637c478bd9Sstevel@tonic-gate part, line);
6647c478bd9Sstevel@tonic-gate exit(1);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_tag = (ushort_t)tag;
6677c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_flag = (ushort_t)flag;
6687c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_start = start;
6697c478bd9Sstevel@tonic-gate (*efi)->efi_parts[part].p_size = size;
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate (*efi)->efi_nparts = max_part;
6727c478bd9Sstevel@tonic-gate free(mem);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate static void
usage()6777c478bd9Sstevel@tonic-gate usage()
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate #if defined(sparc)
680*65908c77Syu, larry liu - Sun Microsystems - Beijing China (void) fprintf(stderr,
6817c478bd9Sstevel@tonic-gate "Usage: fmthard [ -i ] [ -n volumename ] [ -s datafile ] [ -d arguments] \
6827c478bd9Sstevel@tonic-gate raw-device\n");
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate #elif defined(i386)
685*65908c77Syu, larry liu - Sun Microsystems - Beijing China (void) fprintf(stderr,
6867c478bd9Sstevel@tonic-gate "Usage: fmthard [ -i ] [ -S ] [-I geom_file] \
6877c478bd9Sstevel@tonic-gate -n volumename | -s datafile [ -d arguments] raw-device\n");
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate #else
6907c478bd9Sstevel@tonic-gate #error No platform defined.
6917c478bd9Sstevel@tonic-gate #endif
6927c478bd9Sstevel@tonic-gate exit(2);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate /*
6967c478bd9Sstevel@tonic-gate * validate()
6977c478bd9Sstevel@tonic-gate *
6987c478bd9Sstevel@tonic-gate * Validate the new VTOC.
6997c478bd9Sstevel@tonic-gate */
7007c478bd9Sstevel@tonic-gate static void
validate(struct dk_geom * geom,struct extvtoc * vtoc)701342440ecSPrasad Singamsetty validate(struct dk_geom *geom, struct extvtoc *vtoc)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate int i;
7047c478bd9Sstevel@tonic-gate int j;
705342440ecSPrasad Singamsetty uint64_t fullsz;
706342440ecSPrasad Singamsetty diskaddr_t endsect;
707342440ecSPrasad Singamsetty diskaddr_t istart;
708342440ecSPrasad Singamsetty diskaddr_t jstart;
709342440ecSPrasad Singamsetty uint64_t isize;
710342440ecSPrasad Singamsetty uint64_t jsize;
711342440ecSPrasad Singamsetty uint64_t nblks;
7127c478bd9Sstevel@tonic-gate
7137c478bd9Sstevel@tonic-gate nblks = geom->dkg_nsect * geom->dkg_nhead;
7147c478bd9Sstevel@tonic-gate
715342440ecSPrasad Singamsetty fullsz = (uint64_t)geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead;
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
7187c478bd9Sstevel@tonic-gate /* make the vtoc look sane - ha ha */
7197c478bd9Sstevel@tonic-gate vtoc->v_version = V_VERSION;
7207c478bd9Sstevel@tonic-gate vtoc->v_sanity = VTOC_SANE;
7217c478bd9Sstevel@tonic-gate vtoc->v_nparts = V_NUMPAR;
7227c478bd9Sstevel@tonic-gate if (vtoc->v_sectorsz == 0)
7237c478bd9Sstevel@tonic-gate vtoc->v_sectorsz = sectsiz;
7247c478bd9Sstevel@tonic-gate #endif /* defined(_SUNOS_VTOC_16) */
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) {
7277c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_tag == V_BACKUP) {
7287c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size != fullsz) {
7297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\
7307c478bd9Sstevel@tonic-gate fmthard: Partition %d specifies the full disk and is not equal\n\
731342440ecSPrasad Singamsetty full size of disk. The full disk capacity is %llu sectors.\n", i, fullsz);
7327c478bd9Sstevel@tonic-gate #if defined(sparc)
7337c478bd9Sstevel@tonic-gate exit(1);
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_size == 0)
7387c478bd9Sstevel@tonic-gate continue; /* Undefined partition */
7397c478bd9Sstevel@tonic-gate if ((vtoc->v_part[i].p_start % nblks) ||
7407c478bd9Sstevel@tonic-gate (vtoc->v_part[i].p_size % nblks)) {
7417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\
7427c478bd9Sstevel@tonic-gate fmthard: Partition %d not aligned on cylinder boundary \n", i);
7437c478bd9Sstevel@tonic-gate exit(1);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_start > fullsz ||
7467c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start +
7477c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size > fullsz) {
7487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\
749342440ecSPrasad Singamsetty fmthard: Partition %d specified as %llu sectors starting at %llu\n\
750342440ecSPrasad Singamsetty \tdoes not fit. The full disk contains %llu sectors.\n",
7517c478bd9Sstevel@tonic-gate i, vtoc->v_part[i].p_size,
7527c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_start, fullsz);
7537c478bd9Sstevel@tonic-gate #if defined(sparc)
7547c478bd9Sstevel@tonic-gate exit(1);
7557c478bd9Sstevel@tonic-gate #endif
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate if (vtoc->v_part[i].p_tag != V_BACKUP &&
7597c478bd9Sstevel@tonic-gate vtoc->v_part[i].p_size != fullsz) {
7607c478bd9Sstevel@tonic-gate for (j = 0; j < V_NUMPAR; j++) {
7617c478bd9Sstevel@tonic-gate if (vtoc->v_part[j].p_tag == V_BACKUP)
7627c478bd9Sstevel@tonic-gate continue;
7637c478bd9Sstevel@tonic-gate if (vtoc->v_part[j].p_size == fullsz)
7647c478bd9Sstevel@tonic-gate continue;
7657c478bd9Sstevel@tonic-gate isize = vtoc->v_part[i].p_size;
7667c478bd9Sstevel@tonic-gate jsize = vtoc->v_part[j].p_size;
7677c478bd9Sstevel@tonic-gate istart = vtoc->v_part[i].p_start;
7687c478bd9Sstevel@tonic-gate jstart = vtoc->v_part[j].p_start;
7697c478bd9Sstevel@tonic-gate if ((i != j) &&
7707c478bd9Sstevel@tonic-gate (isize != 0) && (jsize != 0)) {
7717c478bd9Sstevel@tonic-gate endsect = jstart + jsize -1;
7727c478bd9Sstevel@tonic-gate if ((jstart <= istart) &&
7737c478bd9Sstevel@tonic-gate (istart <= endsect)) {
7747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\
7757c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\
7767c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n",
7777c478bd9Sstevel@tonic-gate i, j);
7787c478bd9Sstevel@tonic-gate #if defined(sparc)
7797c478bd9Sstevel@tonic-gate exit(1);
7807c478bd9Sstevel@tonic-gate #endif
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate * validate64()
7907c478bd9Sstevel@tonic-gate *
7917c478bd9Sstevel@tonic-gate * Validate the new VTOC.
7927c478bd9Sstevel@tonic-gate */
7937c478bd9Sstevel@tonic-gate static void
validate64(struct dk_gpt * efi)7947c478bd9Sstevel@tonic-gate validate64(struct dk_gpt *efi)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate int i;
7977c478bd9Sstevel@tonic-gate int j;
7987c478bd9Sstevel@tonic-gate int resv_part = 0;
7997c478bd9Sstevel@tonic-gate diskaddr_t endsect;
8007c478bd9Sstevel@tonic-gate diskaddr_t fullsz;
8017c478bd9Sstevel@tonic-gate diskaddr_t istart;
8027c478bd9Sstevel@tonic-gate diskaddr_t jstart;
8037c478bd9Sstevel@tonic-gate diskaddr_t isize;
8047c478bd9Sstevel@tonic-gate diskaddr_t jsize;
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate fullsz = lastlba + 1;
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate for (i = 0; i < efi->efi_nparts; i++) {
8097c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_size == 0)
8107c478bd9Sstevel@tonic-gate continue; /* Undefined partition */
8117c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_tag == V_RESERVED)
8127c478bd9Sstevel@tonic-gate resv_part++;
8137c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_start > fullsz ||
8147c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start +
8157c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size > fullsz) {
8167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\
8177c478bd9Sstevel@tonic-gate fmthard: Partition %d specified as %lld sectors starting at %lld\n\
8187c478bd9Sstevel@tonic-gate \tdoes not fit. The full disk contains %lld sectors.\n",
8197c478bd9Sstevel@tonic-gate i, efi->efi_parts[i].p_size,
8207c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_start, fullsz);
8217c478bd9Sstevel@tonic-gate exit(1);
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate if (efi->efi_parts[i].p_tag != V_BACKUP &&
8257c478bd9Sstevel@tonic-gate efi->efi_parts[i].p_size != fullsz) {
8261cce8a3fSyl194034 for (j = 0; j < efi->efi_nparts; j++) {
8277c478bd9Sstevel@tonic-gate if (efi->efi_parts[j].p_size == fullsz)
8287c478bd9Sstevel@tonic-gate continue;
8297c478bd9Sstevel@tonic-gate isize = efi->efi_parts[i].p_size;
8307c478bd9Sstevel@tonic-gate jsize = efi->efi_parts[j].p_size;
8317c478bd9Sstevel@tonic-gate istart = efi->efi_parts[i].p_start;
8327c478bd9Sstevel@tonic-gate jstart = efi->efi_parts[j].p_start;
8337c478bd9Sstevel@tonic-gate if ((i != j) &&
8347c478bd9Sstevel@tonic-gate (isize != 0) && (jsize != 0)) {
8357c478bd9Sstevel@tonic-gate endsect = jstart + jsize - 1;
8367c478bd9Sstevel@tonic-gate if ((jstart <= istart) &&
8377c478bd9Sstevel@tonic-gate (istart <= endsect)) {
8387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\
8397c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\
8407c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n",
8417c478bd9Sstevel@tonic-gate i, j);
8427c478bd9Sstevel@tonic-gate #if defined(sparc)
8437c478bd9Sstevel@tonic-gate exit(1);
8447c478bd9Sstevel@tonic-gate #endif
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate if (resv_part != 1) {
8517c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8527c478bd9Sstevel@tonic-gate "expected one reserved partition, but found %d\n",
8537c478bd9Sstevel@tonic-gate resv_part);
8547c478bd9Sstevel@tonic-gate exit(1);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate * Read the VTOC
8617c478bd9Sstevel@tonic-gate */
8627c478bd9Sstevel@tonic-gate int
vread(int fd,struct extvtoc * vtoc,char * devname)863342440ecSPrasad Singamsetty vread(int fd, struct extvtoc *vtoc, char *devname)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate int i;
8667c478bd9Sstevel@tonic-gate
867342440ecSPrasad Singamsetty if ((i = read_extvtoc(fd, vtoc)) < 0) {
8687c478bd9Sstevel@tonic-gate if (i == VT_ENOTSUP) {
8697c478bd9Sstevel@tonic-gate return (1);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) {
8727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Invalid VTOC\n",
8737c478bd9Sstevel@tonic-gate devname);
8747c478bd9Sstevel@tonic-gate } else {
8757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot read VTOC\n",
8767c478bd9Sstevel@tonic-gate devname);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate exit(1);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate return (0);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate void
vread64(int fd,struct dk_gpt ** efi_hdr,char * devname)8847c478bd9Sstevel@tonic-gate vread64(int fd, struct dk_gpt **efi_hdr, char *devname)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate int i;
8877c478bd9Sstevel@tonic-gate
8887c478bd9Sstevel@tonic-gate if ((i = efi_alloc_and_read(fd, efi_hdr)) < 0) {
8897c478bd9Sstevel@tonic-gate if (i == VT_EINVAL)
8907c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8917c478bd9Sstevel@tonic-gate "%s: this disk must be labeled first\n",
8927c478bd9Sstevel@tonic-gate devname);
8937c478bd9Sstevel@tonic-gate else
8947c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8957c478bd9Sstevel@tonic-gate "%s: read_efi failed %d\n",
8967c478bd9Sstevel@tonic-gate devname, i);
8977c478bd9Sstevel@tonic-gate exit(1);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate lastlba = (*efi_hdr)->efi_last_u_lba;
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate /*
9037c478bd9Sstevel@tonic-gate * Write the VTOC
9047c478bd9Sstevel@tonic-gate */
9057c478bd9Sstevel@tonic-gate void
vwrite(int fd,struct extvtoc * vtoc,char * devname)906342440ecSPrasad Singamsetty vwrite(int fd, struct extvtoc *vtoc, char *devname)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate int i;
9097c478bd9Sstevel@tonic-gate
910342440ecSPrasad Singamsetty if ((i = write_extvtoc(fd, vtoc)) != 0) {
9117c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) {
9127c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9137c478bd9Sstevel@tonic-gate "%s: invalid entry exists in vtoc\n",
9147c478bd9Sstevel@tonic-gate devname);
9157c478bd9Sstevel@tonic-gate } else {
9167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot write VTOC\n",
9177c478bd9Sstevel@tonic-gate devname);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate exit(1);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate
9237c478bd9Sstevel@tonic-gate /*
9247c478bd9Sstevel@tonic-gate * Write the VTOC
9257c478bd9Sstevel@tonic-gate */
9267c478bd9Sstevel@tonic-gate void
vwrite64(int fd,struct dk_gpt * efi,char * devname)9277c478bd9Sstevel@tonic-gate vwrite64(int fd, struct dk_gpt *efi, char *devname)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate int i;
9307c478bd9Sstevel@tonic-gate
9317c478bd9Sstevel@tonic-gate if ((i = efi_write(fd, efi)) != 0) {
9327c478bd9Sstevel@tonic-gate if (i == VT_EINVAL) {
9337c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
9347c478bd9Sstevel@tonic-gate "%s: invalid entry exists in vtoc\n",
9357c478bd9Sstevel@tonic-gate devname);
9367c478bd9Sstevel@tonic-gate } else {
9377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Cannot write EFI\n",
9387c478bd9Sstevel@tonic-gate devname);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate exit(1);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate }
943