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