xref: /illumos-gate/usr/src/cmd/rmformat/rmf_slice.c (revision bbed02df)
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
5e3397557Szk  * Common Development and Distribution License (the "License").
6e3397557Szk  * 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 /*
22e3397557Szk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * rmf_slice.c :
307c478bd9Sstevel@tonic-gate  * 	This file contains the functions for parsing a slice file
317c478bd9Sstevel@tonic-gate  * 	for rmformat.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <memory.h>
437c478bd9Sstevel@tonic-gate #include <dirent.h>
447c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
49*bbed02dfSzk #include <priv_utils.h>
507c478bd9Sstevel@tonic-gate #include "rmformat.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate extern void my_perror(char *err_string);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int32_t	last_token_type = 0;
557c478bd9Sstevel@tonic-gate #define	spc()	  (last_token_type)
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * This global is used to store the current line # in the
607c478bd9Sstevel@tonic-gate  * data file. It must be global because the I/O routines
617c478bd9Sstevel@tonic-gate  * are allowed to side effect it to keep track of backslashed
627c478bd9Sstevel@tonic-gate  * newlines.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int32_t	data_lineno;		/* current line # in data file */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	CHG_MODE_UNDEFINED  (-1)	/* undefined value */
687c478bd9Sstevel@tonic-gate #define	CHG_MODE_SET	0		/* set bits by or'ing */
697c478bd9Sstevel@tonic-gate #define	CHG_MODE_CLR	1		/* clr bits by and'ing */
707c478bd9Sstevel@tonic-gate #define	CHG_MODE_ABS	2		/* set absolute value */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	TOKEN_SIZE	36		/* max length of a token */
747c478bd9Sstevel@tonic-gate typedef char TOKEN[TOKEN_SIZE+1];	/* token type */
757c478bd9Sstevel@tonic-gate #define	DATA_INPUT	0		/* 2 modes of input */
767c478bd9Sstevel@tonic-gate #define	CMD_INPUT	1
777c478bd9Sstevel@tonic-gate #define	WILD_STRING	"$"		/* wildcard character */
787c478bd9Sstevel@tonic-gate #define	COMMENT_CHAR	'#'		/* comment character */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * List of strings with arbitrary matching values
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate typedef struct slist {
847c478bd9Sstevel@tonic-gate 	char	*str;
857c478bd9Sstevel@tonic-gate 	char	*help;
867c478bd9Sstevel@tonic-gate 	int32_t	value;
877c478bd9Sstevel@tonic-gate } slist_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static slist_t ptag_choices[] = {
907c478bd9Sstevel@tonic-gate 	{ "unassigned", "",	V_UNASSIGNED	},
917c478bd9Sstevel@tonic-gate 	{ "boot",	"",	V_BOOT		},
927c478bd9Sstevel@tonic-gate 	{ "root",	"",	V_ROOT		},
937c478bd9Sstevel@tonic-gate 	{ "swap",	"",	V_SWAP		},
947c478bd9Sstevel@tonic-gate 	{ "usr",	"",	V_USR		},
957c478bd9Sstevel@tonic-gate 	{ "backup",	"",	V_BACKUP	},
967c478bd9Sstevel@tonic-gate 	{ "stand",	"",	V_STAND		},
977c478bd9Sstevel@tonic-gate 	{ "var",	"",	V_VAR		},
987c478bd9Sstevel@tonic-gate 	{ "home",	"",	V_HOME		},
997c478bd9Sstevel@tonic-gate 	{ "alternates", "",	V_ALTSCTR	},
1007c478bd9Sstevel@tonic-gate 	{ NULL }
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Choices for the p_flag vtoc field
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static slist_t pflag_choices[] = {
1087c478bd9Sstevel@tonic-gate 	{ "wm", "read-write, mountable",	0	},
1097c478bd9Sstevel@tonic-gate 	{ "wu", "read-write, unmountable",	V_UNMNT	},
1107c478bd9Sstevel@tonic-gate 	{ "rm", "read-only, mountable",		V_RONLY	},
1117c478bd9Sstevel@tonic-gate 	{ "ru", "read-only, unmountable",	V_RONLY|V_UNMNT },
1127c478bd9Sstevel@tonic-gate 	{ NULL }
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * The definitions are the token types that the data file parser recognizes.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate #define	SUP_EOF			-1		/* eof token */
1197c478bd9Sstevel@tonic-gate #define	SUP_STRING		0		/* string token */
1207c478bd9Sstevel@tonic-gate #define	SUP_EQL			1		/* equals token */
1217c478bd9Sstevel@tonic-gate #define	SUP_COMMA		2		/* comma token */
1227c478bd9Sstevel@tonic-gate #define	SUP_COLON		3		/* colon token */
1237c478bd9Sstevel@tonic-gate #define	SUP_EOL			4		/* newline token */
1247c478bd9Sstevel@tonic-gate #define	SUP_OR			5		/* vertical bar */
1257c478bd9Sstevel@tonic-gate #define	SUP_AND			6		/* ampersand */
1267c478bd9Sstevel@tonic-gate #define	SUP_TILDE		7		/* tilde */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  *	Prototypes for ANSI C compilers
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate static int32_t	sup_prxfile(char *file_name, struct vtoc *vt);
1337c478bd9Sstevel@tonic-gate static int32_t	sup_setpart(struct vtoc *vt);
1347c478bd9Sstevel@tonic-gate static void	sup_pushchar(int32_t c);
1357c478bd9Sstevel@tonic-gate static void	clean_token(char *cleantoken, char *token);
1367c478bd9Sstevel@tonic-gate static void clean_token(char *cleantoken, char *token);
1377c478bd9Sstevel@tonic-gate static int32_t sup_inputchar();
1387c478bd9Sstevel@tonic-gate static int32_t sup_gettoken(char *buf);
1397c478bd9Sstevel@tonic-gate static int32_t sup_get_token(char *buf);
1407c478bd9Sstevel@tonic-gate static int32_t find_value(slist_t *slist, char *str, int32_t *value);
1417c478bd9Sstevel@tonic-gate static int32_t check_vtoc_sanity(smedia_handle_t, int32_t fd, struct vtoc *vt);
1427c478bd9Sstevel@tonic-gate static int32_t str2sector(char *str);
1437c478bd9Sstevel@tonic-gate static int32_t strcnt(char *s1, char *s2);
1447c478bd9Sstevel@tonic-gate static int32_t get_fdisk(smedia_handle_t, int32_t fd, int32_t offset,
1457c478bd9Sstevel@tonic-gate 		struct fdisk_info *fdisk);
1467c478bd9Sstevel@tonic-gate static void erase(smedia_handle_t handle, uint32_t offset, uint32_t size);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate extern char *myname;
1497c478bd9Sstevel@tonic-gate extern int64_t my_atoll(char *ptr);
1507c478bd9Sstevel@tonic-gate extern smmedium_prop_t med_info;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static FILE *data_file;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static int32_t
1557c478bd9Sstevel@tonic-gate sup_prxfile(char *file_name, struct vtoc *vt)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	int32_t	status, ret_val;
1587c478bd9Sstevel@tonic-gate 	TOKEN	token;
1597c478bd9Sstevel@tonic-gate 	TOKEN	cleaned;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Open the data file.  Return 0 if unable to do so.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	data_file = fopen(file_name, "r");
1657c478bd9Sstevel@tonic-gate 	if (data_file == NULL) {
1667c478bd9Sstevel@tonic-gate 		PERROR("Open failed");
1677c478bd9Sstevel@tonic-gate 		return (-1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Step through the data file a meta-line at a time.  There are
1717c478bd9Sstevel@tonic-gate 	 * typically several backslashed newlines in each meta-line,
1727c478bd9Sstevel@tonic-gate 	 * so data_lineno will be getting side effected along the way.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	data_lineno = 1;
1757c478bd9Sstevel@tonic-gate 	for (;;) {
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		/*
1787c478bd9Sstevel@tonic-gate 		 * Get the keyword.
1797c478bd9Sstevel@tonic-gate 		 */
1807c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
1817c478bd9Sstevel@tonic-gate 		/*
1827c478bd9Sstevel@tonic-gate 		 * If we hit the end of the data file, we're done.
1837c478bd9Sstevel@tonic-gate 		 */
1847c478bd9Sstevel@tonic-gate 		if (status == SUP_EOF)
1857c478bd9Sstevel@tonic-gate 			break;
1867c478bd9Sstevel@tonic-gate 		/*
1877c478bd9Sstevel@tonic-gate 		 * If the line starts with some key character, it's an error.
1887c478bd9Sstevel@tonic-gate 		 */
1897c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
1907c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1917c478bd9Sstevel@tonic-gate 				gettext("Expecting keyword, found '%s'"),
1927c478bd9Sstevel@tonic-gate 			    token);
1937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1947c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
1957c478bd9Sstevel@tonic-gate 			continue;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 		/*
1987c478bd9Sstevel@tonic-gate 		 * Clean up the token and see which keyword it is.  Call
1997c478bd9Sstevel@tonic-gate 		 * the appropriate routine to process the rest of the line.
2007c478bd9Sstevel@tonic-gate 		 */
2017c478bd9Sstevel@tonic-gate 		clean_token(cleaned, token);
2027c478bd9Sstevel@tonic-gate 		if (strcmp(cleaned, "slices") == 0) {
2037c478bd9Sstevel@tonic-gate 			ret_val = sup_setpart(vt);
2047c478bd9Sstevel@tonic-gate 			(void) fclose(data_file);
2057c478bd9Sstevel@tonic-gate 			return (ret_val);
2067c478bd9Sstevel@tonic-gate 		} else {
2077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Unknown keyword '%s'"),
2087c478bd9Sstevel@tonic-gate 			    cleaned);
2097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2107c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
2117c478bd9Sstevel@tonic-gate 			(void) fclose(data_file);
2127c478bd9Sstevel@tonic-gate 			return (-1);
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * Close the data file.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	(void) fclose(data_file);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
2217c478bd9Sstevel@tonic-gate 		gettext("Unexpected end of file (line no %d)\n"), data_lineno);
2227c478bd9Sstevel@tonic-gate 	return (-1);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static int32_t
2267c478bd9Sstevel@tonic-gate sup_gettoken(char *buf)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * Skip end of lines and blank lines.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	while ((last_token_type = sup_get_token(buf)) == SUP_EOL)
2327c478bd9Sstevel@tonic-gate 			;
2337c478bd9Sstevel@tonic-gate 	return (last_token_type);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static int32_t
2377c478bd9Sstevel@tonic-gate sup_get_token(char *buf)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	char	*ptr = buf;
2407c478bd9Sstevel@tonic-gate 	int32_t	c, quoted = 0;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Was an end of file detected last try?
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (feof(data_file)) {
2477c478bd9Sstevel@tonic-gate 		return (SUP_EOF);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * Zero out the returned token buffer
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	bzero(buf, TOKEN_SIZE + 1);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Strip off leading white-space.
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	while (isspace(c = sup_inputchar()))
2607c478bd9Sstevel@tonic-gate 		;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * Only white spaces and then end of file?
2647c478bd9Sstevel@tonic-gate 	 */
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (feof(data_file)) {
2677c478bd9Sstevel@tonic-gate 		return (SUP_EOF);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Read in characters until we hit unquoted white-space.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	for (; !isspace(c) || quoted; c = sup_inputchar()) {
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		/*
2767c478bd9Sstevel@tonic-gate 		 * If we hit eof, check if we have anything in buffer.
2777c478bd9Sstevel@tonic-gate 		 * if we have, return STRING, next time we will return EOF
2787c478bd9Sstevel@tonic-gate 		 * else, return EOF here...should not happen.
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 		if (feof(data_file)) {
2817c478bd9Sstevel@tonic-gate 			if (ptr - buf > 0) {
2827c478bd9Sstevel@tonic-gate 				return (SUP_STRING);
2837c478bd9Sstevel@tonic-gate 			} else {
2847c478bd9Sstevel@tonic-gate 				return (SUP_EOF);
2857c478bd9Sstevel@tonic-gate 			}
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		/*
2897c478bd9Sstevel@tonic-gate 		 * If we hit a double quote, change the state of quoting.
2907c478bd9Sstevel@tonic-gate 		 */
2917c478bd9Sstevel@tonic-gate 		if (c == '"') {
2927c478bd9Sstevel@tonic-gate 			quoted = !quoted;
2937c478bd9Sstevel@tonic-gate 			continue;
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 		/*
2967c478bd9Sstevel@tonic-gate 		 * If we hit a newline, that delimits a token.
2977c478bd9Sstevel@tonic-gate 		 */
2987c478bd9Sstevel@tonic-gate 		if (c == '\n')
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 		/*
3017c478bd9Sstevel@tonic-gate 		 * If we hit any nonquoted special delimiters, that delimits
3027c478bd9Sstevel@tonic-gate 		 * a token.
3037c478bd9Sstevel@tonic-gate 		 */
3047c478bd9Sstevel@tonic-gate 		if (!quoted && (c == '=' || c == ',' || c == ':' ||
3057c478bd9Sstevel@tonic-gate 			c == '#' || c == '|' || c == '&' || c == '~'))
3067c478bd9Sstevel@tonic-gate 				break;
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * Store the character if there's room left.
3097c478bd9Sstevel@tonic-gate 		 */
3107c478bd9Sstevel@tonic-gate 		if (ptr - buf < TOKEN_SIZE)
3117c478bd9Sstevel@tonic-gate 			*ptr++ = (char)c;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * If we stored characters in the buffer, then we inputted a string.
3157c478bd9Sstevel@tonic-gate 	 * Push the delimiter back into the pipe and return the string.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	if (ptr - buf > 0) {
3187c478bd9Sstevel@tonic-gate 		sup_pushchar(c);
3197c478bd9Sstevel@tonic-gate 		return (SUP_STRING);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * We didn't input a string, so we must have inputted a known delimiter.
3237c478bd9Sstevel@tonic-gate 	 * store the delimiter in the buffer, so it will get returned.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	buf[0] = c;
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Switch on the delimiter.  Return the appropriate value for each one.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	switch (c) {
3307c478bd9Sstevel@tonic-gate 	case '=':
3317c478bd9Sstevel@tonic-gate 		return (SUP_EQL);
3327c478bd9Sstevel@tonic-gate 	case ':':
3337c478bd9Sstevel@tonic-gate 		return (SUP_COLON);
3347c478bd9Sstevel@tonic-gate 	case ',':
3357c478bd9Sstevel@tonic-gate 		return (SUP_COMMA);
3367c478bd9Sstevel@tonic-gate 	case '\n':
3377c478bd9Sstevel@tonic-gate 		return (SUP_EOL);
3387c478bd9Sstevel@tonic-gate 	case '|':
3397c478bd9Sstevel@tonic-gate 		return (SUP_OR);
3407c478bd9Sstevel@tonic-gate 	case '&':
3417c478bd9Sstevel@tonic-gate 		return (SUP_AND);
3427c478bd9Sstevel@tonic-gate 	case '~':
3437c478bd9Sstevel@tonic-gate 		return (SUP_TILDE);
3447c478bd9Sstevel@tonic-gate 	case '#':
3457c478bd9Sstevel@tonic-gate 		/*
3467c478bd9Sstevel@tonic-gate 		 * For comments, we flush out the rest of the line and return
3477c478bd9Sstevel@tonic-gate 		 * an eol.
3487c478bd9Sstevel@tonic-gate 		 */
3497c478bd9Sstevel@tonic-gate 		while ((c = sup_inputchar()) != '\n' && !feof(data_file))
3507c478bd9Sstevel@tonic-gate 			;
3517c478bd9Sstevel@tonic-gate 		if (feof(data_file))
3527c478bd9Sstevel@tonic-gate 			return (SUP_EOF);
3537c478bd9Sstevel@tonic-gate 		else
3547c478bd9Sstevel@tonic-gate 			return (SUP_EOL);
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * Shouldn't ever get here.
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	default:
3597c478bd9Sstevel@tonic-gate 		return (SUP_STRING);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate static int32_t
3637c478bd9Sstevel@tonic-gate sup_inputchar()
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	int32_t	c;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Input the character.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	c = getc(data_file);
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * If it's not a backslash, return it.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * It was a backslash.  Get the next character.
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (c == '\\')
3807c478bd9Sstevel@tonic-gate 		c = getc(data_file);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * If it was a newline, update the line counter and get the next
3847c478bd9Sstevel@tonic-gate 	 * character.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 	if (c == '\n') {
3877c478bd9Sstevel@tonic-gate 		data_lineno++;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * Return the character.
3917c478bd9Sstevel@tonic-gate 	 */
3927c478bd9Sstevel@tonic-gate 	return (c);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate static void
3967c478bd9Sstevel@tonic-gate sup_pushchar(int32_t c)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	(void) ungetc(c, data_file);
4007c478bd9Sstevel@tonic-gate 	if (c == '\n')
4017c478bd9Sstevel@tonic-gate 		data_lineno--;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static void
4057c478bd9Sstevel@tonic-gate clean_token(char *cleantoken, char *token)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	char	*ptr;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/*
4107c478bd9Sstevel@tonic-gate 	 * Strip off leading white-space.
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	for (ptr = token; isspace(*ptr) && (ptr <=
4137c478bd9Sstevel@tonic-gate 	    (token + strlen(token) - 1)); ptr++);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * Copy it into the clean buffer.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	(void) strcpy(cleantoken, ptr);
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * Strip off trailing white-space.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	for (ptr = cleantoken + strlen(cleantoken) - 1;
4237c478bd9Sstevel@tonic-gate 			isspace(*ptr) && (ptr >= cleantoken); ptr--) {
4247c478bd9Sstevel@tonic-gate 		*ptr = '\0';
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate static int32_t
4297c478bd9Sstevel@tonic-gate sup_setpart(struct vtoc *vt)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	TOKEN	token, cleaned, ident;
4327c478bd9Sstevel@tonic-gate 	int32_t	i, index, status, val1, val2;
4337c478bd9Sstevel@tonic-gate 	ushort_t	vtoc_tag = 0xFFFF;
4347c478bd9Sstevel@tonic-gate 	ushort_t	vtoc_flag = 0xFFFF;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Pull in some grammar.
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		if (status != SUP_COLON) {
4437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4447c478bd9Sstevel@tonic-gate 				gettext("Expecting ':', found '%s'"), token);
4457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4467c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
4477c478bd9Sstevel@tonic-gate 			return (-1);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	for (;;) {
4517c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
4527c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
4537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4547c478bd9Sstevel@tonic-gate 				gettext("Expecting string, found '%s'"), token);
4557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4567c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
4577c478bd9Sstevel@tonic-gate 			return (-1);
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		clean_token(ident, token);
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * Also make sure it is within the legal range of letters.
4627c478bd9Sstevel@tonic-gate 		 */
4637c478bd9Sstevel@tonic-gate 		/*
4647c478bd9Sstevel@tonic-gate 		 * Here's the index of the partition we're dealing with
4657c478bd9Sstevel@tonic-gate 		 */
4667c478bd9Sstevel@tonic-gate 		index = (int32_t)my_atoll(ident);
4677c478bd9Sstevel@tonic-gate 		if ((index < 0) || (index > NDKMAP)) {
4687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4697c478bd9Sstevel@tonic-gate 				gettext("Unknown partition '%s'"), index);
4707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4717c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
4727c478bd9Sstevel@tonic-gate 			return (-1);
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 		/*
4757c478bd9Sstevel@tonic-gate 		 * Check for floppy and PCMCIA_MEM cards.
4767c478bd9Sstevel@tonic-gate 		 * for floppy, the partition no. can be 0 1 2.
4777c478bd9Sstevel@tonic-gate 		 * for PCMCIA, the partition no. can be 2
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		if (med_info.sm_media_type == SM_FLOPPY) {
4807c478bd9Sstevel@tonic-gate 			if ((index < 0) || (index > 2)) {
4817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4827c478bd9Sstevel@tonic-gate 			"Floppy can have partitions 0 1 and 2\n"));
4837c478bd9Sstevel@tonic-gate 				return (-1);
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		if (med_info.sm_media_type == SM_PCMCIA_MEM) {
4877c478bd9Sstevel@tonic-gate 			if (index != 2) {
4887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4897c478bd9Sstevel@tonic-gate 			"PCMCIA Memory cards can have partition 2 only.\n"));
4907c478bd9Sstevel@tonic-gate 				return (-1);
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		DPRINTF1("\n Partition %d: ", index);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
4977c478bd9Sstevel@tonic-gate 		if (status != SUP_EQL) {
4987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4997c478bd9Sstevel@tonic-gate 				gettext("Expecting '=', found '%s'"), token);
5007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5017c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
5027c478bd9Sstevel@tonic-gate 			return (-1);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * If we hit a key character, it's an error.
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
5127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5137c478bd9Sstevel@tonic-gate 				gettext("Expecting value, found '%s'"), token);
5147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5157c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
5167c478bd9Sstevel@tonic-gate 			return (-1);
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 		clean_token(cleaned, token);
5197c478bd9Sstevel@tonic-gate 		/*
5207c478bd9Sstevel@tonic-gate 		 * <tag> may be one of: boot, root, swap, etc.
5217c478bd9Sstevel@tonic-gate 		 * <flag> consists of two characters:
5227c478bd9Sstevel@tonic-gate 		 *	W (writable) or R (read-only)
5237c478bd9Sstevel@tonic-gate 		 *	M (mountable) or U (unmountable)
5247c478bd9Sstevel@tonic-gate 		 *
5257c478bd9Sstevel@tonic-gate 		 * Start with the defaults assigned above:
5267c478bd9Sstevel@tonic-gate 		 */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		/*
5297c478bd9Sstevel@tonic-gate 		 * All other attributes have a pair of numeric values.
5307c478bd9Sstevel@tonic-gate 		 * Convert the first value to a number.  This value
5317c478bd9Sstevel@tonic-gate 		 * is the starting cylinder number of the partition.
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		/* Check for valid partition, e.g. > 8 or 16 */
5357c478bd9Sstevel@tonic-gate 		val1 = str2sector(cleaned);
5367c478bd9Sstevel@tonic-gate 		if (val1 == -1) {
5377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5387c478bd9Sstevel@tonic-gate 				gettext("Invalid partition beggining %s \n"),
5397c478bd9Sstevel@tonic-gate 			    cleaned);
5407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5417c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		DPRINTF1(" begins %s", cleaned);
5457c478bd9Sstevel@tonic-gate 		/*
5467c478bd9Sstevel@tonic-gate 		 * Pull in some grammar.
5477c478bd9Sstevel@tonic-gate 		 */
5487c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5497c478bd9Sstevel@tonic-gate 		if (status != SUP_COMMA) {
5507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5517c478bd9Sstevel@tonic-gate 				gettext("Expecting ', ', found '%s'"), token);
5527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5537c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
5547c478bd9Sstevel@tonic-gate 			return (-1);
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 		/*
5577c478bd9Sstevel@tonic-gate 		 * Pull in the second value.
5587c478bd9Sstevel@tonic-gate 		 */
5597c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5607c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
5617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5627c478bd9Sstevel@tonic-gate 				gettext("Expecting value, found '%s'"), token);
5637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5647c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
5657c478bd9Sstevel@tonic-gate 			return (-1);
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		clean_token(cleaned, token);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		val2 = str2sector(cleaned);
5707c478bd9Sstevel@tonic-gate 		if (val2 == -1) {
5717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5727c478bd9Sstevel@tonic-gate 				gettext("Invalid partition size %s \n"),
5737c478bd9Sstevel@tonic-gate 			    cleaned);
5747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5757c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		DPRINTF1(" ends %s ", cleaned);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		/*
5807c478bd9Sstevel@tonic-gate 		 * Pull in some grammar.
5817c478bd9Sstevel@tonic-gate 		 */
5827c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		if (status == SUP_COMMA) {
5857c478bd9Sstevel@tonic-gate 			/* tags and flags  */
5867c478bd9Sstevel@tonic-gate 			status = sup_gettoken(token);
5877c478bd9Sstevel@tonic-gate 			if (status != SUP_STRING) {
5887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5897c478bd9Sstevel@tonic-gate 					gettext("Expecting value, found '%s'"),
5907c478bd9Sstevel@tonic-gate 				    token);
5917c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5927c478bd9Sstevel@tonic-gate 					gettext("Line no %d\n"), data_lineno);
5937c478bd9Sstevel@tonic-gate 				return (-1);
5947c478bd9Sstevel@tonic-gate 			}
5957c478bd9Sstevel@tonic-gate 			clean_token(cleaned, token);
5967c478bd9Sstevel@tonic-gate 			if (find_value(pflag_choices, cleaned, &i) == 1) {
5977c478bd9Sstevel@tonic-gate 				/*
5987c478bd9Sstevel@tonic-gate 				 * Found valid tag. Use it and advance parser
5997c478bd9Sstevel@tonic-gate 				 */
6007c478bd9Sstevel@tonic-gate 				DPRINTF1(" flag = %s", cleaned);
6017c478bd9Sstevel@tonic-gate 				vtoc_flag = (ushort_t)i;
6027c478bd9Sstevel@tonic-gate 				status = sup_gettoken(token);
6037c478bd9Sstevel@tonic-gate 			} else if (find_value(ptag_choices, cleaned, &i) == 1) {
6047c478bd9Sstevel@tonic-gate 				DPRINTF1(" tag = %s", cleaned);
6057c478bd9Sstevel@tonic-gate 				vtoc_tag = (ushort_t)i;
6067c478bd9Sstevel@tonic-gate 				status = sup_gettoken(token);
6077c478bd9Sstevel@tonic-gate 				if (status == SUP_COMMA) {
6087c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6097c478bd9Sstevel@tonic-gate 						gettext("Expecting : got %s\n"),
6107c478bd9Sstevel@tonic-gate 					    token);
6117c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6127c478bd9Sstevel@tonic-gate 						gettext("Line no %d\n"),
6137c478bd9Sstevel@tonic-gate 					    data_lineno);
6147c478bd9Sstevel@tonic-gate 					return (-1);
6157c478bd9Sstevel@tonic-gate 				}
6167c478bd9Sstevel@tonic-gate 			} else {
6177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6187c478bd9Sstevel@tonic-gate 					gettext("Invalid flag or tag\n"));
6197c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6207c478bd9Sstevel@tonic-gate 					gettext("Line no %d\n"), data_lineno);
6217c478bd9Sstevel@tonic-gate 				return (-1);
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 			if (status == SUP_COMMA) {
6267c478bd9Sstevel@tonic-gate 					/* Can be tag only */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 					status = sup_gettoken(token);
6297c478bd9Sstevel@tonic-gate 					if (status != SUP_STRING) {
6307c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6317c478bd9Sstevel@tonic-gate 						    gettext(
6327c478bd9Sstevel@tonic-gate 					"Expecting value, found '%s'"),
6337c478bd9Sstevel@tonic-gate 						token);
6347c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6357c478bd9Sstevel@tonic-gate 							gettext("Line no %d\n"),
6367c478bd9Sstevel@tonic-gate 						    data_lineno);
6377c478bd9Sstevel@tonic-gate 						return (-1);
6387c478bd9Sstevel@tonic-gate 					}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 					clean_token(cleaned, token);
6417c478bd9Sstevel@tonic-gate 					if (find_value(ptag_choices,
6427c478bd9Sstevel@tonic-gate 					    cleaned, &i) == 1) {
6437c478bd9Sstevel@tonic-gate 						DPRINTF1(" tag = %s", cleaned);
6447c478bd9Sstevel@tonic-gate 						vtoc_tag = (ushort_t)i;
6457c478bd9Sstevel@tonic-gate 					}
6467c478bd9Sstevel@tonic-gate 			status = sup_gettoken(token);
6477c478bd9Sstevel@tonic-gate 			}
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		/*
6517c478bd9Sstevel@tonic-gate 		 * Fill in the appropriate map entry with the values.
6527c478bd9Sstevel@tonic-gate 		 */
6537c478bd9Sstevel@tonic-gate 		vt->v_part[index].p_start = val1;
6547c478bd9Sstevel@tonic-gate 		vt->v_part[index].p_size = val2;
6557c478bd9Sstevel@tonic-gate 		if (vtoc_tag != 0xFFFF) {
6567c478bd9Sstevel@tonic-gate 			vt->v_part[index].p_tag = vtoc_tag;
6577c478bd9Sstevel@tonic-gate 			vtoc_tag = 0xFFFF;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 		if (vtoc_flag != 0xFFFF) {
6607c478bd9Sstevel@tonic-gate 			vt->v_part[index].p_flag = vtoc_flag;
6617c478bd9Sstevel@tonic-gate 			vtoc_flag = 0xFFFF;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		if (status == SUP_EOF) {
6647c478bd9Sstevel@tonic-gate 			DPRINTF("\nEnd of file\n");
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		if (status != SUP_COLON) {
6687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6697c478bd9Sstevel@tonic-gate 				gettext("Expecting ':', found '%s'"), token);
6707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6717c478bd9Sstevel@tonic-gate 				gettext("Line no %d\n"), data_lineno);
6727c478bd9Sstevel@tonic-gate 			return (-1);
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	return (0);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static int32_t
6807c478bd9Sstevel@tonic-gate find_value(slist_t *slist, char *match_str, int32_t *match_value)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	int32_t	i;
6837c478bd9Sstevel@tonic-gate 	int32_t	nmatches;
6847c478bd9Sstevel@tonic-gate 	int32_t	length;
6857c478bd9Sstevel@tonic-gate 	int32_t	match_length;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	nmatches = 0;
6887c478bd9Sstevel@tonic-gate 	length = 0;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	match_length = strlen(match_str);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	for (; slist->str != NULL; slist++) {
6937c478bd9Sstevel@tonic-gate 		/*
6947c478bd9Sstevel@tonic-gate 		 * See how many characters of the token match
6957c478bd9Sstevel@tonic-gate 		 */
6967c478bd9Sstevel@tonic-gate 		i = strcnt(match_str, slist->str);
6977c478bd9Sstevel@tonic-gate 		/*
6987c478bd9Sstevel@tonic-gate 		 * If it's not the whole token, then it's not a match.
6997c478bd9Sstevel@tonic-gate 		 */
7007c478bd9Sstevel@tonic-gate 		if (i  < match_length) {
7017c478bd9Sstevel@tonic-gate 			continue;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * If it ties with another input, remember that.
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 		if (i == length)
7077c478bd9Sstevel@tonic-gate 			nmatches++;
7087c478bd9Sstevel@tonic-gate 		/*
7097c478bd9Sstevel@tonic-gate 		 * If it matches the most so far, record that.
7107c478bd9Sstevel@tonic-gate 		 */
7117c478bd9Sstevel@tonic-gate 		if (i > length) {
7127c478bd9Sstevel@tonic-gate 			*match_value = slist->value;
7137c478bd9Sstevel@tonic-gate 			nmatches = 1;
7147c478bd9Sstevel@tonic-gate 			length = i;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	return (nmatches);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate static int32_t
7227c478bd9Sstevel@tonic-gate strcnt(char	*s1, char *s2)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	int32_t	i = 0;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	while ((*s1 != '\0') && (*s1++ == *s2++))
7277c478bd9Sstevel@tonic-gate 		i++;
7287c478bd9Sstevel@tonic-gate 	return (i);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static int32_t
7327c478bd9Sstevel@tonic-gate str2sector(char *str)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	int32_t mul_factor = 1;
7357c478bd9Sstevel@tonic-gate 	char *s1, *s2, *base;
7367c478bd9Sstevel@tonic-gate 	int32_t num_sectors;
7377c478bd9Sstevel@tonic-gate 	int64_t size;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	base = s2 = (char *)malloc(strlen(str) + 1);
7407c478bd9Sstevel@tonic-gate 	if (s2 == NULL) {
7417c478bd9Sstevel@tonic-gate 		PERROR("Malloc failed");
7427c478bd9Sstevel@tonic-gate 		return (-1);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	*s2 = '\0';
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	s1 = str;
7497c478bd9Sstevel@tonic-gate 	while (*s1) {
7507c478bd9Sstevel@tonic-gate 		if ((*s1 != 'x') && ((*s1 < 'A') || (*s1 > 'F')) &&
7517c478bd9Sstevel@tonic-gate 			((*s1 < 'a') || (*s1 > 'f')) && ((*s1 < '0') ||
7527c478bd9Sstevel@tonic-gate 			(*s1 > '9'))) {
7537c478bd9Sstevel@tonic-gate 			if (*s1 == 'G') {
7547c478bd9Sstevel@tonic-gate 					mul_factor = 1024*1024*1024;
7557c478bd9Sstevel@tonic-gate 					s1++;
7567c478bd9Sstevel@tonic-gate 			} else if (*s1 == 'M') {
7577c478bd9Sstevel@tonic-gate 					mul_factor = 1024*1024;
7587c478bd9Sstevel@tonic-gate 					s1++;
7597c478bd9Sstevel@tonic-gate 			} else if (*s1 == 'K') {
7607c478bd9Sstevel@tonic-gate 					mul_factor = 1024;
7617c478bd9Sstevel@tonic-gate 					s1++;
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate 			if ((*s1 != 'B') || (*(++s1) != NULL)) {
7647c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
7657c478bd9Sstevel@tonic-gate 					gettext("Extra chars at the end\n"));
7667c478bd9Sstevel@tonic-gate 				free(base);
7677c478bd9Sstevel@tonic-gate 				return (-1);
7687c478bd9Sstevel@tonic-gate 			}
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		} else {
7717c478bd9Sstevel@tonic-gate 			*s2++ = *s1++;
7727c478bd9Sstevel@tonic-gate 			*s2 = '\0';
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	*s2 = NULL;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	size = my_atoll(base);
7787c478bd9Sstevel@tonic-gate 	if ((!mul_factor) || (size == -1)) {
7797c478bd9Sstevel@tonic-gate 		free(base);
7807c478bd9Sstevel@tonic-gate 		return (-1);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	num_sectors = (uint64_t)size * (uint64_t)mul_factor /512;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	free(base);
7857c478bd9Sstevel@tonic-gate 	return (num_sectors);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate int32_t
7907c478bd9Sstevel@tonic-gate valid_slice_file(smedia_handle_t handle, int32_t fd, char *file_name,
7917c478bd9Sstevel@tonic-gate 	struct vtoc *vt)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	struct stat status;
7947c478bd9Sstevel@tonic-gate 	int32_t ret_val;
7957c478bd9Sstevel@tonic-gate 	if (stat(file_name, &status)) {
7967c478bd9Sstevel@tonic-gate 		PERROR(file_name);
7977c478bd9Sstevel@tonic-gate 		return (-1);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	(void) memset(vt, 0, sizeof (*vt));
8007c478bd9Sstevel@tonic-gate 	/* Set default tag and flag */
8017c478bd9Sstevel@tonic-gate #ifdef sparc
8027c478bd9Sstevel@tonic-gate 	vt->v_part[0].p_tag = V_ROOT;
8037c478bd9Sstevel@tonic-gate 	vt->v_part[1].p_tag = V_SWAP;
8047c478bd9Sstevel@tonic-gate 	vt->v_part[2].p_tag = V_BACKUP;
8057c478bd9Sstevel@tonic-gate 	vt->v_part[6].p_tag = V_USR;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	vt->v_part[1].p_flag = V_UNMNT; /* Unmountable */
8087c478bd9Sstevel@tonic-gate 	vt->v_part[2].p_flag = V_UNMNT; /* Unmountable */
8097c478bd9Sstevel@tonic-gate #endif
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	ret_val = sup_prxfile(file_name, vt);
8127c478bd9Sstevel@tonic-gate 	if (ret_val < 0)
8137c478bd9Sstevel@tonic-gate 		return (-1);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate #ifdef DEBUG
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	int32_t i;
8187c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
8197c478bd9Sstevel@tonic-gate 		DPRINTF1("\npart %d\n", i);
8207c478bd9Sstevel@tonic-gate 		DPRINTF1("\t start %d",  (int32_t)vt->v_part[i].p_start);
8217c478bd9Sstevel@tonic-gate 		DPRINTF1("\t size %d ", (int32_t)vt->v_part[i].p_size);
8227c478bd9Sstevel@tonic-gate 		DPRINTF1("\t tag %d", vt->v_part[i].p_tag);
8237c478bd9Sstevel@tonic-gate 		DPRINTF1("\t flag %d", vt->v_part[i].p_flag);
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8277c478bd9Sstevel@tonic-gate 	if (check_vtoc_sanity(handle, fd, vt) < 0) {
8287c478bd9Sstevel@tonic-gate 		return (-1);
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate #ifdef DEBUG
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	int32_t i;
8337c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
8347c478bd9Sstevel@tonic-gate 		DPRINTF1("\npart %d\n", i);
8357c478bd9Sstevel@tonic-gate 		DPRINTF1("\t start %d",  (int32_t)vt->v_part[i].p_start);
8367c478bd9Sstevel@tonic-gate 		DPRINTF1("\t size %d ", (int32_t)vt->v_part[i].p_size);
8377c478bd9Sstevel@tonic-gate 		DPRINTF1("\t tag %d", vt->v_part[i].p_tag);
8387c478bd9Sstevel@tonic-gate 		DPRINTF1("\t flag %d", vt->v_part[i].p_flag);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8427c478bd9Sstevel@tonic-gate 	return (0);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate #define	SWAP(a, b)	{int32_t tmp; tmp = (a); (a) = (b); (b) = tmp; }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * On x86 Solaris, the partitioning is done in two levels, fdisk and Solaris
8497c478bd9Sstevel@tonic-gate  * VTOC. Where as, on sparc solaris, it is only VTOC. On floppy and PCMCIA
8507c478bd9Sstevel@tonic-gate  * also it is assumed to be only VTOC, no fdisk.
8517c478bd9Sstevel@tonic-gate  *
8527c478bd9Sstevel@tonic-gate  * On sparc, the back up slice can cover the whole medium. But on x86
8537c478bd9Sstevel@tonic-gate  * (SCSI/ATAPI disks), the backup slice can cover the solaris partition
8547c478bd9Sstevel@tonic-gate  * in fdisk table.
8557c478bd9Sstevel@tonic-gate  *	Following table describes how is it handled
8567c478bd9Sstevel@tonic-gate  * SPARC:
8577c478bd9Sstevel@tonic-gate  * 	SCSI/ATAPI, floppy, pcmcia : don't check for fdisk.
8587c478bd9Sstevel@tonic-gate  *				DKIOCGGEOM is sufficient.
8597c478bd9Sstevel@tonic-gate  * x86 : floppy, pcmcia : Don't check for fdisk. DKIOCGGEOM is sufficient.
8607c478bd9Sstevel@tonic-gate  * 	SCSI/ATAPI : Check for fdisk.
8617c478bd9Sstevel@tonic-gate  *			if not present, assume that the solaris
8627c478bd9Sstevel@tonic-gate  *				partition covers 100% of the medium
8637c478bd9Sstevel@tonic-gate  * 				(minus one cylinder).
8647c478bd9Sstevel@tonic-gate  *
8657c478bd9Sstevel@tonic-gate  * 		if present :
8667c478bd9Sstevel@tonic-gate  *				check for active solaris partition.
8677c478bd9Sstevel@tonic-gate  * 				if not found, take the first solaris
8687c478bd9Sstevel@tonic-gate  *					partition.
8697c478bd9Sstevel@tonic-gate  *			If there are no solaris partitions, its an error, stop.
8707c478bd9Sstevel@tonic-gate  */
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate static int32_t
8737c478bd9Sstevel@tonic-gate check_vtoc_sanity(smedia_handle_t handle, int32_t fd, struct vtoc *vt)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	int32_t i, j;
8777c478bd9Sstevel@tonic-gate 	struct dk_geom dkg;
8787c478bd9Sstevel@tonic-gate 	int32_t num_backup = 0;
8797c478bd9Sstevel@tonic-gate 	long backup_size = 0;
8807c478bd9Sstevel@tonic-gate 	struct part_struct {
8817c478bd9Sstevel@tonic-gate 		long start;
8827c478bd9Sstevel@tonic-gate 		long end;
8837c478bd9Sstevel@tonic-gate 		int32_t num;
8847c478bd9Sstevel@tonic-gate 	} part[NDKMAP];
8857c478bd9Sstevel@tonic-gate 	long min_val, min_slice, num_slices;
8867c478bd9Sstevel@tonic-gate 	long media_size;
8877c478bd9Sstevel@tonic-gate 	int32_t cyl_size;
8887c478bd9Sstevel@tonic-gate 	int sparc_style = 0;	/* sparc_style handling ? */
8897c478bd9Sstevel@tonic-gate 	struct fdisk_info fdisk;
8907c478bd9Sstevel@tonic-gate 	int sol_part;
8917c478bd9Sstevel@tonic-gate 	int total_parts = 0;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate #ifdef sparc
8947c478bd9Sstevel@tonic-gate 	sparc_style = 1;
8957c478bd9Sstevel@tonic-gate #endif /* sparc */
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	if ((med_info.sm_media_type == SM_FLOPPY) ||
8987c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_PCMCIA_MEM) ||
8997c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_PCMCIA_ATA) ||
9007c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_SCSI_FLOPPY)) {
9017c478bd9Sstevel@tonic-gate 		sparc_style = 1;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (sparc_style) {
9057c478bd9Sstevel@tonic-gate 		DPRINTF("sparc style true\n");
9067c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) {
9077c478bd9Sstevel@tonic-gate 			PERROR("DKIOCGGEOM Failed");
9087c478bd9Sstevel@tonic-gate 			return (-1);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		media_size = dkg.dkg_ncyl * dkg.dkg_nhead * dkg.dkg_nsect;
9117c478bd9Sstevel@tonic-gate 		cyl_size = dkg.dkg_nhead * dkg.dkg_nsect;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (!sparc_style) {
9157c478bd9Sstevel@tonic-gate 	/*
9167c478bd9Sstevel@tonic-gate 	 * Try to get the fdisk information if available.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 		if (get_fdisk(handle, fd, 0, &fdisk) >= 0) {
9197c478bd9Sstevel@tonic-gate 			/* fdisk table on disk */
9207c478bd9Sstevel@tonic-gate 			sol_part = 0xFF;
9217c478bd9Sstevel@tonic-gate 			for (i = 0; i < FD_NUMPART; i++) {
9227c478bd9Sstevel@tonic-gate 				if (fdisk.part[i].systid == SUNIXOS ||
9237c478bd9Sstevel@tonic-gate 				    fdisk.part[i].systid == SUNIXOS2) {
9247c478bd9Sstevel@tonic-gate 					if (sol_part == 0xFF)
9257c478bd9Sstevel@tonic-gate 						sol_part = i;
9267c478bd9Sstevel@tonic-gate 					total_parts++;
9277c478bd9Sstevel@tonic-gate 					if (fdisk.part[i].bootid == ACTIVE)
9287c478bd9Sstevel@tonic-gate 						sol_part = i;
9297c478bd9Sstevel@tonic-gate 				}
9307c478bd9Sstevel@tonic-gate 			}
9317c478bd9Sstevel@tonic-gate 			if (sol_part == 0xFF) {
9327c478bd9Sstevel@tonic-gate 				/* No Solaris partition */
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("No FDISK \
9357c478bd9Sstevel@tonic-gate Solaris partition found!\n"));
9367c478bd9Sstevel@tonic-gate 				return (-1);
9377c478bd9Sstevel@tonic-gate 			}
9387c478bd9Sstevel@tonic-gate 			if (total_parts > 1)
9397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("Multiple FDISK \
9407c478bd9Sstevel@tonic-gate Solaris partitions found.\n"));
9417c478bd9Sstevel@tonic-gate 			media_size = fdisk.part[sol_part].numsect;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 			DPRINTF1("sol_part %d\n", sol_part);
9447c478bd9Sstevel@tonic-gate 			DPRINTF1("media_size %d\n", (int)media_size);
9457c478bd9Sstevel@tonic-gate 		} else {
9467c478bd9Sstevel@tonic-gate 			DPRINTF("Didn't get fdisk\n");
9477c478bd9Sstevel@tonic-gate 			/*
9487c478bd9Sstevel@tonic-gate 			 * No fdisk partition available. Assume a 100% Solaris.
9497c478bd9Sstevel@tonic-gate 			 * partition.
9507c478bd9Sstevel@tonic-gate 			 * Try getting disk geometry.
9517c478bd9Sstevel@tonic-gate 			 */
9527c478bd9Sstevel@tonic-gate 			if (ioctl(fd, DKIOCGGEOM, &dkg) < 0)
9537c478bd9Sstevel@tonic-gate 				if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) {
9547c478bd9Sstevel@tonic-gate 					DPRINTF("DKIOCG_PHYGEOM ioctl failed");
9557c478bd9Sstevel@tonic-gate 					return (-1);
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 			/* On x86 platform 1 cylinder is used for fdisk table */
9587c478bd9Sstevel@tonic-gate 			dkg.dkg_ncyl = dkg.dkg_ncyl - 1;
9597c478bd9Sstevel@tonic-gate 			media_size = dkg.dkg_ncyl * dkg.dkg_nhead *
9607c478bd9Sstevel@tonic-gate 			    dkg.dkg_nsect;
9617c478bd9Sstevel@tonic-gate 		}
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate #ifdef DEBUG
9657c478bd9Sstevel@tonic-gate 	DPRINTF1("Ncyl %d\n", dkg.dkg_ncyl);
9667c478bd9Sstevel@tonic-gate 	DPRINTF1("nhead %d\n", dkg.dkg_nhead);
9677c478bd9Sstevel@tonic-gate 	DPRINTF1("nsect %d\n", dkg.dkg_nsect);
9687c478bd9Sstevel@tonic-gate #endif /* DEBUG */
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (media_size == 0) {
9717c478bd9Sstevel@tonic-gate 		media_size = med_info.sm_capacity;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	(void) memset(&part, 0, sizeof (part));
9757c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < NDKMAP; i++) {
9767c478bd9Sstevel@tonic-gate 		if (vt->v_part[i].p_tag == V_BACKUP) {
9777c478bd9Sstevel@tonic-gate 			if (vt->v_part[i].p_start != 0) {
9787c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
9797c478bd9Sstevel@tonic-gate 				    gettext(
9807c478bd9Sstevel@tonic-gate 			"Backup slice should start at sector 0\n"));
9817c478bd9Sstevel@tonic-gate 			return (-1);
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 			backup_size = vt->v_part[i].p_size;
9847c478bd9Sstevel@tonic-gate 			num_backup++;
9857c478bd9Sstevel@tonic-gate 			continue;
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 		if (vt->v_part[i].p_size) {
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 			if (sparc_style) {
9907c478bd9Sstevel@tonic-gate 				if (vt->v_part[i].p_start % cyl_size) {
9917c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
9927c478bd9Sstevel@tonic-gate 					    gettext(
9937c478bd9Sstevel@tonic-gate 			"Slice %d does not start on cylinder boundary\n"), i);
9947c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
9957c478bd9Sstevel@tonic-gate 					    gettext(
9967c478bd9Sstevel@tonic-gate 			"Cylinder size %d 512 byte sectors\n"), cyl_size);
9977c478bd9Sstevel@tonic-gate 					return (-1);
9987c478bd9Sstevel@tonic-gate 				}
9997c478bd9Sstevel@tonic-gate 			}
10007c478bd9Sstevel@tonic-gate 			part[j].start = vt->v_part[i].p_start;
10017c478bd9Sstevel@tonic-gate 			part[j].end = vt->v_part[i].p_start +
10027c478bd9Sstevel@tonic-gate 			    vt->v_part[i].p_size -1;
10037c478bd9Sstevel@tonic-gate 			part[j].num = i;
10047c478bd9Sstevel@tonic-gate 			j++;
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	if (num_backup > 1) {
10087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
10097c478bd9Sstevel@tonic-gate 			gettext("Maximum one backup slice is allowed\n"));
10107c478bd9Sstevel@tonic-gate 		(void) smedia_release_handle(handle);
10117c478bd9Sstevel@tonic-gate 		(void) close(fd);
10127c478bd9Sstevel@tonic-gate 		exit(1);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 	num_slices = j;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_slices; i++) {
10177c478bd9Sstevel@tonic-gate 		min_val = part[i].start;
10187c478bd9Sstevel@tonic-gate 		min_slice = i;
10197c478bd9Sstevel@tonic-gate 		for (j = i+1; j < num_slices; j++) {
10207c478bd9Sstevel@tonic-gate 			if (part[j].start < min_val) {
10217c478bd9Sstevel@tonic-gate 				min_val = part[j].start;
10227c478bd9Sstevel@tonic-gate 				min_slice = j;
10237c478bd9Sstevel@tonic-gate 			}
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 		if (min_slice != i) {
10267c478bd9Sstevel@tonic-gate 			SWAP(part[i].start, part[min_slice].start)
10277c478bd9Sstevel@tonic-gate 			SWAP(part[i].end, part[min_slice].end)
10287c478bd9Sstevel@tonic-gate 			SWAP(part[i].num, part[min_slice].num)
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate #ifdef DEBUG
10337c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_slices; i++) {
10347c478bd9Sstevel@tonic-gate 		DPRINTF4("\n %d (%d) : %d, %d", i, part[i].num,
10357c478bd9Sstevel@tonic-gate 			part[i].start, part[i].end);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate #endif /* DEBUG */
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (backup_size > media_size) {
10407c478bd9Sstevel@tonic-gate 		if (sparc_style) {
10417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10427c478bd9Sstevel@tonic-gate 			    gettext(
10437c478bd9Sstevel@tonic-gate 			"Backup slice extends beyond size of media\n"));
10447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10457c478bd9Sstevel@tonic-gate 			    gettext("media size : %d sectors \n"), media_size);
10467c478bd9Sstevel@tonic-gate 		} else {
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10497c478bd9Sstevel@tonic-gate 			    gettext("Backup slice extends beyond size of FDISK \
10507c478bd9Sstevel@tonic-gate Solaris partition\n"));
10517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10527c478bd9Sstevel@tonic-gate 			    gettext(
10537c478bd9Sstevel@tonic-gate 			"FDISK Solaris partition size : %d sectors \n"),
10547c478bd9Sstevel@tonic-gate 			    media_size);
10557c478bd9Sstevel@tonic-gate 		}
10567c478bd9Sstevel@tonic-gate 		return (-1);
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * If we have only backup slice return success here.
10617c478bd9Sstevel@tonic-gate 	 */
10627c478bd9Sstevel@tonic-gate 	if (num_slices == 0)
10637c478bd9Sstevel@tonic-gate 		return (0);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (backup_size) {
10667c478bd9Sstevel@tonic-gate 		if (part[num_slices - 1].end > backup_size) {
10677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10687c478bd9Sstevel@tonic-gate 			    gettext("Slice %d extends beyond backup slice.\n"),
10697c478bd9Sstevel@tonic-gate 			    part[num_slices -1].num);
10707c478bd9Sstevel@tonic-gate 			return (-1);
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 	} else {
10737c478bd9Sstevel@tonic-gate 		if (part[num_slices - 1].end > media_size) {
10747c478bd9Sstevel@tonic-gate 			if (sparc_style) {
10757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10767c478bd9Sstevel@tonic-gate 				    gettext(
10777c478bd9Sstevel@tonic-gate 				"Slice %d extends beyond media size\n"),
10787c478bd9Sstevel@tonic-gate 				    part[num_slices -1].num);
10797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10807c478bd9Sstevel@tonic-gate 					gettext("media size : %d sectors \n"),
10817c478bd9Sstevel@tonic-gate 				    media_size);
10827c478bd9Sstevel@tonic-gate 			} else {
10837c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10847c478bd9Sstevel@tonic-gate 				    gettext(
10857c478bd9Sstevel@tonic-gate 		"Slice %d extends beyond FDISK Solaris partition size\n"),
10867c478bd9Sstevel@tonic-gate 			part[num_slices -1].num);
10877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10887c478bd9Sstevel@tonic-gate 				    gettext("FDISK Solaris partition size : %d \
10897c478bd9Sstevel@tonic-gate sectors \n"), media_size);
10907c478bd9Sstevel@tonic-gate 			}
10917c478bd9Sstevel@tonic-gate 			return (-1);
10927c478bd9Sstevel@tonic-gate 		}
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_slices; i++) {
10987c478bd9Sstevel@tonic-gate 		if (i == 0)
10997c478bd9Sstevel@tonic-gate 			continue;
11007c478bd9Sstevel@tonic-gate 		if (part[i].start <= part[i-1].end) {
11017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11027c478bd9Sstevel@tonic-gate 				gettext("Overlap between slices %d and %d\n"),
11037c478bd9Sstevel@tonic-gate 				part[i-1].num, part[i].num);
11047c478bd9Sstevel@tonic-gate 			(void) smedia_release_handle(handle);
11057c478bd9Sstevel@tonic-gate 			(void) close(fd);
11067c478bd9Sstevel@tonic-gate 			exit(1);
11077c478bd9Sstevel@tonic-gate 		}
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	return (0);
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate static int32_t
11157c478bd9Sstevel@tonic-gate get_fdisk(smedia_handle_t handle, int32_t fd, int32_t offset,
11167c478bd9Sstevel@tonic-gate 	struct fdisk_info *fdisk)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	struct mboot *boot_sec;
11197c478bd9Sstevel@tonic-gate 	struct ipart *part;
11207c478bd9Sstevel@tonic-gate 	char *buf;
11217c478bd9Sstevel@tonic-gate 	int32_t i, ret;
11227c478bd9Sstevel@tonic-gate 	int	save_errno;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/* Read the master boot program */
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	buf = (char *)malloc(med_info.sm_blocksize);
11277c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
11287c478bd9Sstevel@tonic-gate 		PERROR("malloc failed");
11297c478bd9Sstevel@tonic-gate 		exit(1);
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 	errno = 0;
11327c478bd9Sstevel@tonic-gate 	ret = ioctl(fd, DKIOCGMBOOT, buf);
11337c478bd9Sstevel@tonic-gate 	if (ret < 0) {
11347c478bd9Sstevel@tonic-gate 		if (errno != ENOTTY) {
11357c478bd9Sstevel@tonic-gate 			PERROR("DKIOCGMBOOT ioctl failed");
11367c478bd9Sstevel@tonic-gate 			return (-1);
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 
1139e3397557Szk 		/* Turn on privileges. */
1140e3397557Szk 		(void) __priv_bracket(PRIV_ON);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		ret = smedia_raw_read(handle, offset/med_info.sm_blocksize,
11437c478bd9Sstevel@tonic-gate 			buf, med_info.sm_blocksize);
11447c478bd9Sstevel@tonic-gate 
1145e3397557Szk 		/* Turn off privileges. */
1146e3397557Szk 		(void) __priv_bracket(PRIV_OFF);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		save_errno = errno;
11497c478bd9Sstevel@tonic-gate 		errno = save_errno;
11507c478bd9Sstevel@tonic-gate 		if (ret != med_info.sm_blocksize) {
11517c478bd9Sstevel@tonic-gate 			if (errno == ENOTSUP) {
11527c478bd9Sstevel@tonic-gate 				errno = 0;
11537c478bd9Sstevel@tonic-gate 				if (lseek(fd, offset, SEEK_SET)) {
11547c478bd9Sstevel@tonic-gate 				    PERROR("Seek failed:");
11557c478bd9Sstevel@tonic-gate 				    free(buf);
11567c478bd9Sstevel@tonic-gate 				    return (-1);
11577c478bd9Sstevel@tonic-gate 				}
11587c478bd9Sstevel@tonic-gate 
1159e3397557Szk 				/* Turn on privileges. */
1160e3397557Szk 				(void) __priv_bracket(PRIV_ON);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 				ret = read(fd, buf, sizeof (struct mboot));
11637c478bd9Sstevel@tonic-gate 
1164e3397557Szk 				/* Turn off privileges. */
1165e3397557Szk 				(void) __priv_bracket(PRIV_OFF);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 				if (ret != sizeof (struct mboot)) {
11687c478bd9Sstevel@tonic-gate 				    PERROR("Could not read master boot record");
11697c478bd9Sstevel@tonic-gate 				    free(buf);
11707c478bd9Sstevel@tonic-gate 				    return (-1);
11717c478bd9Sstevel@tonic-gate 				}
11727c478bd9Sstevel@tonic-gate 			} else {
11737c478bd9Sstevel@tonic-gate 				PERROR("Could not read master boot record");
11747c478bd9Sstevel@tonic-gate 				free(buf);
11757c478bd9Sstevel@tonic-gate 				return (-1);
11767c478bd9Sstevel@tonic-gate 			}
11777c478bd9Sstevel@tonic-gate 		}
11787c478bd9Sstevel@tonic-gate 	}
11797c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
11807c478bd9Sstevel@tonic-gate 	boot_sec = (struct mboot *)buf;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/* Is this really a master boot record? */
11837c478bd9Sstevel@tonic-gate 	if (les(boot_sec->signature) != MBB_MAGIC) {
11847c478bd9Sstevel@tonic-gate 		DPRINTF("fdisk: Invalid master boot file \n");
11857c478bd9Sstevel@tonic-gate 		DPRINTF2("Bad magic number: is %x, should be %x.\n",
11867c478bd9Sstevel@tonic-gate 			les(boot_sec->signature), MBB_MAGIC);
11877c478bd9Sstevel@tonic-gate 		free(buf);
11887c478bd9Sstevel@tonic-gate 		return (-1);
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
11927c478bd9Sstevel@tonic-gate 		DPRINTF1("part %d\n", i);
11937c478bd9Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
11947c478bd9Sstevel@tonic-gate 		part = (struct ipart *)&boot_sec->parts[i *
11957c478bd9Sstevel@tonic-gate 		    sizeof (struct ipart)];
11967c478bd9Sstevel@tonic-gate 		fdisk->part[i].bootid = part->bootid;
11977c478bd9Sstevel@tonic-gate 		if (part->bootid && (part->bootid != ACTIVE)) {
11987c478bd9Sstevel@tonic-gate 			/* Hmmm...not a valid fdisk! */
11997c478bd9Sstevel@tonic-gate 			return (-1);
12007c478bd9Sstevel@tonic-gate 		}
12017c478bd9Sstevel@tonic-gate 		fdisk->part[i].systid = part->systid;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 		/* To avoid the misalign access in sparc */
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		fdisk->part[i].relsect = lel(GET_32(&(part->relsect)));
12067c478bd9Sstevel@tonic-gate 		fdisk->part[i].numsect = lel(GET_32(&(part->numsect)));
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		DPRINTF1("\tboot id 0x%x\n", part->bootid);
12097c478bd9Sstevel@tonic-gate 		DPRINTF1("\tsystem id 0x%x\n", part->systid);
12107c478bd9Sstevel@tonic-gate 		DPRINTF1("\trel sector 0x%x\n", fdisk->part[i].relsect);
12117c478bd9Sstevel@tonic-gate 		DPRINTF1("\tnum sector 0x%x\n", fdisk->part[i].numsect);
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 	free(buf);
12147c478bd9Sstevel@tonic-gate 	return (0);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate /*
12197c478bd9Sstevel@tonic-gate  * wrrite_defualt_label(int32_t fd)
12207c478bd9Sstevel@tonic-gate  * 	fd = file descriptor for the device.
12217c478bd9Sstevel@tonic-gate  *
12227c478bd9Sstevel@tonic-gate  * For sparc solaris
12237c478bd9Sstevel@tonic-gate  *	Create a vtoc partition with
12247c478bd9Sstevel@tonic-gate  *		slice 0 = slice 2 = medium capacity.
12257c478bd9Sstevel@tonic-gate  *	The cyl, head, sect (CHS) values are computed as done in sd
12267c478bd9Sstevel@tonic-gate  *	capacity <= 1GB,
12277c478bd9Sstevel@tonic-gate  *		nhead = 64, nsect = 32
12287c478bd9Sstevel@tonic-gate  *	capacity > 1gb,
12297c478bd9Sstevel@tonic-gate  *		nhead = 255, nsect = 63
12307c478bd9Sstevel@tonic-gate  *
12317c478bd9Sstevel@tonic-gate  * For x86 solaris
12327c478bd9Sstevel@tonic-gate  *	Create a fdisk partition,
12337c478bd9Sstevel@tonic-gate  *		partition 0 covers the full medium, the partition
12347c478bd9Sstevel@tonic-gate  *		type is set to Solaris.
12357c478bd9Sstevel@tonic-gate  *	Then create solaris vtoc. The algorithm is same as sparc solaris.
12367c478bd9Sstevel@tonic-gate  *	But the capacity is reduced by 1 cyl, to leave space for fdisk table.
12377c478bd9Sstevel@tonic-gate  */
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate #ifdef sparc
12407c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12417c478bd9Sstevel@tonic-gate void
12427c478bd9Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	struct vtoc v_toc;
12467c478bd9Sstevel@tonic-gate 	int32_t nhead, numcyl, nsect, capacity;
12477c478bd9Sstevel@tonic-gate 	int32_t ret;
12487c478bd9Sstevel@tonic-gate 	char asciilabel[LEN_DKL_ASCII];
12497c478bd9Sstevel@tonic-gate 	char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0";
12507c478bd9Sstevel@tonic-gate 	int32_t acyl = 2;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	DPRINTF("Writing default vtoc\n");
12547c478bd9Sstevel@tonic-gate 	(void) memset(&v_toc, 0, sizeof (v_toc));
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	v_toc.v_nparts = V_NUMPAR;
12587c478bd9Sstevel@tonic-gate 	v_toc.v_sanity = VTOC_SANE;
12597c478bd9Sstevel@tonic-gate 	v_toc.v_version = V_VERSION;
12607c478bd9Sstevel@tonic-gate 	v_toc.v_sectorsz = DEV_BSIZE;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/*
12637c478bd9Sstevel@tonic-gate 	 * For the head, cyl and number of sector per track,
12647c478bd9Sstevel@tonic-gate 	 * if the capacity <= 1GB, head = 64, sect = 32.
12657c478bd9Sstevel@tonic-gate 	 * else head = 255, sect 63
12667c478bd9Sstevel@tonic-gate 	 * NOTE: the capacity should be equal to C*H*S values.
12677c478bd9Sstevel@tonic-gate 	 * This will cause some truncation of size due to
12687c478bd9Sstevel@tonic-gate 	 * round off errors.
12697c478bd9Sstevel@tonic-gate 	 */
12707c478bd9Sstevel@tonic-gate 	if (med_info.sm_capacity <= 0x200000) {
12717c478bd9Sstevel@tonic-gate 		nhead = 64;
12727c478bd9Sstevel@tonic-gate 		nsect = 32;
12737c478bd9Sstevel@tonic-gate 	} else {
12747c478bd9Sstevel@tonic-gate 		nhead = 255;
12757c478bd9Sstevel@tonic-gate 		nsect = 63;
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	numcyl = med_info.sm_capacity / (nhead * nsect);
12797c478bd9Sstevel@tonic-gate 	capacity = nhead * nsect * numcyl;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_start = 0;
12827c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_size = capacity;
12837c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_tag  = V_ROOT;
12847c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_flag = 0;	/* Mountable */
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_start = 0;
12877c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_size = capacity;
12887c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_tag  = V_BACKUP;
12897c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_flag = V_UNMNT;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/* Create asciilabel for compatibility with format utility */
12927c478bd9Sstevel@tonic-gate 	(void) snprintf(asciilabel, sizeof (asciilabel),
12937c478bd9Sstevel@tonic-gate 	    "%s cyl %d alt %d hd %d sec %d",
12947c478bd9Sstevel@tonic-gate 	    asciilabel2, numcyl, acyl, nhead, nsect);
12957c478bd9Sstevel@tonic-gate 	(void) memcpy(v_toc.v_asciilabel, asciilabel,
12967c478bd9Sstevel@tonic-gate 		LEN_DKL_ASCII);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	errno = 0;
12997c478bd9Sstevel@tonic-gate 
1300e3397557Szk 	/* Turn on privileges. */
1301e3397557Szk 	(void) __priv_bracket(PRIV_ON);
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	ret = write_vtoc(fd, &v_toc);
13047c478bd9Sstevel@tonic-gate 
1305e3397557Szk 	/* Turn off privileges. */
1306e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if (ret < 0) {
13097c478bd9Sstevel@tonic-gate 		PERROR("write VTOC failed");
13107c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate #else /* !sparc */
13157c478bd9Sstevel@tonic-gate #ifdef i386
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate void
13187c478bd9Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd)
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	int32_t i, ret;
13227c478bd9Sstevel@tonic-gate 	struct dk_geom  dkg;
13237c478bd9Sstevel@tonic-gate 	struct vtoc v_toc;
13247c478bd9Sstevel@tonic-gate 	int tmp_fd;
13257c478bd9Sstevel@tonic-gate 	char *fdisk_buf;
13267c478bd9Sstevel@tonic-gate 	struct mboot boot_code;		/* Buffer for master boot record */
13277c478bd9Sstevel@tonic-gate 	struct ipart parts[FD_NUMPART];
13287c478bd9Sstevel@tonic-gate 	int32_t numcyl, nhead, nsect;
13297c478bd9Sstevel@tonic-gate 	int32_t unixend;
13307c478bd9Sstevel@tonic-gate 	int32_t blocksize;
13317c478bd9Sstevel@tonic-gate 	int32_t capacity;
13327c478bd9Sstevel@tonic-gate 	int	save_errno;
13337c478bd9Sstevel@tonic-gate 	size_t	bytes_written;
13347c478bd9Sstevel@tonic-gate 	char asciilabel[LEN_DKL_ASCII];
13357c478bd9Sstevel@tonic-gate 	char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0";
13367c478bd9Sstevel@tonic-gate 	int32_t acyl = 2;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	DPRINTF("Writing default fdisk table and vtoc\n");
13397c478bd9Sstevel@tonic-gate 	(void) memset(&v_toc, 0, sizeof (v_toc));
13407c478bd9Sstevel@tonic-gate 	/*
13417c478bd9Sstevel@tonic-gate 	 * Try getting disk geometry.
13427c478bd9Sstevel@tonic-gate 	 */
13437c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGGEOM, &dkg) < 0)
13447c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) {
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 			DPRINTF("DKIOCG_PHYGEOM ioctl failed");
13477c478bd9Sstevel@tonic-gate 			return;
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	tmp_fd = open("/usr/lib/fs/ufs/mboot", O_RDONLY);
13517c478bd9Sstevel@tonic-gate 	if (tmp_fd <= 0) {
13527c478bd9Sstevel@tonic-gate 		return;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	if (read(tmp_fd, &boot_code, sizeof (struct mboot))
13567c478bd9Sstevel@tonic-gate 			!= sizeof (struct mboot)) {
13577c478bd9Sstevel@tonic-gate 		(void) close(tmp_fd);
13587c478bd9Sstevel@tonic-gate 		return;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	blocksize = med_info.sm_blocksize;
13627c478bd9Sstevel@tonic-gate 	fdisk_buf = (char *)malloc(blocksize);
13637c478bd9Sstevel@tonic-gate 	if (fdisk_buf == NULL) {
13647c478bd9Sstevel@tonic-gate 		DPRINTF("malloc for fdisk_buf failed\n");
13657c478bd9Sstevel@tonic-gate 		return;
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	(void) memset(&parts, 0, sizeof (parts));
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
13717c478bd9Sstevel@tonic-gate 		parts[i].systid = UNUSED;
13727c478bd9Sstevel@tonic-gate 		parts[i].numsect = lel(UNUSED);
13737c478bd9Sstevel@tonic-gate 		parts[i].relsect = lel(UNUSED);
13747c478bd9Sstevel@tonic-gate 		parts[i].bootid = 0;
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	numcyl = dkg.dkg_ncyl;
13787c478bd9Sstevel@tonic-gate 	nhead = dkg.dkg_nhead;
13797c478bd9Sstevel@tonic-gate 	nsect = dkg.dkg_nsect;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	parts[0].bootid = ACTIVE;
13827c478bd9Sstevel@tonic-gate 	parts[0].begsect = 1;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	unixend = numcyl;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	parts[0].relsect = lel(nhead * nsect);
13877c478bd9Sstevel@tonic-gate 	parts[0].numsect = lel((long)((numcyl) * nhead * nsect));
13887c478bd9Sstevel@tonic-gate 	parts[0].systid = SUNIXOS2;   /* Solaris */
13897c478bd9Sstevel@tonic-gate 	parts[0].beghead = 0;
13907c478bd9Sstevel@tonic-gate 	parts[0].begcyl = 1;
13917c478bd9Sstevel@tonic-gate 	parts[0].endhead = nhead - 1;
13927c478bd9Sstevel@tonic-gate 	parts[0].endsect = (nsect & 0x3f) |
13937c478bd9Sstevel@tonic-gate 	    (char)((unixend >> 2) & 0x00c0);
13947c478bd9Sstevel@tonic-gate 	parts[0].endcyl = (char)(unixend & 0x00ff);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	(void) memcpy(&(boot_code.parts), parts, sizeof (parts));
13977c478bd9Sstevel@tonic-gate 	(void) memcpy(fdisk_buf, &boot_code, sizeof (boot_code));
13987c478bd9Sstevel@tonic-gate 
1399e3397557Szk 	/* Turn on privileges. */
1400e3397557Szk 	(void) __priv_bracket(PRIV_ON);
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	ret = ioctl(fd, DKIOCSMBOOT, fdisk_buf);
14037c478bd9Sstevel@tonic-gate 
1404e3397557Szk 	/* Turn off privileges. */
1405e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (ret == -1) {
14087c478bd9Sstevel@tonic-gate 		if (errno != ENOTTY) {
14097c478bd9Sstevel@tonic-gate 			PERROR("DKIOCSMBOOT ioctl Failed");
14107c478bd9Sstevel@tonic-gate 			return;
14117c478bd9Sstevel@tonic-gate 		}
14127c478bd9Sstevel@tonic-gate 
1413e3397557Szk 		/* Turn on privileges. */
1414e3397557Szk 		(void) __priv_bracket(PRIV_ON);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		bytes_written = smedia_raw_write(handle, 0, fdisk_buf,
14177c478bd9Sstevel@tonic-gate 			blocksize);
14187c478bd9Sstevel@tonic-gate 
1419e3397557Szk 		/* Turn off privileges. */
1420e3397557Szk 		(void) __priv_bracket(PRIV_OFF);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 		save_errno = errno;
14237c478bd9Sstevel@tonic-gate 		errno = save_errno;
14247c478bd9Sstevel@tonic-gate 		if (bytes_written != blocksize) {
14257c478bd9Sstevel@tonic-gate 			if (errno == ENOTSUP) {
14267c478bd9Sstevel@tonic-gate 
1427e3397557Szk 			    /* Turn on privileges. */
1428e3397557Szk 			    (void) __priv_bracket(PRIV_ON);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 			    ret = write(fd, fdisk_buf, blocksize);
14317c478bd9Sstevel@tonic-gate 
1432e3397557Szk 			    /* Turn off privileges. */
1433e3397557Szk 			    (void) __priv_bracket(PRIV_OFF);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 			    if (ret != blocksize) {
14367c478bd9Sstevel@tonic-gate 					return;
14377c478bd9Sstevel@tonic-gate 			    }
14387c478bd9Sstevel@tonic-gate 			} else {
14397c478bd9Sstevel@tonic-gate 				return;
14407c478bd9Sstevel@tonic-gate 			}
14417c478bd9Sstevel@tonic-gate 		}
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	capacity = (numcyl - 1) * nhead * nsect;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	v_toc.v_nparts = V_NUMPAR;
14467c478bd9Sstevel@tonic-gate 	v_toc.v_sanity = VTOC_SANE;
14477c478bd9Sstevel@tonic-gate 	v_toc.v_version = V_VERSION;
14487c478bd9Sstevel@tonic-gate 	v_toc.v_sectorsz = DEV_BSIZE;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_start = 0;
14517c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_size = capacity;
14527c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_tag  = V_ROOT;
14537c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_flag = 0;	/* Mountable */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_start = 0;
14567c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_size = capacity;
14577c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_tag  = V_BACKUP;
14587c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_flag = V_UNMNT;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	/* Create asciilabel for compatibility with format utility */
14617c478bd9Sstevel@tonic-gate 	(void) snprintf(asciilabel, sizeof (asciilabel),
14627c478bd9Sstevel@tonic-gate 	    "%s cyl %d alt %d hd %d sec %d",
14637c478bd9Sstevel@tonic-gate 	    asciilabel2, numcyl, acyl, nhead, nsect);
14647c478bd9Sstevel@tonic-gate 	(void) memcpy(v_toc.v_asciilabel, asciilabel,
14657c478bd9Sstevel@tonic-gate 		LEN_DKL_ASCII);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	errno = 0;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 
1470e3397557Szk 	/* Turn on privileges. */
1471e3397557Szk 	(void) __priv_bracket(PRIV_ON);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	ret = write_vtoc(fd, &v_toc);
14747c478bd9Sstevel@tonic-gate 
1475e3397557Szk 	/* Turn off privileges. */
1476e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	if (ret < 0) {
14797c478bd9Sstevel@tonic-gate 		PERROR("write VTOC failed");
14807c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate }
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate #else	/* !i386 */
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate #error One of sparc or i386 must be defined!
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate #endif /* i386 */
14897c478bd9Sstevel@tonic-gate #endif /* sparc */
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate  * void overwrite_metadata(int32_t fd, smedia_handle_t handle)
14937c478bd9Sstevel@tonic-gate  *
14947c478bd9Sstevel@tonic-gate  * purpose : quick format does not erase the data on Iomega
14957c478bd9Sstevel@tonic-gate  * zip/jaz media. So, the meta data on the disk should be erased.
14967c478bd9Sstevel@tonic-gate  *
14977c478bd9Sstevel@tonic-gate  * If there is a valid fdisk table,
14987c478bd9Sstevel@tonic-gate  * 	erase first 64K of each partition.
14997c478bd9Sstevel@tonic-gate  * If there is a valid vtoc,
15007c478bd9Sstevel@tonic-gate  * 	erase first 64k of each slice.
15017c478bd9Sstevel@tonic-gate  * Then erase the 0th sector (the home for vtoc and fdisk) of the disk.
15027c478bd9Sstevel@tonic-gate  * Note that teh vtoc on x86 resides in one of the fdisk partition.
15037c478bd9Sstevel@tonic-gate  * So delay the erasing of the solaris partition until the vtoc is read.
15047c478bd9Sstevel@tonic-gate  */
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate void
15077c478bd9Sstevel@tonic-gate overwrite_metadata(int32_t fd, smedia_handle_t handle)
15087c478bd9Sstevel@tonic-gate {
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	struct fdisk_info fdisk;
15117c478bd9Sstevel@tonic-gate 	uint32_t sol_offset = 0;
15127c478bd9Sstevel@tonic-gate 	int i, ret;
15137c478bd9Sstevel@tonic-gate 	struct vtoc t_vtoc;
15147c478bd9Sstevel@tonic-gate #ifdef i386
15157c478bd9Sstevel@tonic-gate 	uint32_t sol_size = 0;
15167c478bd9Sstevel@tonic-gate 	int32_t active = 0;
15177c478bd9Sstevel@tonic-gate #endif /* i386 */
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	/* Get fdisk info. */
15207c478bd9Sstevel@tonic-gate 	if (get_fdisk(handle, fd, 0, &fdisk) >= 0) {
15217c478bd9Sstevel@tonic-gate 		/* Got a valid fdisk */
15227c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 			if (fdisk.part[i].numsect == 0)
15257c478bd9Sstevel@tonic-gate 				continue;
15267c478bd9Sstevel@tonic-gate 			if ((fdisk.part[i].systid == UNUSED) ||
15277c478bd9Sstevel@tonic-gate 				(fdisk.part[i].systid == 0))
15287c478bd9Sstevel@tonic-gate 				continue;
15297c478bd9Sstevel@tonic-gate #ifdef i386
15307c478bd9Sstevel@tonic-gate 			if (fdisk.part[i].systid == SUNIXOS ||
15317c478bd9Sstevel@tonic-gate 			    fdisk.part[i].systid == SUNIXOS2) {
15327c478bd9Sstevel@tonic-gate 				if (!sol_offset) {
15337c478bd9Sstevel@tonic-gate 					sol_offset = fdisk.part[i].relsect;
15347c478bd9Sstevel@tonic-gate 					sol_size = fdisk.part[i].numsect;
15357c478bd9Sstevel@tonic-gate 					if (fdisk.part[i].bootid == ACTIVE)
15367c478bd9Sstevel@tonic-gate 						active = 1;
15377c478bd9Sstevel@tonic-gate 					continue;
15387c478bd9Sstevel@tonic-gate 				} else if ((fdisk.part[i].bootid == ACTIVE) &&
15397c478bd9Sstevel@tonic-gate 				    (!active)) {
15407c478bd9Sstevel@tonic-gate 					erase(handle, sol_offset, sol_size);
15417c478bd9Sstevel@tonic-gate 					sol_offset = fdisk.part[i].relsect;
15427c478bd9Sstevel@tonic-gate 					sol_size = fdisk.part[i].numsect;
15437c478bd9Sstevel@tonic-gate 					active = 1;
15447c478bd9Sstevel@tonic-gate 					continue;
15457c478bd9Sstevel@tonic-gate 				}
15467c478bd9Sstevel@tonic-gate 			}
15477c478bd9Sstevel@tonic-gate #endif /* i386 */
15487c478bd9Sstevel@tonic-gate 			erase(handle, fdisk.part[i].relsect,
15497c478bd9Sstevel@tonic-gate 			    fdisk.part[i].numsect);
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	(void) memset(&t_vtoc, 0, sizeof (t_vtoc));
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	if (sol_offset) {
15567c478bd9Sstevel@tonic-gate 		/* fdisk x86 Solaris partition */
15577c478bd9Sstevel@tonic-gate 		/* VTOC location in solaris partition is DK_LABEL_LOC */
15587c478bd9Sstevel@tonic-gate 
1559e3397557Szk 		/* Turn on privileges. */
1560e3397557Szk 		(void) __priv_bracket(PRIV_ON);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 		ret = read_vtoc(fd, &t_vtoc);
15637c478bd9Sstevel@tonic-gate 
1564e3397557Szk 		/* Turn off privileges. */
15650c887d22Szk 		(void) __priv_bracket(PRIV_OFF);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		if (ret < 0) {
15687c478bd9Sstevel@tonic-gate 			/* No valid vtoc, erase fdisk table. */
15697c478bd9Sstevel@tonic-gate 			erase(handle, 0, 1);
15707c478bd9Sstevel@tonic-gate 			return;
15717c478bd9Sstevel@tonic-gate 		}
15727c478bd9Sstevel@tonic-gate 	} else {
15737c478bd9Sstevel@tonic-gate 		/* Sparc Solaris or x86 solaris with faked fdisk */
15747c478bd9Sstevel@tonic-gate 
1575e3397557Szk 		/* Turn on privileges */
1576e3397557Szk 		(void) __priv_bracket(PRIV_ON);
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 		ret = read_vtoc(fd, &t_vtoc);
15797c478bd9Sstevel@tonic-gate 
1580e3397557Szk 		/* Turn off privileges. */
1581e3397557Szk 		(void) __priv_bracket(PRIV_OFF);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 		if (ret < 0) {
15847c478bd9Sstevel@tonic-gate 			/* No valid vtoc, erase from 0th sector */
15857c478bd9Sstevel@tonic-gate 			erase(handle, 0, med_info.sm_capacity);
15867c478bd9Sstevel@tonic-gate 			return;
15877c478bd9Sstevel@tonic-gate 		}
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++) {
15917c478bd9Sstevel@tonic-gate 		if (t_vtoc.v_part[i].p_size != 0) {
15927c478bd9Sstevel@tonic-gate 			erase(handle, sol_offset + t_vtoc.v_part[i].p_start,
15937c478bd9Sstevel@tonic-gate 				t_vtoc.v_part[i].p_size);
15947c478bd9Sstevel@tonic-gate 			/*
15957c478bd9Sstevel@tonic-gate 			 * To make the udfs not recognise the partition we will
15967c478bd9Sstevel@tonic-gate 			 * erase sectors 256, (p_size-256) and psize.
15977c478bd9Sstevel@tonic-gate 			 */
15987c478bd9Sstevel@tonic-gate 			erase(handle,
15997c478bd9Sstevel@tonic-gate 				sol_offset + t_vtoc.v_part[i].p_start + 256,
16007c478bd9Sstevel@tonic-gate 				1);
16017c478bd9Sstevel@tonic-gate 			erase(handle,
16027c478bd9Sstevel@tonic-gate 				(sol_offset + t_vtoc.v_part[i].p_start +
16037c478bd9Sstevel@tonic-gate 				t_vtoc.v_part[i].p_size - 256),
16047c478bd9Sstevel@tonic-gate 				1);
16057c478bd9Sstevel@tonic-gate 			erase(handle,
16067c478bd9Sstevel@tonic-gate 				(sol_offset + t_vtoc.v_part[i].p_start +
16077c478bd9Sstevel@tonic-gate 				t_vtoc.v_part[i].p_size - 1),
16087c478bd9Sstevel@tonic-gate 				1);
16097c478bd9Sstevel@tonic-gate 		}
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	/*
16137c478bd9Sstevel@tonic-gate 	 * If x86 fdisk solaris partition, erase the vtoc also.
16147c478bd9Sstevel@tonic-gate 	 * for sparc, the erasing 0the sector erases vtoc.
16157c478bd9Sstevel@tonic-gate 	 */
16167c478bd9Sstevel@tonic-gate 	if (sol_offset) {
16177c478bd9Sstevel@tonic-gate 		erase(handle, sol_offset, DK_LABEL_LOC + 2);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	/*
16217c478bd9Sstevel@tonic-gate 	 * erase the 0th sector, it is not guaranteed to be
16227c478bd9Sstevel@tonic-gate 	 * erased in the above sequence.
16237c478bd9Sstevel@tonic-gate 	 */
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	erase(handle, 0, 1);
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate /*
16297c478bd9Sstevel@tonic-gate  * void erase(smedia_handle_t handle, uint32_t offset, uint32_t size)
16307c478bd9Sstevel@tonic-gate  *
16317c478bd9Sstevel@tonic-gate  * Initialize the media with '0' from offset 'offset' upto 'size'
16327c478bd9Sstevel@tonic-gate  * or 128 blocks(64k), whichever is smaller.
16337c478bd9Sstevel@tonic-gate  */
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate static void
16367c478bd9Sstevel@tonic-gate erase(smedia_handle_t handle, uint32_t offset, uint32_t size)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	char *buf;
16397c478bd9Sstevel@tonic-gate 	int32_t nblocks = size;
16407c478bd9Sstevel@tonic-gate 	int32_t ret;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	nblocks = (nblocks < 128) ? nblocks : 128;
16447c478bd9Sstevel@tonic-gate 	buf = (char *)malloc(nblocks * med_info.sm_blocksize);
16457c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
16467c478bd9Sstevel@tonic-gate 		PERROR("malloc failed");
16477c478bd9Sstevel@tonic-gate 		return;
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 	(void) memset(buf, 0, nblocks * med_info.sm_blocksize);
16507c478bd9Sstevel@tonic-gate 
1651e3397557Szk 	/* Turn on privileges. */
1652e3397557Szk 	(void) __priv_bracket(PRIV_ON);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	ret = smedia_raw_write(handle, offset, buf,
16557c478bd9Sstevel@tonic-gate 	    nblocks * med_info.sm_blocksize);
16567c478bd9Sstevel@tonic-gate 
1657e3397557Szk 	/* Turn off privileges. */
1658e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	if (ret != (nblocks * med_info.sm_blocksize))
16617c478bd9Sstevel@tonic-gate 		PERROR("error in writing\n");
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	free(buf);
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate }
1666