xref: /titanic_52/usr/src/cmd/fmthard/fmthard.c (revision 342440ec94087b8c751c580ab9ed6c693d31d418)
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*342440ecSPrasad Singamsetty  * Copyright 2008 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>
68*342440ecSPrasad 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 **);
91*342440ecSPrasad Singamsetty static	void	display(struct dk_geom *, struct extvtoc *, char *);
927c478bd9Sstevel@tonic-gate static	void	display64(struct dk_gpt *,  char *);
93*342440ecSPrasad Singamsetty static	void	insert(char *, struct extvtoc *);
947c478bd9Sstevel@tonic-gate static	void	insert64(char *, struct dk_gpt *);
95*342440ecSPrasad 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);
98*342440ecSPrasad Singamsetty static	void	validate(struct dk_geom *, struct extvtoc *);
997c478bd9Sstevel@tonic-gate static	void	validate64(struct dk_gpt *);
100*342440ecSPrasad Singamsetty static	int	vread(int, struct extvtoc *, char *);
1017c478bd9Sstevel@tonic-gate static	void	vread64(int, struct dk_gpt **, char *);
102*342440ecSPrasad 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 #if defined(_SUNOS_VTOC_16)
1297c478bd9Sstevel@tonic-gate static int		sectsiz;
130*342440ecSPrasad Singamsetty static struct extvtoc	disk_vtoc;
1317c478bd9Sstevel@tonic-gate #endif	/* defined(_SUNOS_VTOC_16) */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
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)
142*342440ecSPrasad 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;
1467c478bd9Sstevel@tonic-gate 	int		n;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1492f7fafa7Szl149053 	disk_efi = NULL;
1507c478bd9Sstevel@tonic-gate 	dfile = NULL;
1517c478bd9Sstevel@tonic-gate 	vname = NULL;
1527c478bd9Sstevel@tonic-gate #if defined(sparc)
1537c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ed:u:in:qs:")) != EOF)
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #elif defined(i386)
1567c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ed:u:in:qb:p:s:")) != EOF)
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #else
1597c478bd9Sstevel@tonic-gate #error No platform defined.
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 		switch (c) {
1627c478bd9Sstevel@tonic-gate #if defined(i386)
1637c478bd9Sstevel@tonic-gate 		case 'p':
1647c478bd9Sstevel@tonic-gate 		case 'b':
1657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1667c478bd9Sstevel@tonic-gate 			    "fmthard: -p and -b no longer supported."
1677c478bd9Sstevel@tonic-gate 			    " Use installgrub(1M) to install boot blocks\n");
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate #endif	/* defined(i386) */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		case 'd':
1727c478bd9Sstevel@tonic-gate 			delta = optarg;
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		case 'e':
1757c478bd9Sstevel@tonic-gate 			++eflag;
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 		case 'i':
1787c478bd9Sstevel@tonic-gate 			++iflag;
1797c478bd9Sstevel@tonic-gate 			break;
1807c478bd9Sstevel@tonic-gate 		case 'n':
1817c478bd9Sstevel@tonic-gate 			vname = optarg;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		case 'q':
1847c478bd9Sstevel@tonic-gate 			++qflag;
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		case 's':
1877c478bd9Sstevel@tonic-gate 			dfile = optarg;
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		case 'u':
1907c478bd9Sstevel@tonic-gate 			if (strcmp(uboot, optarg) == 0)
1917c478bd9Sstevel@tonic-gate 				++uflag;
1927c478bd9Sstevel@tonic-gate 			else if (strcmp(ufirm, optarg) == 0)
1937c478bd9Sstevel@tonic-gate 				uflag = 2;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		default:
1977c478bd9Sstevel@tonic-gate 			usage();
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (argc - optind != 1)
2027c478bd9Sstevel@tonic-gate 		usage();
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (stat(argv[optind], (struct stat *)&statbuf) == -1) {
2057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2067c478bd9Sstevel@tonic-gate 			"fmthard:  Cannot stat device %s\n",
2077c478bd9Sstevel@tonic-gate 			argv[optind]);
2087c478bd9Sstevel@tonic-gate 		exit(1);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
2127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 			"fmthard:  %s must be a raw device.\n",
2147c478bd9Sstevel@tonic-gate 			argv[optind]);
2157c478bd9Sstevel@tonic-gate 		exit(1);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if ((fd = open(argv[optind], O_RDWR|O_NDELAY)) < 0) {
2197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "fmthard:  Cannot open device %s - %s\n",
220ace1a5f1Sdp 			argv[optind], strerror(errno));
2217c478bd9Sstevel@tonic-gate 		exit(1);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Get the geometry information for this disk from the driver
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	if (!eflag && ioctl(fd, DKIOCGGEOM, &disk_geom)) {
2287c478bd9Sstevel@tonic-gate #ifdef DEBUG
2297c478bd9Sstevel@tonic-gate 		perror("DKIOCGGEOM failed");
2307c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2317c478bd9Sstevel@tonic-gate 		if (errno == ENOTSUP) {
2327c478bd9Sstevel@tonic-gate 			/* disk has EFI labels */
2337c478bd9Sstevel@tonic-gate 			eflag++;
2347c478bd9Sstevel@tonic-gate 		} else {
2357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2367c478bd9Sstevel@tonic-gate 				"%s: Cannot get disk geometry\n", argv[optind]);
2377c478bd9Sstevel@tonic-gate 			(void) close(fd);
2387c478bd9Sstevel@tonic-gate 			exit(1);
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Read the vtoc on the disk
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	if (!eflag) {
2467c478bd9Sstevel@tonic-gate 		if (vread(fd, &disk_vtoc, argv[optind]) == 1)
2477c478bd9Sstevel@tonic-gate 			eflag++;
2487c478bd9Sstevel@tonic-gate 	}
2492f7fafa7Szl149053 	if (eflag && ((dfile == NULL) || qflag)) {
2507c478bd9Sstevel@tonic-gate 		vread64(fd, &disk_efi, argv[optind]);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * Quick check for valid disk: 0 if ok, 1 if not
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	if (qflag) {
2577c478bd9Sstevel@tonic-gate 		(void) close(fd);
2587c478bd9Sstevel@tonic-gate 		if (!eflag) {
2597c478bd9Sstevel@tonic-gate 			exit(disk_vtoc.v_sanity == VTOC_SANE ? 0 : 1);
2607c478bd9Sstevel@tonic-gate 		} else {
2617c478bd9Sstevel@tonic-gate 			exit(disk_efi->efi_version <= EFI_VERSION102 ? 0 : 1);
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * Incremental changes to the VTOC
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	if (delta) {
2697c478bd9Sstevel@tonic-gate 		if (!eflag) {
2707c478bd9Sstevel@tonic-gate 			insert(delta, &disk_vtoc);
2717c478bd9Sstevel@tonic-gate 			validate(&disk_geom, &disk_vtoc);
2727c478bd9Sstevel@tonic-gate 			vwrite(fd, &disk_vtoc, argv[optind]);
2737c478bd9Sstevel@tonic-gate 		} else {
2747c478bd9Sstevel@tonic-gate 			insert64(delta, disk_efi);
2757c478bd9Sstevel@tonic-gate 			validate64(disk_efi);
2767c478bd9Sstevel@tonic-gate 			vwrite64(fd, disk_efi, argv[optind]);
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 		(void) close(fd);
2797c478bd9Sstevel@tonic-gate 		exit(0);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (!dfile && !vname)
2837c478bd9Sstevel@tonic-gate 		usage();
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Read new VTOC from stdin or data file
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 	if (dfile) {
2897c478bd9Sstevel@tonic-gate 		if (strcmp(dfile, "-") == 0) {
2907c478bd9Sstevel@tonic-gate 			if (!eflag)
2917c478bd9Sstevel@tonic-gate 				load(stdin, &disk_geom, &disk_vtoc);
2927c478bd9Sstevel@tonic-gate 			else
2937c478bd9Sstevel@tonic-gate 				load64(stdin, fd, &disk_efi);
2947c478bd9Sstevel@tonic-gate 		} else {
2957c478bd9Sstevel@tonic-gate 			FILE *fp;
2967c478bd9Sstevel@tonic-gate 			if ((fp = fopen(dfile, "r")) == NULL) {
2977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "Cannot open file %s\n",
2987c478bd9Sstevel@tonic-gate 					dfile);
2997c478bd9Sstevel@tonic-gate 				(void) close(fd);
3007c478bd9Sstevel@tonic-gate 				exit(1);
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 			if (!eflag)
3037c478bd9Sstevel@tonic-gate 				load(fp, &disk_geom, &disk_vtoc);
3047c478bd9Sstevel@tonic-gate 			else
3057c478bd9Sstevel@tonic-gate 				load64(fp, fd, &disk_efi);
3067c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * Print the modified VTOC, rather than updating the disk
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	if (iflag) {
3157c478bd9Sstevel@tonic-gate 		if (!eflag)
3167c478bd9Sstevel@tonic-gate 			display(&disk_geom, &disk_vtoc, argv[optind]);
3177c478bd9Sstevel@tonic-gate 		else
3187c478bd9Sstevel@tonic-gate 			display64(disk_efi, argv[optind]);
3197c478bd9Sstevel@tonic-gate 		(void) close(fd);
3207c478bd9Sstevel@tonic-gate 		exit(0);
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (vname) {
3247c478bd9Sstevel@tonic-gate 		n = MIN(strlen(vname) + 1, LEN_DKL_VVOL);
3257c478bd9Sstevel@tonic-gate 		if (!eflag) {
3267c478bd9Sstevel@tonic-gate 			(void) memcpy(disk_vtoc.v_volume, vname, n);
3277c478bd9Sstevel@tonic-gate 		} else {
3287c478bd9Sstevel@tonic-gate 			for (c = 0; c < disk_efi->efi_nparts; c++) {
3297c478bd9Sstevel@tonic-gate 			    if (disk_efi->efi_parts[c].p_tag ==
3307c478bd9Sstevel@tonic-gate 				    V_RESERVED) {
3317c478bd9Sstevel@tonic-gate 				(void) memcpy(&disk_efi->efi_parts[c].p_name,
3327c478bd9Sstevel@tonic-gate 					    vname, n);
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 	/*
3397c478bd9Sstevel@tonic-gate 	 * Write the new VTOC on the disk
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	if (!eflag) {
3427c478bd9Sstevel@tonic-gate 		validate(&disk_geom, &disk_vtoc);
3437c478bd9Sstevel@tonic-gate 		vwrite(fd, &disk_vtoc, argv[optind]);
3447c478bd9Sstevel@tonic-gate 	} else {
3457c478bd9Sstevel@tonic-gate 		validate64(disk_efi);
3467c478bd9Sstevel@tonic-gate 		vwrite64(fd, disk_efi, argv[optind]);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Shut system down after writing a new vtoc to disk
3517c478bd9Sstevel@tonic-gate 	 * This is used during installation of core floppies.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	if (uflag == 1)
3543f2f09c1Sdp 		(void) uadmin(A_REBOOT, AD_BOOT, 0);
3557c478bd9Sstevel@tonic-gate 	else if (uflag == 2)
3563f2f09c1Sdp 		(void) uadmin(A_REBOOT, AD_IBOOT, 0);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	(void) printf("fmthard:  New volume table of contents now in place.\n");
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * display ()
3677c478bd9Sstevel@tonic-gate  *
3687c478bd9Sstevel@tonic-gate  * display contents of VTOC without writing it to disk
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate static void
371*342440ecSPrasad Singamsetty display(struct dk_geom *geom, struct extvtoc *vtoc, char *device)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	int	i;
3747c478bd9Sstevel@tonic-gate 	int	c;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * Print out the VTOC
3787c478bd9Sstevel@tonic-gate 	 */
3797c478bd9Sstevel@tonic-gate 	(void) printf("* %s default partition map\n", device);
3807c478bd9Sstevel@tonic-gate 	if (*vtoc->v_volume) {
3817c478bd9Sstevel@tonic-gate 		(void) printf("* Volume Name:  ");
3827c478bd9Sstevel@tonic-gate 		for (i = 0; i < LEN_DKL_VVOL; i++) {
3837c478bd9Sstevel@tonic-gate 			if ((c = vtoc->v_volume[i]) == 0)
3847c478bd9Sstevel@tonic-gate 				break;
3857c478bd9Sstevel@tonic-gate 			(void) printf("%c", c);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 		(void) printf("\n");
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	(void) printf("*\n");
3907c478bd9Sstevel@tonic-gate 	(void) printf("* Dimensions:\n");
3917c478bd9Sstevel@tonic-gate 	(void) printf("*     %d bytes/sector\n", SECSIZE);
3927c478bd9Sstevel@tonic-gate 	(void) printf("*      %d sectors/track\n", geom->dkg_nsect);
3937c478bd9Sstevel@tonic-gate 	(void) printf("*       %d tracks/cylinder\n", geom->dkg_nhead);
3947c478bd9Sstevel@tonic-gate 	(void) printf("*     %d cylinders\n", geom->dkg_pcyl);
3957c478bd9Sstevel@tonic-gate 	(void) printf("*     %d accessible cylinders\n", geom->dkg_ncyl);
3967c478bd9Sstevel@tonic-gate 	(void) printf("*\n");
3977c478bd9Sstevel@tonic-gate 	(void) printf("* Flags:\n");
3987c478bd9Sstevel@tonic-gate 	(void) printf("*   1:  unmountable\n");
3997c478bd9Sstevel@tonic-gate 	(void) printf("*  10:  read-only\n");
4007c478bd9Sstevel@tonic-gate 	(void) printf("*\n");
4017c478bd9Sstevel@tonic-gate 	(void) printf(
4027c478bd9Sstevel@tonic-gate "\n* Partition    Tag     Flag	    First Sector    Sector Count\n");
4037c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++) {
4047c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[i].p_size > 0)
4057c478bd9Sstevel@tonic-gate 			(void) printf(
406*342440ecSPrasad Singamsetty "    %d		%d	0%x		%llu		%llu\n",
4077c478bd9Sstevel@tonic-gate 				i, vtoc->v_part[i].p_tag,
4087c478bd9Sstevel@tonic-gate 				vtoc->v_part[i].p_flag,
4097c478bd9Sstevel@tonic-gate 				vtoc->v_part[i].p_start,
4107c478bd9Sstevel@tonic-gate 				vtoc->v_part[i].p_size);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 	exit(0);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * display64 ()
4177c478bd9Sstevel@tonic-gate  *
4187c478bd9Sstevel@tonic-gate  * display64 contents of EFI partition without writing it to disk
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate static void
4217c478bd9Sstevel@tonic-gate display64(struct dk_gpt *efi, char *device)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	int	i;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Print out the VTOC
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	(void) printf("* %s default partition map\n", device);
4297c478bd9Sstevel@tonic-gate 	(void) printf("*\n");
4307c478bd9Sstevel@tonic-gate 	(void) printf("* Dimensions:\n");
4317c478bd9Sstevel@tonic-gate 	(void) printf("*     %d bytes/sector\n", efi->efi_lbasize);
4327c478bd9Sstevel@tonic-gate 	(void) printf("*     N/A sectors/track\n");
4337c478bd9Sstevel@tonic-gate 	(void) printf("*     N/A tracks/cylinder\n");
4347c478bd9Sstevel@tonic-gate 	(void) printf("*     N/A cylinders\n");
4357c478bd9Sstevel@tonic-gate 	(void) printf("*     N/A accessible cylinders\n");
4367c478bd9Sstevel@tonic-gate 	(void) printf("*\n");
4377c478bd9Sstevel@tonic-gate 	(void) printf("* Flags:\n");
4387c478bd9Sstevel@tonic-gate 	(void) printf("*   1:  unmountable\n");
4397c478bd9Sstevel@tonic-gate 	(void) printf("*  10:  read-only\n");
4407c478bd9Sstevel@tonic-gate 	(void) printf("*\n");
4417c478bd9Sstevel@tonic-gate 	(void) printf(
4427c478bd9Sstevel@tonic-gate "\n* Partition    Tag     Flag	    First Sector    Sector Count\n");
4437c478bd9Sstevel@tonic-gate 	for (i = 0; i < efi->efi_nparts; i++) {
4447c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[i].p_size > 0)
4457c478bd9Sstevel@tonic-gate 			(void) printf(
4467c478bd9Sstevel@tonic-gate "    %d		%d	0%x		%8lld	%8lld\n",
4477c478bd9Sstevel@tonic-gate 				i, efi->efi_parts[i].p_tag,
4487c478bd9Sstevel@tonic-gate 				efi->efi_parts[i].p_flag,
4497c478bd9Sstevel@tonic-gate 				efi->efi_parts[i].p_start,
4507c478bd9Sstevel@tonic-gate 				efi->efi_parts[i].p_size);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	exit(0);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate  * insert()
4587c478bd9Sstevel@tonic-gate  *
4597c478bd9Sstevel@tonic-gate  * Insert a change into the VTOC.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate static void
462*342440ecSPrasad Singamsetty insert(char *data, struct extvtoc *vtoc)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	int		part;
4657c478bd9Sstevel@tonic-gate 	int		tag;
4667c478bd9Sstevel@tonic-gate 	uint_t		flag;
467*342440ecSPrasad Singamsetty 	diskaddr_t	start;
468*342440ecSPrasad Singamsetty 	uint64_t	size;
4697c478bd9Sstevel@tonic-gate 
470*342440ecSPrasad Singamsetty 	if (sscanf(data, "%d:%d:%x:%llu:%llu",
4717c478bd9Sstevel@tonic-gate 	    &part, &tag, &flag, &start, &size) != 5) {
4727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data);
4737c478bd9Sstevel@tonic-gate 		exit(1);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	if (part >= V_NUMPAR) {
4767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4777c478bd9Sstevel@tonic-gate 			"Error in data \"%s\": No such partition %x\n",
4787c478bd9Sstevel@tonic-gate 			data, part);
4797c478bd9Sstevel@tonic-gate 		exit(1);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	vtoc->v_part[part].p_tag = (ushort_t)tag;
4827c478bd9Sstevel@tonic-gate 	vtoc->v_part[part].p_flag = (ushort_t)flag;
4837c478bd9Sstevel@tonic-gate 	vtoc->v_part[part].p_start = start;
4847c478bd9Sstevel@tonic-gate 	vtoc->v_part[part].p_size = size;
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * insert64()
4897c478bd9Sstevel@tonic-gate  *
4907c478bd9Sstevel@tonic-gate  * Insert a change into the VTOC.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate static void
4937c478bd9Sstevel@tonic-gate insert64(char *data, struct dk_gpt *efi)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	int		part;
4967c478bd9Sstevel@tonic-gate 	int		tag;
4977c478bd9Sstevel@tonic-gate 	uint_t		flag;
4987c478bd9Sstevel@tonic-gate 	diskaddr_t	start;
4997c478bd9Sstevel@tonic-gate 	diskaddr_t	size;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (sscanf(data, "%d:%d:%x:%lld:%lld",
5027c478bd9Sstevel@tonic-gate 	    &part, &tag, &flag, &start, &size) != 5) {
5037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Delta syntax error on \"%s\"\n", data);
5047c478bd9Sstevel@tonic-gate 		exit(1);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	if (part >= efi->efi_nparts) {
5077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5087c478bd9Sstevel@tonic-gate 			"Error in data \"%s\": No such partition %x\n",
5097c478bd9Sstevel@tonic-gate 			data, part);
5107c478bd9Sstevel@tonic-gate 		exit(1);
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 	efi->efi_parts[part].p_tag = (ushort_t)tag;
5137c478bd9Sstevel@tonic-gate 	efi->efi_parts[part].p_flag = (ushort_t)flag;
5147c478bd9Sstevel@tonic-gate 	efi->efi_parts[part].p_start = start;
5157c478bd9Sstevel@tonic-gate 	efi->efi_parts[part].p_size = size;
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate /*
5197c478bd9Sstevel@tonic-gate  * load()
5207c478bd9Sstevel@tonic-gate  *
5217c478bd9Sstevel@tonic-gate  * Load VTOC information from a datafile.
5227c478bd9Sstevel@tonic-gate  */
5237c478bd9Sstevel@tonic-gate static void
524*342440ecSPrasad Singamsetty load(FILE *fp, struct dk_geom *geom, struct extvtoc *vtoc)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	int		part;
5277c478bd9Sstevel@tonic-gate 	int		tag;
5287c478bd9Sstevel@tonic-gate 	uint_t		flag;
529*342440ecSPrasad Singamsetty 	diskaddr_t	start;
530*342440ecSPrasad Singamsetty 	uint64_t	size;
5317c478bd9Sstevel@tonic-gate 	char		line[256];
5327c478bd9Sstevel@tonic-gate 	int		i;
533*342440ecSPrasad Singamsetty 	uint64_t	nblks;
534*342440ecSPrasad Singamsetty 	uint64_t	fullsz;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; ++i) {
5377c478bd9Sstevel@tonic-gate 		vtoc->v_part[i].p_tag = 0;
5387c478bd9Sstevel@tonic-gate 		vtoc->v_part[i].p_flag = V_UNMNT;
5397c478bd9Sstevel@tonic-gate 		vtoc->v_part[i].p_start = 0;
5407c478bd9Sstevel@tonic-gate 		vtoc->v_part[i].p_size = 0;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * initialize partition 2, by convention it corresponds to whole
5447c478bd9Sstevel@tonic-gate 	 * disk. It will be overwritten, if specified in the input datafile
5457c478bd9Sstevel@tonic-gate 	 */
546*342440ecSPrasad Singamsetty 	fullsz = (uint64_t)geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead;
5477c478bd9Sstevel@tonic-gate 	vtoc->v_part[2].p_tag = V_BACKUP;
5487c478bd9Sstevel@tonic-gate 	vtoc->v_part[2].p_flag = V_UNMNT;
5497c478bd9Sstevel@tonic-gate 	vtoc->v_part[2].p_start = 0;
5507c478bd9Sstevel@tonic-gate 	vtoc->v_part[2].p_size = fullsz;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	nblks = geom->dkg_nsect * geom->dkg_nhead;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
5557c478bd9Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
5567c478bd9Sstevel@tonic-gate 			continue;
5577c478bd9Sstevel@tonic-gate 		line[strlen(line) - 1] = '\0';
558*342440ecSPrasad Singamsetty 		if (sscanf(line, "%d %d %x %llu %llu",
5597c478bd9Sstevel@tonic-gate 		    &part, &tag, &flag, &start, &size) != 5) {
5607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Syntax error: \"%s\"\n",
5617c478bd9Sstevel@tonic-gate 				line);
5627c478bd9Sstevel@tonic-gate 			exit(1);
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 		if (part >= V_NUMPAR) {
5657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5667c478bd9Sstevel@tonic-gate 				"No such partition %x: \"%s\"\n",
5677c478bd9Sstevel@tonic-gate 				part, line);
5687c478bd9Sstevel@tonic-gate 			exit(1);
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 		if (!eflag && ((start % nblks) != 0 || (size % nblks) != 0)) {
5717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5727c478bd9Sstevel@tonic-gate "Partition %d not aligned on cylinder boundary: \"%s\"\n",
5737c478bd9Sstevel@tonic-gate 					part, line);
5747c478bd9Sstevel@tonic-gate 			exit(1);
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 		vtoc->v_part[part].p_tag = (ushort_t)tag;
5777c478bd9Sstevel@tonic-gate 		vtoc->v_part[part].p_flag = (ushort_t)flag;
5787c478bd9Sstevel@tonic-gate 		vtoc->v_part[part].p_start = start;
5797c478bd9Sstevel@tonic-gate 		vtoc->v_part[part].p_size = size;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 	for (part = 0; part < V_NUMPAR; part++) {
5827c478bd9Sstevel@tonic-gate 		vtoc->timestamp[part] = (time_t)0;
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * load64()
5887c478bd9Sstevel@tonic-gate  *
5897c478bd9Sstevel@tonic-gate  * Load VTOC information from a datafile.
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate static void
5927c478bd9Sstevel@tonic-gate load64(FILE *fp, int fd, struct dk_gpt **efi)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	int	part;
5957c478bd9Sstevel@tonic-gate 	int	tag;
5967c478bd9Sstevel@tonic-gate 	uint_t	flag;
5977c478bd9Sstevel@tonic-gate 	diskaddr_t	start;
5987c478bd9Sstevel@tonic-gate 	diskaddr_t	size;
5997c478bd9Sstevel@tonic-gate 	int	nlines = 0;
6007c478bd9Sstevel@tonic-gate 	char	line[256];
6017c478bd9Sstevel@tonic-gate 	int	i;
6027c478bd9Sstevel@tonic-gate 	uint_t	max_part = 0;
6037c478bd9Sstevel@tonic-gate 	char	**mem = NULL;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
6067c478bd9Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
6077c478bd9Sstevel@tonic-gate 			continue;
6087c478bd9Sstevel@tonic-gate 		line[strlen(line) - 1] = '\0';
6097c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%d %d %x %lld %lld",
6107c478bd9Sstevel@tonic-gate 		    &part, &tag, &flag, &start, &size) != 5) {
6117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Syntax error: \"%s\"\n",
6127c478bd9Sstevel@tonic-gate 				line);
6137c478bd9Sstevel@tonic-gate 			exit(1);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 		mem = realloc(mem, sizeof (*mem) * (nlines + 1));
6167c478bd9Sstevel@tonic-gate 		if (mem == NULL) {
6177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "realloc failed\n");
6187c478bd9Sstevel@tonic-gate 			exit(1);
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 		mem[nlines] = strdup(line);
6217c478bd9Sstevel@tonic-gate 		if (mem[nlines] == NULL) {
6227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "strdup failed\n");
6237c478bd9Sstevel@tonic-gate 			exit(1);
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 		nlines++;
6267c478bd9Sstevel@tonic-gate 		if (part > max_part)
6277c478bd9Sstevel@tonic-gate 		    max_part = part;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	max_part++;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if ((i = efi_alloc_and_init(fd, max_part, efi)) < 0) {
6327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
6337c478bd9Sstevel@tonic-gate 				"efi_alloc_and_init failed: %d\n", i);
6347c478bd9Sstevel@tonic-gate 		exit(1);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 	for (i = 0; i < (*efi)->efi_nparts; ++i) {
6377c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[i].p_tag = V_UNASSIGNED;
6387c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[i].p_flag = V_UNMNT;
6397c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[i].p_start = 0;
6407c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[i].p_size = 0;
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	lastlba = (*efi)->efi_last_u_lba;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlines; i++) {
6457c478bd9Sstevel@tonic-gate 		if (sscanf(mem[i], "%d %d %x %lld %lld",
6467c478bd9Sstevel@tonic-gate 		    &part, &tag, &flag, &start, &size) != 5) {
6477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Syntax error: \"%s\"\n",
6487c478bd9Sstevel@tonic-gate 				line);
6497c478bd9Sstevel@tonic-gate 			exit(1);
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		free(mem[i]);
6527c478bd9Sstevel@tonic-gate 		if (part >= (*efi)->efi_nparts) {
6537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6547c478bd9Sstevel@tonic-gate 				"No such partition %x: \"%s\"\n",
6557c478bd9Sstevel@tonic-gate 				part, line);
6567c478bd9Sstevel@tonic-gate 			exit(1);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[part].p_tag = (ushort_t)tag;
6597c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[part].p_flag = (ushort_t)flag;
6607c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[part].p_start = start;
6617c478bd9Sstevel@tonic-gate 		(*efi)->efi_parts[part].p_size = size;
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 	(*efi)->efi_nparts = max_part;
6647c478bd9Sstevel@tonic-gate 	free(mem);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate static void
6697c478bd9Sstevel@tonic-gate usage()
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6727c478bd9Sstevel@tonic-gate #if defined(sparc)
6737c478bd9Sstevel@tonic-gate "Usage:	fmthard [ -i ] [ -n volumename ] [ -s datafile ] [ -d arguments] \
6747c478bd9Sstevel@tonic-gate raw-device\n");
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate #elif defined(i386)
6777c478bd9Sstevel@tonic-gate "Usage:	fmthard [ -i ] [ -S ] [-I geom_file]  \
6787c478bd9Sstevel@tonic-gate -n volumename | -s datafile  [ -d arguments] raw-device\n");
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate #else
6817c478bd9Sstevel@tonic-gate #error No platform defined.
6827c478bd9Sstevel@tonic-gate #endif
6837c478bd9Sstevel@tonic-gate 	exit(2);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*
6877c478bd9Sstevel@tonic-gate  * validate()
6887c478bd9Sstevel@tonic-gate  *
6897c478bd9Sstevel@tonic-gate  * Validate the new VTOC.
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate static void
692*342440ecSPrasad Singamsetty validate(struct dk_geom *geom, struct extvtoc *vtoc)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	int		i;
6957c478bd9Sstevel@tonic-gate 	int		j;
696*342440ecSPrasad Singamsetty 	uint64_t	fullsz;
697*342440ecSPrasad Singamsetty 	diskaddr_t	endsect;
698*342440ecSPrasad Singamsetty 	diskaddr_t	istart;
699*342440ecSPrasad Singamsetty 	diskaddr_t	jstart;
700*342440ecSPrasad Singamsetty 	uint64_t	isize;
701*342440ecSPrasad Singamsetty 	uint64_t	jsize;
702*342440ecSPrasad Singamsetty 	uint64_t	nblks;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	nblks = geom->dkg_nsect * geom->dkg_nhead;
7057c478bd9Sstevel@tonic-gate 
706*342440ecSPrasad Singamsetty 	fullsz = (uint64_t)geom->dkg_ncyl * geom->dkg_nsect * geom->dkg_nhead;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
7097c478bd9Sstevel@tonic-gate 	/* make the vtoc look sane - ha ha */
7107c478bd9Sstevel@tonic-gate 	vtoc->v_version = V_VERSION;
7117c478bd9Sstevel@tonic-gate 	vtoc->v_sanity = VTOC_SANE;
7127c478bd9Sstevel@tonic-gate 	vtoc->v_nparts = V_NUMPAR;
7137c478bd9Sstevel@tonic-gate 	if (sectsiz == 0)
7147c478bd9Sstevel@tonic-gate 		sectsiz = SECSIZE;
7157c478bd9Sstevel@tonic-gate 	if (vtoc->v_sectorsz == 0)
7167c478bd9Sstevel@tonic-gate 		vtoc->v_sectorsz = sectsiz;
7177c478bd9Sstevel@tonic-gate #endif				/* defined(_SUNOS_VTOC_16) */
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++) {
7207c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[i].p_tag == V_BACKUP) {
7217c478bd9Sstevel@tonic-gate 			if (vtoc->v_part[i].p_size != fullsz) {
7227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "\
7237c478bd9Sstevel@tonic-gate fmthard: Partition %d specifies the full disk and is not equal\n\
724*342440ecSPrasad Singamsetty full size of disk.  The full disk capacity is %llu sectors.\n", i, fullsz);
7257c478bd9Sstevel@tonic-gate #if defined(sparc)
7267c478bd9Sstevel@tonic-gate 			exit(1);
7277c478bd9Sstevel@tonic-gate #endif
7287c478bd9Sstevel@tonic-gate 			}
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[i].p_size == 0)
7317c478bd9Sstevel@tonic-gate 			continue;	/* Undefined partition */
7327c478bd9Sstevel@tonic-gate 		if ((vtoc->v_part[i].p_start % nblks) ||
7337c478bd9Sstevel@tonic-gate 				(vtoc->v_part[i].p_size % nblks)) {
7347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\
7357c478bd9Sstevel@tonic-gate fmthard: Partition %d not aligned on cylinder boundary \n", i);
7367c478bd9Sstevel@tonic-gate 				exit(1);
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[i].p_start > fullsz ||
7397c478bd9Sstevel@tonic-gate 			vtoc->v_part[i].p_start +
7407c478bd9Sstevel@tonic-gate 				vtoc->v_part[i].p_size > fullsz) {
7417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\
742*342440ecSPrasad Singamsetty fmthard: Partition %d specified as %llu sectors starting at %llu\n\
743*342440ecSPrasad Singamsetty \tdoes not fit. The full disk contains %llu sectors.\n",
7447c478bd9Sstevel@tonic-gate 				i, vtoc->v_part[i].p_size,
7457c478bd9Sstevel@tonic-gate 				vtoc->v_part[i].p_start, fullsz);
7467c478bd9Sstevel@tonic-gate #if defined(sparc)
7477c478bd9Sstevel@tonic-gate 			exit(1);
7487c478bd9Sstevel@tonic-gate #endif
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[i].p_tag != V_BACKUP &&
7527c478bd9Sstevel@tonic-gate 		    vtoc->v_part[i].p_size != fullsz) {
7537c478bd9Sstevel@tonic-gate 			for (j = 0; j < V_NUMPAR; j++) {
7547c478bd9Sstevel@tonic-gate 				if (vtoc->v_part[j].p_tag == V_BACKUP)
7557c478bd9Sstevel@tonic-gate 					continue;
7567c478bd9Sstevel@tonic-gate 				if (vtoc->v_part[j].p_size == fullsz)
7577c478bd9Sstevel@tonic-gate 					continue;
7587c478bd9Sstevel@tonic-gate 				isize = vtoc->v_part[i].p_size;
7597c478bd9Sstevel@tonic-gate 				jsize = vtoc->v_part[j].p_size;
7607c478bd9Sstevel@tonic-gate 				istart = vtoc->v_part[i].p_start;
7617c478bd9Sstevel@tonic-gate 				jstart = vtoc->v_part[j].p_start;
7627c478bd9Sstevel@tonic-gate 				if ((i != j) &&
7637c478bd9Sstevel@tonic-gate 				    (isize != 0) && (jsize != 0)) {
7647c478bd9Sstevel@tonic-gate 					endsect = jstart + jsize -1;
7657c478bd9Sstevel@tonic-gate 					if ((jstart <= istart) &&
7667c478bd9Sstevel@tonic-gate 						(istart <= endsect)) {
7677c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "\
7687c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\
7697c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n",
7707c478bd9Sstevel@tonic-gate 						    i, j);
7717c478bd9Sstevel@tonic-gate #if defined(sparc)
7727c478bd9Sstevel@tonic-gate 						exit(1);
7737c478bd9Sstevel@tonic-gate #endif
7747c478bd9Sstevel@tonic-gate 					}
7757c478bd9Sstevel@tonic-gate 				}
7767c478bd9Sstevel@tonic-gate 			}
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate /*
7827c478bd9Sstevel@tonic-gate  * validate64()
7837c478bd9Sstevel@tonic-gate  *
7847c478bd9Sstevel@tonic-gate  * Validate the new VTOC.
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate static void
7877c478bd9Sstevel@tonic-gate validate64(struct dk_gpt *efi)
7887c478bd9Sstevel@tonic-gate {
7897c478bd9Sstevel@tonic-gate 	int		i;
7907c478bd9Sstevel@tonic-gate 	int		j;
7917c478bd9Sstevel@tonic-gate 	int		resv_part = 0;
7927c478bd9Sstevel@tonic-gate 	diskaddr_t	endsect;
7937c478bd9Sstevel@tonic-gate 	diskaddr_t	fullsz;
7947c478bd9Sstevel@tonic-gate 	diskaddr_t		istart;
7957c478bd9Sstevel@tonic-gate 	diskaddr_t		jstart;
7967c478bd9Sstevel@tonic-gate 	diskaddr_t		isize;
7977c478bd9Sstevel@tonic-gate 	diskaddr_t		jsize;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	fullsz = lastlba + 1;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	for (i = 0; i < efi->efi_nparts; i++) {
8027c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[i].p_size == 0)
8037c478bd9Sstevel@tonic-gate 			continue;	/* Undefined partition */
8047c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[i].p_tag == V_RESERVED)
8057c478bd9Sstevel@tonic-gate 			resv_part++;
8067c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[i].p_start > fullsz ||
8077c478bd9Sstevel@tonic-gate 			efi->efi_parts[i].p_start +
8087c478bd9Sstevel@tonic-gate 				efi->efi_parts[i].p_size > fullsz) {
8097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\
8107c478bd9Sstevel@tonic-gate fmthard: Partition %d specified as %lld sectors starting at %lld\n\
8117c478bd9Sstevel@tonic-gate \tdoes not fit. The full disk contains %lld sectors.\n",
8127c478bd9Sstevel@tonic-gate 				i, efi->efi_parts[i].p_size,
8137c478bd9Sstevel@tonic-gate 				efi->efi_parts[i].p_start, fullsz);
8147c478bd9Sstevel@tonic-gate 			exit(1);
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[i].p_tag != V_BACKUP &&
8187c478bd9Sstevel@tonic-gate 		    efi->efi_parts[i].p_size != fullsz) {
8191cce8a3fSyl194034 			for (j = 0; j < efi->efi_nparts; j++) {
8207c478bd9Sstevel@tonic-gate 				if (efi->efi_parts[j].p_size == fullsz)
8217c478bd9Sstevel@tonic-gate 					continue;
8227c478bd9Sstevel@tonic-gate 				isize = efi->efi_parts[i].p_size;
8237c478bd9Sstevel@tonic-gate 				jsize = efi->efi_parts[j].p_size;
8247c478bd9Sstevel@tonic-gate 				istart = efi->efi_parts[i].p_start;
8257c478bd9Sstevel@tonic-gate 				jstart = efi->efi_parts[j].p_start;
8267c478bd9Sstevel@tonic-gate 				if ((i != j) &&
8277c478bd9Sstevel@tonic-gate 				    (isize != 0) && (jsize != 0)) {
8287c478bd9Sstevel@tonic-gate 					endsect = jstart + jsize - 1;
8297c478bd9Sstevel@tonic-gate 					if ((jstart <= istart) &&
8307c478bd9Sstevel@tonic-gate 						(istart <= endsect)) {
8317c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "\
8327c478bd9Sstevel@tonic-gate fmthard: Partition %d overlaps partition %d. Overlap is allowed\n\
8337c478bd9Sstevel@tonic-gate \tonly on partition on the full disk partition).\n",
8347c478bd9Sstevel@tonic-gate 						    i, j);
8357c478bd9Sstevel@tonic-gate #if defined(sparc)
8367c478bd9Sstevel@tonic-gate 						exit(1);
8377c478bd9Sstevel@tonic-gate #endif
8387c478bd9Sstevel@tonic-gate 					}
8397c478bd9Sstevel@tonic-gate 				}
8407c478bd9Sstevel@tonic-gate 			}
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	if (resv_part != 1) {
8447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8457c478bd9Sstevel@tonic-gate 		    "expected one reserved partition, but found %d\n",
8467c478bd9Sstevel@tonic-gate 		    resv_part);
8477c478bd9Sstevel@tonic-gate 		exit(1);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * Read the VTOC
8547c478bd9Sstevel@tonic-gate  */
8557c478bd9Sstevel@tonic-gate int
856*342440ecSPrasad Singamsetty vread(int fd, struct extvtoc *vtoc, char *devname)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	int	i;
8597c478bd9Sstevel@tonic-gate 
860*342440ecSPrasad Singamsetty 	if ((i = read_extvtoc(fd, vtoc)) < 0) {
8617c478bd9Sstevel@tonic-gate 		if (i == VT_ENOTSUP) {
8627c478bd9Sstevel@tonic-gate 			return (1);
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		if (i == VT_EINVAL) {
8657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: Invalid VTOC\n",
8667c478bd9Sstevel@tonic-gate 				devname);
8677c478bd9Sstevel@tonic-gate 		} else {
8687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: Cannot read VTOC\n",
8697c478bd9Sstevel@tonic-gate 				devname);
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 		exit(1);
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 	return (0);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate void
8777c478bd9Sstevel@tonic-gate vread64(int fd, struct dk_gpt **efi_hdr, char *devname)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	int i;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if ((i = efi_alloc_and_read(fd, efi_hdr)) < 0) {
8827c478bd9Sstevel@tonic-gate 		if (i == VT_EINVAL)
8837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8847c478bd9Sstevel@tonic-gate 				"%s: this disk must be labeled first\n",
8857c478bd9Sstevel@tonic-gate 				devname);
8867c478bd9Sstevel@tonic-gate 		else
8877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8887c478bd9Sstevel@tonic-gate 				"%s: read_efi failed %d\n",
8897c478bd9Sstevel@tonic-gate 				devname, i);
8907c478bd9Sstevel@tonic-gate 		exit(1);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 	lastlba = (*efi_hdr)->efi_last_u_lba;
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate  * Write the VTOC
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate void
899*342440ecSPrasad Singamsetty vwrite(int fd, struct extvtoc *vtoc, char *devname)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	int	i;
9027c478bd9Sstevel@tonic-gate 
903*342440ecSPrasad Singamsetty 	if ((i = write_extvtoc(fd, vtoc)) != 0) {
9047c478bd9Sstevel@tonic-gate 		if (i == VT_EINVAL) {
9057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9067c478bd9Sstevel@tonic-gate 			"%s: invalid entry exists in vtoc\n",
9077c478bd9Sstevel@tonic-gate 				devname);
9087c478bd9Sstevel@tonic-gate 		} else {
9097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: Cannot write VTOC\n",
9107c478bd9Sstevel@tonic-gate 				devname);
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 		exit(1);
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate /*
9177c478bd9Sstevel@tonic-gate  * Write the VTOC
9187c478bd9Sstevel@tonic-gate  */
9197c478bd9Sstevel@tonic-gate void
9207c478bd9Sstevel@tonic-gate vwrite64(int fd, struct dk_gpt *efi, char *devname)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	int	i;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	if ((i = efi_write(fd, efi)) != 0) {
9257c478bd9Sstevel@tonic-gate 		if (i == VT_EINVAL) {
9267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9277c478bd9Sstevel@tonic-gate 			"%s: invalid entry exists in vtoc\n",
9287c478bd9Sstevel@tonic-gate 				devname);
9297c478bd9Sstevel@tonic-gate 		} else {
9307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: Cannot write EFI\n",
9317c478bd9Sstevel@tonic-gate 				devname);
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 		exit(1);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate }
936