xref: /illumos-gate/usr/src/cmd/rmformat/rmf_slice.c (revision 9292c78d)
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 /*
22622a268dSPavel Potoplyak  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * rmf_slice.c :
28e74ff653SToomas Soome  *	This file contains the functions for parsing a slice file
29e74ff653SToomas Soome  *	for rmformat.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <memory.h>
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
47bbed02dfSzk #include <priv_utils.h>
487c478bd9Sstevel@tonic-gate #include "rmformat.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate extern void my_perror(char *err_string);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int32_t	last_token_type = 0;
537c478bd9Sstevel@tonic-gate #define	spc()	  (last_token_type)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * This global is used to store the current line # in the
587c478bd9Sstevel@tonic-gate  * data file. It must be global because the I/O routines
597c478bd9Sstevel@tonic-gate  * are allowed to side effect it to keep track of backslashed
607c478bd9Sstevel@tonic-gate  * newlines.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static int32_t	data_lineno;		/* current line # in data file */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	CHG_MODE_UNDEFINED  (-1)	/* undefined value */
667c478bd9Sstevel@tonic-gate #define	CHG_MODE_SET	0		/* set bits by or'ing */
677c478bd9Sstevel@tonic-gate #define	CHG_MODE_CLR	1		/* clr bits by and'ing */
687c478bd9Sstevel@tonic-gate #define	CHG_MODE_ABS	2		/* set absolute value */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	TOKEN_SIZE	36		/* max length of a token */
727c478bd9Sstevel@tonic-gate typedef char TOKEN[TOKEN_SIZE+1];	/* token type */
737c478bd9Sstevel@tonic-gate #define	DATA_INPUT	0		/* 2 modes of input */
747c478bd9Sstevel@tonic-gate #define	CMD_INPUT	1
757c478bd9Sstevel@tonic-gate #define	WILD_STRING	"$"		/* wildcard character */
767c478bd9Sstevel@tonic-gate #define	COMMENT_CHAR	'#'		/* comment character */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * List of strings with arbitrary matching values
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate typedef struct slist {
827c478bd9Sstevel@tonic-gate 	char	*str;
837c478bd9Sstevel@tonic-gate 	char	*help;
847c478bd9Sstevel@tonic-gate 	int32_t	value;
857c478bd9Sstevel@tonic-gate } slist_t;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static slist_t ptag_choices[] = {
887c478bd9Sstevel@tonic-gate 	{ "unassigned", "",	V_UNASSIGNED	},
897c478bd9Sstevel@tonic-gate 	{ "boot",	"",	V_BOOT		},
907c478bd9Sstevel@tonic-gate 	{ "root",	"",	V_ROOT		},
917c478bd9Sstevel@tonic-gate 	{ "swap",	"",	V_SWAP		},
927c478bd9Sstevel@tonic-gate 	{ "usr",	"",	V_USR		},
937c478bd9Sstevel@tonic-gate 	{ "backup",	"",	V_BACKUP	},
947c478bd9Sstevel@tonic-gate 	{ "stand",	"",	V_STAND		},
957c478bd9Sstevel@tonic-gate 	{ "var",	"",	V_VAR		},
967c478bd9Sstevel@tonic-gate 	{ "home",	"",	V_HOME		},
977c478bd9Sstevel@tonic-gate 	{ "alternates", "",	V_ALTSCTR	},
987c478bd9Sstevel@tonic-gate 	{ NULL }
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Choices for the p_flag vtoc field
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static slist_t pflag_choices[] = {
1067c478bd9Sstevel@tonic-gate 	{ "wm", "read-write, mountable",	0	},
1077c478bd9Sstevel@tonic-gate 	{ "wu", "read-write, unmountable",	V_UNMNT	},
1087c478bd9Sstevel@tonic-gate 	{ "rm", "read-only, mountable",		V_RONLY	},
1097c478bd9Sstevel@tonic-gate 	{ "ru", "read-only, unmountable",	V_RONLY|V_UNMNT },
1107c478bd9Sstevel@tonic-gate 	{ NULL }
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * The definitions are the token types that the data file parser recognizes.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate #define	SUP_EOF			-1		/* eof token */
1177c478bd9Sstevel@tonic-gate #define	SUP_STRING		0		/* string token */
1187c478bd9Sstevel@tonic-gate #define	SUP_EQL			1		/* equals token */
1197c478bd9Sstevel@tonic-gate #define	SUP_COMMA		2		/* comma token */
1207c478bd9Sstevel@tonic-gate #define	SUP_COLON		3		/* colon token */
1217c478bd9Sstevel@tonic-gate #define	SUP_EOL			4		/* newline token */
1227c478bd9Sstevel@tonic-gate #define	SUP_OR			5		/* vertical bar */
1237c478bd9Sstevel@tonic-gate #define	SUP_AND			6		/* ampersand */
1247c478bd9Sstevel@tonic-gate #define	SUP_TILDE		7		/* tilde */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  *	Prototypes for ANSI C compilers
1297c478bd9Sstevel@tonic-gate  */
130342440ecSPrasad Singamsetty static int32_t	sup_prxfile(char *file_name, struct extvtoc *vt);
131342440ecSPrasad Singamsetty static int32_t	sup_setpart(struct extvtoc *vt);
1327c478bd9Sstevel@tonic-gate static void	sup_pushchar(int32_t c);
1337c478bd9Sstevel@tonic-gate static void	clean_token(char *cleantoken, char *token);
1347c478bd9Sstevel@tonic-gate static void clean_token(char *cleantoken, char *token);
1357c478bd9Sstevel@tonic-gate static int32_t sup_inputchar();
1367c478bd9Sstevel@tonic-gate static int32_t sup_gettoken(char *buf);
1377c478bd9Sstevel@tonic-gate static int32_t sup_get_token(char *buf);
1387c478bd9Sstevel@tonic-gate static int32_t find_value(slist_t *slist, char *str, int32_t *value);
139342440ecSPrasad Singamsetty static int32_t check_vtoc_sanity(smedia_handle_t, int32_t fd,
140342440ecSPrasad Singamsetty 		struct extvtoc *vt);
141342440ecSPrasad Singamsetty static uint64_t str2sector(char *str);
1427c478bd9Sstevel@tonic-gate static int32_t strcnt(char *s1, char *s2);
1437c478bd9Sstevel@tonic-gate static int32_t get_fdisk(smedia_handle_t, int32_t fd, int32_t offset,
1447c478bd9Sstevel@tonic-gate 		struct fdisk_info *fdisk);
145342440ecSPrasad Singamsetty static void erase(smedia_handle_t handle, diskaddr_t offset, diskaddr_t size);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate extern char *myname;
148342440ecSPrasad Singamsetty extern uint64_t my_atoll(char *ptr);
1497c478bd9Sstevel@tonic-gate extern smmedium_prop_t med_info;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static FILE *data_file;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static int32_t
154342440ecSPrasad Singamsetty sup_prxfile(char *file_name, struct extvtoc *vt)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	int32_t	status, ret_val;
1577c478bd9Sstevel@tonic-gate 	TOKEN	token;
1587c478bd9Sstevel@tonic-gate 	TOKEN	cleaned;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * Open the data file.  Return 0 if unable to do so.
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	data_file = fopen(file_name, "r");
1647c478bd9Sstevel@tonic-gate 	if (data_file == NULL) {
1657c478bd9Sstevel@tonic-gate 		PERROR("Open failed");
1667c478bd9Sstevel@tonic-gate 		return (-1);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	/*
1697c478bd9Sstevel@tonic-gate 	 * Step through the data file a meta-line at a time.  There are
1707c478bd9Sstevel@tonic-gate 	 * typically several backslashed newlines in each meta-line,
1717c478bd9Sstevel@tonic-gate 	 * so data_lineno will be getting side effected along the way.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	data_lineno = 1;
1747c478bd9Sstevel@tonic-gate 	for (;;) {
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		/*
1777c478bd9Sstevel@tonic-gate 		 * Get the keyword.
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
1807c478bd9Sstevel@tonic-gate 		/*
1817c478bd9Sstevel@tonic-gate 		 * If we hit the end of the data file, we're done.
1827c478bd9Sstevel@tonic-gate 		 */
1837c478bd9Sstevel@tonic-gate 		if (status == SUP_EOF)
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * If the line starts with some key character, it's an error.
1877c478bd9Sstevel@tonic-gate 		 */
1887c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
1897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
190622a268dSPavel Potoplyak 			    gettext("Expecting keyword, found '%s'"),
1917c478bd9Sstevel@tonic-gate 			    token);
1927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
193622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
1947c478bd9Sstevel@tonic-gate 			continue;
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 		/*
1977c478bd9Sstevel@tonic-gate 		 * Clean up the token and see which keyword it is.  Call
1987c478bd9Sstevel@tonic-gate 		 * the appropriate routine to process the rest of the line.
1997c478bd9Sstevel@tonic-gate 		 */
2007c478bd9Sstevel@tonic-gate 		clean_token(cleaned, token);
2017c478bd9Sstevel@tonic-gate 		if (strcmp(cleaned, "slices") == 0) {
2027c478bd9Sstevel@tonic-gate 			ret_val = sup_setpart(vt);
2037c478bd9Sstevel@tonic-gate 			(void) fclose(data_file);
2047c478bd9Sstevel@tonic-gate 			return (ret_val);
2057c478bd9Sstevel@tonic-gate 		} else {
2067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("Unknown keyword '%s'"),
2077c478bd9Sstevel@tonic-gate 			    cleaned);
2087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
209622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
2107c478bd9Sstevel@tonic-gate 			(void) fclose(data_file);
2117c478bd9Sstevel@tonic-gate 			return (-1);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Close the data file.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	(void) fclose(data_file);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
220622a268dSPavel Potoplyak 	    gettext("Unexpected end of file (line no %d)\n"), data_lineno);
2217c478bd9Sstevel@tonic-gate 	return (-1);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static int32_t
2257c478bd9Sstevel@tonic-gate sup_gettoken(char *buf)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * Skip end of lines and blank lines.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	while ((last_token_type = sup_get_token(buf)) == SUP_EOL)
2317c478bd9Sstevel@tonic-gate 			;
2327c478bd9Sstevel@tonic-gate 	return (last_token_type);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static int32_t
2367c478bd9Sstevel@tonic-gate sup_get_token(char *buf)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	char	*ptr = buf;
2397c478bd9Sstevel@tonic-gate 	int32_t	c, quoted = 0;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Was an end of file detected last try?
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (feof(data_file)) {
2467c478bd9Sstevel@tonic-gate 		return (SUP_EOF);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Zero out the returned token buffer
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	bzero(buf, TOKEN_SIZE + 1);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Strip off leading white-space.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	while (isspace(c = sup_inputchar()))
2597c478bd9Sstevel@tonic-gate 		;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * Only white spaces and then end of file?
2637c478bd9Sstevel@tonic-gate 	 */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (feof(data_file)) {
2667c478bd9Sstevel@tonic-gate 		return (SUP_EOF);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * Read in characters until we hit unquoted white-space.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	for (; !isspace(c) || quoted; c = sup_inputchar()) {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * If we hit eof, check if we have anything in buffer.
2767c478bd9Sstevel@tonic-gate 		 * if we have, return STRING, next time we will return EOF
2777c478bd9Sstevel@tonic-gate 		 * else, return EOF here...should not happen.
2787c478bd9Sstevel@tonic-gate 		 */
2797c478bd9Sstevel@tonic-gate 		if (feof(data_file)) {
2807c478bd9Sstevel@tonic-gate 			if (ptr - buf > 0) {
2817c478bd9Sstevel@tonic-gate 				return (SUP_STRING);
2827c478bd9Sstevel@tonic-gate 			} else {
2837c478bd9Sstevel@tonic-gate 				return (SUP_EOF);
2847c478bd9Sstevel@tonic-gate 			}
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/*
2887c478bd9Sstevel@tonic-gate 		 * If we hit a double quote, change the state of quoting.
2897c478bd9Sstevel@tonic-gate 		 */
2907c478bd9Sstevel@tonic-gate 		if (c == '"') {
2917c478bd9Sstevel@tonic-gate 			quoted = !quoted;
2927c478bd9Sstevel@tonic-gate 			continue;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * If we hit a newline, that delimits a token.
2967c478bd9Sstevel@tonic-gate 		 */
2977c478bd9Sstevel@tonic-gate 		if (c == '\n')
2987c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 		/*
3007c478bd9Sstevel@tonic-gate 		 * If we hit any nonquoted special delimiters, that delimits
3017c478bd9Sstevel@tonic-gate 		 * a token.
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		if (!quoted && (c == '=' || c == ',' || c == ':' ||
304622a268dSPavel Potoplyak 		    c == '#' || c == '|' || c == '&' || c == '~'))
3057c478bd9Sstevel@tonic-gate 				break;
3067c478bd9Sstevel@tonic-gate 		/*
3077c478bd9Sstevel@tonic-gate 		 * Store the character if there's room left.
3087c478bd9Sstevel@tonic-gate 		 */
3097c478bd9Sstevel@tonic-gate 		if (ptr - buf < TOKEN_SIZE)
3107c478bd9Sstevel@tonic-gate 			*ptr++ = (char)c;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * If we stored characters in the buffer, then we inputted a string.
3147c478bd9Sstevel@tonic-gate 	 * Push the delimiter back into the pipe and return the string.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if (ptr - buf > 0) {
3177c478bd9Sstevel@tonic-gate 		sup_pushchar(c);
3187c478bd9Sstevel@tonic-gate 		return (SUP_STRING);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * We didn't input a string, so we must have inputted a known delimiter.
3227c478bd9Sstevel@tonic-gate 	 * store the delimiter in the buffer, so it will get returned.
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	buf[0] = c;
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 * Switch on the delimiter.  Return the appropriate value for each one.
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 	switch (c) {
3297c478bd9Sstevel@tonic-gate 	case '=':
3307c478bd9Sstevel@tonic-gate 		return (SUP_EQL);
3317c478bd9Sstevel@tonic-gate 	case ':':
3327c478bd9Sstevel@tonic-gate 		return (SUP_COLON);
3337c478bd9Sstevel@tonic-gate 	case ',':
3347c478bd9Sstevel@tonic-gate 		return (SUP_COMMA);
3357c478bd9Sstevel@tonic-gate 	case '\n':
3367c478bd9Sstevel@tonic-gate 		return (SUP_EOL);
3377c478bd9Sstevel@tonic-gate 	case '|':
3387c478bd9Sstevel@tonic-gate 		return (SUP_OR);
3397c478bd9Sstevel@tonic-gate 	case '&':
3407c478bd9Sstevel@tonic-gate 		return (SUP_AND);
3417c478bd9Sstevel@tonic-gate 	case '~':
3427c478bd9Sstevel@tonic-gate 		return (SUP_TILDE);
3437c478bd9Sstevel@tonic-gate 	case '#':
3447c478bd9Sstevel@tonic-gate 		/*
3457c478bd9Sstevel@tonic-gate 		 * For comments, we flush out the rest of the line and return
3467c478bd9Sstevel@tonic-gate 		 * an eol.
3477c478bd9Sstevel@tonic-gate 		 */
3487c478bd9Sstevel@tonic-gate 		while ((c = sup_inputchar()) != '\n' && !feof(data_file))
3497c478bd9Sstevel@tonic-gate 			;
3507c478bd9Sstevel@tonic-gate 		if (feof(data_file))
3517c478bd9Sstevel@tonic-gate 			return (SUP_EOF);
3527c478bd9Sstevel@tonic-gate 		else
3537c478bd9Sstevel@tonic-gate 			return (SUP_EOL);
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Shouldn't ever get here.
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 	default:
3587c478bd9Sstevel@tonic-gate 		return (SUP_STRING);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate static int32_t
3627c478bd9Sstevel@tonic-gate sup_inputchar()
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	int32_t	c;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Input the character.
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	c = getc(data_file);
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * If it's not a backslash, return it.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * It was a backslash.  Get the next character.
3767c478bd9Sstevel@tonic-gate 	 */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (c == '\\')
3797c478bd9Sstevel@tonic-gate 		c = getc(data_file);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * If it was a newline, update the line counter and get the next
3837c478bd9Sstevel@tonic-gate 	 * character.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	if (c == '\n') {
3867c478bd9Sstevel@tonic-gate 		data_lineno++;
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * Return the character.
3907c478bd9Sstevel@tonic-gate 	 */
3917c478bd9Sstevel@tonic-gate 	return (c);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static void
3957c478bd9Sstevel@tonic-gate sup_pushchar(int32_t c)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	(void) ungetc(c, data_file);
3997c478bd9Sstevel@tonic-gate 	if (c == '\n')
4007c478bd9Sstevel@tonic-gate 		data_lineno--;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static void
4047c478bd9Sstevel@tonic-gate clean_token(char *cleantoken, char *token)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	char	*ptr;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * Strip off leading white-space.
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	for (ptr = token; isspace(*ptr) && (ptr <=
412622a268dSPavel Potoplyak 	    (token + strlen(token) - 1)); ptr++)
413622a268dSPavel Potoplyak 		;
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;
423622a268dSPavel Potoplyak 	    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
429342440ecSPrasad Singamsetty sup_setpart(struct extvtoc *vt)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	TOKEN	token, cleaned, ident;
432342440ecSPrasad Singamsetty 	int32_t	i, index, status;
433342440ecSPrasad Singamsetty 	uint64_t	val1, val2;
4347c478bd9Sstevel@tonic-gate 	ushort_t	vtoc_tag = 0xFFFF;
4357c478bd9Sstevel@tonic-gate 	ushort_t	vtoc_flag = 0xFFFF;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Pull in some grammar.
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		if (status != SUP_COLON) {
4447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
445622a268dSPavel Potoplyak 			    gettext("Expecting ':', found '%s'"), token);
4467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
447622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
4487c478bd9Sstevel@tonic-gate 			return (-1);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	for (;;) {
4527c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
4537c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
4547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
455622a268dSPavel Potoplyak 			    gettext("Expecting string, found '%s'"), token);
4567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
457622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
4587c478bd9Sstevel@tonic-gate 			return (-1);
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 		clean_token(ident, token);
4617c478bd9Sstevel@tonic-gate 		/*
4627c478bd9Sstevel@tonic-gate 		 * Here's the index of the partition we're dealing with
4637c478bd9Sstevel@tonic-gate 		 */
4647c478bd9Sstevel@tonic-gate 		index = (int32_t)my_atoll(ident);
465622a268dSPavel Potoplyak 		if ((index < 0) || (index >= NDKMAP)) {
4667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
467622a268dSPavel Potoplyak 			    gettext("Unknown partition %d"), index);
4687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
469622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
4707c478bd9Sstevel@tonic-gate 			return (-1);
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 		/*
4737c478bd9Sstevel@tonic-gate 		 * Check for floppy and PCMCIA_MEM cards.
4747c478bd9Sstevel@tonic-gate 		 * for floppy, the partition no. can be 0 1 2.
4757c478bd9Sstevel@tonic-gate 		 * for PCMCIA, the partition no. can be 2
4767c478bd9Sstevel@tonic-gate 		 */
4777c478bd9Sstevel@tonic-gate 		if (med_info.sm_media_type == SM_FLOPPY) {
4787c478bd9Sstevel@tonic-gate 			if ((index < 0) || (index > 2)) {
4797c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4807c478bd9Sstevel@tonic-gate 			"Floppy can have partitions 0 1 and 2\n"));
4817c478bd9Sstevel@tonic-gate 				return (-1);
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		if (med_info.sm_media_type == SM_PCMCIA_MEM) {
4857c478bd9Sstevel@tonic-gate 			if (index != 2) {
4867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4877c478bd9Sstevel@tonic-gate 			"PCMCIA Memory cards can have partition 2 only.\n"));
4887c478bd9Sstevel@tonic-gate 				return (-1);
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		DPRINTF1("\n Partition %d: ", index);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
4957c478bd9Sstevel@tonic-gate 		if (status != SUP_EQL) {
4967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
497622a268dSPavel Potoplyak 			    gettext("Expecting '=', found '%s'"), token);
4987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
499622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
5007c478bd9Sstevel@tonic-gate 			return (-1);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5067c478bd9Sstevel@tonic-gate 		/*
5077c478bd9Sstevel@tonic-gate 		 * If we hit a key character, it's an error.
5087c478bd9Sstevel@tonic-gate 		 */
5097c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
5107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
511622a268dSPavel Potoplyak 			    gettext("Expecting value, found '%s'"), token);
5127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
513622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
5147c478bd9Sstevel@tonic-gate 			return (-1);
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		clean_token(cleaned, token);
5177c478bd9Sstevel@tonic-gate 		/*
5187c478bd9Sstevel@tonic-gate 		 * <tag> may be one of: boot, root, swap, etc.
5197c478bd9Sstevel@tonic-gate 		 * <flag> consists of two characters:
5207c478bd9Sstevel@tonic-gate 		 *	W (writable) or R (read-only)
5217c478bd9Sstevel@tonic-gate 		 *	M (mountable) or U (unmountable)
5227c478bd9Sstevel@tonic-gate 		 *
5237c478bd9Sstevel@tonic-gate 		 * Start with the defaults assigned above:
5247c478bd9Sstevel@tonic-gate 		 */
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		/*
5277c478bd9Sstevel@tonic-gate 		 * All other attributes have a pair of numeric values.
5287c478bd9Sstevel@tonic-gate 		 * Convert the first value to a number.  This value
5297c478bd9Sstevel@tonic-gate 		 * is the starting cylinder number of the partition.
5307c478bd9Sstevel@tonic-gate 		 */
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		/* Check for valid partition, e.g. > 8 or 16 */
5337c478bd9Sstevel@tonic-gate 		val1 = str2sector(cleaned);
5347c478bd9Sstevel@tonic-gate 		if (val1 == -1) {
5357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
536622a268dSPavel Potoplyak 			    gettext("Invalid partition beggining %s \n"),
5377c478bd9Sstevel@tonic-gate 			    cleaned);
5387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
539622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		DPRINTF1(" begins %s", cleaned);
5437c478bd9Sstevel@tonic-gate 		/*
5447c478bd9Sstevel@tonic-gate 		 * Pull in some grammar.
5457c478bd9Sstevel@tonic-gate 		 */
5467c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5477c478bd9Sstevel@tonic-gate 		if (status != SUP_COMMA) {
5487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
549622a268dSPavel Potoplyak 			    gettext("Expecting ', ', found '%s'"), token);
5507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
551622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
5527c478bd9Sstevel@tonic-gate 			return (-1);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * Pull in the second value.
5567c478bd9Sstevel@tonic-gate 		 */
5577c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5587c478bd9Sstevel@tonic-gate 		if (status != SUP_STRING) {
5597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
560622a268dSPavel Potoplyak 			    gettext("Expecting value, found '%s'"), token);
5617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
562622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
5637c478bd9Sstevel@tonic-gate 			return (-1);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 		clean_token(cleaned, token);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		val2 = str2sector(cleaned);
5687c478bd9Sstevel@tonic-gate 		if (val2 == -1) {
5697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
570622a268dSPavel Potoplyak 			    gettext("Invalid partition size %s \n"),
5717c478bd9Sstevel@tonic-gate 			    cleaned);
5727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
573622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		DPRINTF1(" ends %s ", cleaned);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 * Pull in some grammar.
5797c478bd9Sstevel@tonic-gate 		 */
5807c478bd9Sstevel@tonic-gate 		status = sup_gettoken(token);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		if (status == SUP_COMMA) {
5837c478bd9Sstevel@tonic-gate 			/* tags and flags  */
5847c478bd9Sstevel@tonic-gate 			status = sup_gettoken(token);
5857c478bd9Sstevel@tonic-gate 			if (status != SUP_STRING) {
5867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
587622a268dSPavel Potoplyak 				    gettext("Expecting value, found '%s'"),
5887c478bd9Sstevel@tonic-gate 				    token);
5897c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
590622a268dSPavel Potoplyak 				    gettext("Line no %d\n"), data_lineno);
5917c478bd9Sstevel@tonic-gate 				return (-1);
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 			clean_token(cleaned, token);
5947c478bd9Sstevel@tonic-gate 			if (find_value(pflag_choices, cleaned, &i) == 1) {
5957c478bd9Sstevel@tonic-gate 				/*
5967c478bd9Sstevel@tonic-gate 				 * Found valid tag. Use it and advance parser
5977c478bd9Sstevel@tonic-gate 				 */
5987c478bd9Sstevel@tonic-gate 				DPRINTF1(" flag = %s", cleaned);
5997c478bd9Sstevel@tonic-gate 				vtoc_flag = (ushort_t)i;
6007c478bd9Sstevel@tonic-gate 				status = sup_gettoken(token);
6017c478bd9Sstevel@tonic-gate 			} else if (find_value(ptag_choices, cleaned, &i) == 1) {
6027c478bd9Sstevel@tonic-gate 				DPRINTF1(" tag = %s", cleaned);
6037c478bd9Sstevel@tonic-gate 				vtoc_tag = (ushort_t)i;
6047c478bd9Sstevel@tonic-gate 				status = sup_gettoken(token);
6057c478bd9Sstevel@tonic-gate 				if (status == SUP_COMMA) {
6067c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
607622a268dSPavel Potoplyak 					    gettext("Expecting : got %s\n"),
6087c478bd9Sstevel@tonic-gate 					    token);
6097c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
610622a268dSPavel Potoplyak 					    gettext("Line no %d\n"),
6117c478bd9Sstevel@tonic-gate 					    data_lineno);
6127c478bd9Sstevel@tonic-gate 					return (-1);
6137c478bd9Sstevel@tonic-gate 				}
6147c478bd9Sstevel@tonic-gate 			} else {
6157c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
616622a268dSPavel Potoplyak 				    gettext("Invalid flag or tag\n"));
6177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
618622a268dSPavel Potoplyak 				    gettext("Line no %d\n"), data_lineno);
6197c478bd9Sstevel@tonic-gate 				return (-1);
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			if (status == SUP_COMMA) {
6247c478bd9Sstevel@tonic-gate 					/* Can be tag only */
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 					status = sup_gettoken(token);
6277c478bd9Sstevel@tonic-gate 					if (status != SUP_STRING) {
6287c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
629622a268dSPavel Potoplyak 						    gettext("Expecting value"
630622a268dSPavel Potoplyak 						    ", found '%s'"),
631622a268dSPavel Potoplyak 						    token);
6327c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
633622a268dSPavel Potoplyak 						    gettext("Line no %d\n"),
6347c478bd9Sstevel@tonic-gate 						    data_lineno);
6357c478bd9Sstevel@tonic-gate 						return (-1);
6367c478bd9Sstevel@tonic-gate 					}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 					clean_token(cleaned, token);
6397c478bd9Sstevel@tonic-gate 					if (find_value(ptag_choices,
6407c478bd9Sstevel@tonic-gate 					    cleaned, &i) == 1) {
6417c478bd9Sstevel@tonic-gate 						DPRINTF1(" tag = %s", cleaned);
6427c478bd9Sstevel@tonic-gate 						vtoc_tag = (ushort_t)i;
6437c478bd9Sstevel@tonic-gate 					}
6447c478bd9Sstevel@tonic-gate 			status = sup_gettoken(token);
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		/*
6497c478bd9Sstevel@tonic-gate 		 * Fill in the appropriate map entry with the values.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		vt->v_part[index].p_start = val1;
6527c478bd9Sstevel@tonic-gate 		vt->v_part[index].p_size = val2;
6537c478bd9Sstevel@tonic-gate 		if (vtoc_tag != 0xFFFF) {
6547c478bd9Sstevel@tonic-gate 			vt->v_part[index].p_tag = vtoc_tag;
6557c478bd9Sstevel@tonic-gate 			vtoc_tag = 0xFFFF;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 		if (vtoc_flag != 0xFFFF) {
6587c478bd9Sstevel@tonic-gate 			vt->v_part[index].p_flag = vtoc_flag;
6597c478bd9Sstevel@tonic-gate 			vtoc_flag = 0xFFFF;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		if (status == SUP_EOF) {
6627c478bd9Sstevel@tonic-gate 			DPRINTF("\nEnd of file\n");
6637c478bd9Sstevel@tonic-gate 			break;
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		if (status != SUP_COLON) {
6667c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
667622a268dSPavel Potoplyak 			    gettext("Expecting ':', found '%s'"), token);
6687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
669622a268dSPavel Potoplyak 			    gettext("Line no %d\n"), data_lineno);
6707c478bd9Sstevel@tonic-gate 			return (-1);
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	return (0);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate static int32_t
6787c478bd9Sstevel@tonic-gate find_value(slist_t *slist, char *match_str, int32_t *match_value)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	int32_t	i;
6817c478bd9Sstevel@tonic-gate 	int32_t	nmatches;
6827c478bd9Sstevel@tonic-gate 	int32_t	length;
6837c478bd9Sstevel@tonic-gate 	int32_t	match_length;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	nmatches = 0;
6867c478bd9Sstevel@tonic-gate 	length = 0;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	match_length = strlen(match_str);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	for (; slist->str != NULL; slist++) {
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * See how many characters of the token match
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		i = strcnt(match_str, slist->str);
6957c478bd9Sstevel@tonic-gate 		/*
6967c478bd9Sstevel@tonic-gate 		 * If it's not the whole token, then it's not a match.
6977c478bd9Sstevel@tonic-gate 		 */
6987c478bd9Sstevel@tonic-gate 		if (i  < match_length) {
6997c478bd9Sstevel@tonic-gate 			continue;
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 		/*
7027c478bd9Sstevel@tonic-gate 		 * If it ties with another input, remember that.
7037c478bd9Sstevel@tonic-gate 		 */
7047c478bd9Sstevel@tonic-gate 		if (i == length)
7057c478bd9Sstevel@tonic-gate 			nmatches++;
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * If it matches the most so far, record that.
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		if (i > length) {
7107c478bd9Sstevel@tonic-gate 			*match_value = slist->value;
7117c478bd9Sstevel@tonic-gate 			nmatches = 1;
7127c478bd9Sstevel@tonic-gate 			length = i;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	return (nmatches);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static int32_t
7207c478bd9Sstevel@tonic-gate strcnt(char	*s1, char *s2)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	int32_t	i = 0;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	while ((*s1 != '\0') && (*s1++ == *s2++))
7257c478bd9Sstevel@tonic-gate 		i++;
7267c478bd9Sstevel@tonic-gate 	return (i);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
729342440ecSPrasad Singamsetty static uint64_t
7307c478bd9Sstevel@tonic-gate str2sector(char *str)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	int32_t mul_factor = 1;
7337c478bd9Sstevel@tonic-gate 	char *s1, *s2, *base;
734342440ecSPrasad Singamsetty 	uint64_t num_sectors;
735342440ecSPrasad Singamsetty 	uint64_t size;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	base = s2 = (char *)malloc(strlen(str) + 1);
7387c478bd9Sstevel@tonic-gate 	if (s2 == NULL) {
7397c478bd9Sstevel@tonic-gate 		PERROR("Malloc failed");
7407c478bd9Sstevel@tonic-gate 		return (-1);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 	*s2 = '\0';
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	s1 = str;
7477c478bd9Sstevel@tonic-gate 	while (*s1) {
7487c478bd9Sstevel@tonic-gate 		if ((*s1 != 'x') && ((*s1 < 'A') || (*s1 > 'F')) &&
749622a268dSPavel Potoplyak 		    ((*s1 < 'a') || (*s1 > 'f')) && ((*s1 < '0') ||
750622a268dSPavel Potoplyak 		    (*s1 > '9'))) {
7517c478bd9Sstevel@tonic-gate 			if (*s1 == 'G') {
7527c478bd9Sstevel@tonic-gate 					mul_factor = 1024*1024*1024;
7537c478bd9Sstevel@tonic-gate 					s1++;
7547c478bd9Sstevel@tonic-gate 			} else if (*s1 == 'M') {
7557c478bd9Sstevel@tonic-gate 					mul_factor = 1024*1024;
7567c478bd9Sstevel@tonic-gate 					s1++;
7577c478bd9Sstevel@tonic-gate 			} else if (*s1 == 'K') {
7587c478bd9Sstevel@tonic-gate 					mul_factor = 1024;
7597c478bd9Sstevel@tonic-gate 					s1++;
7607c478bd9Sstevel@tonic-gate 			}
761*9292c78dSToomas Soome 			if ((*s1 != 'B') || (*(++s1) != '\0')) {
7627c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
763622a268dSPavel Potoplyak 				    gettext("Extra chars at the end\n"));
7647c478bd9Sstevel@tonic-gate 				free(base);
7657c478bd9Sstevel@tonic-gate 				return (-1);
7667c478bd9Sstevel@tonic-gate 			}
7677c478bd9Sstevel@tonic-gate 			break;
7687c478bd9Sstevel@tonic-gate 		} else {
7697c478bd9Sstevel@tonic-gate 			*s2++ = *s1++;
7707c478bd9Sstevel@tonic-gate 			*s2 = '\0';
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 	}
773*9292c78dSToomas Soome 	*s2 = '\0';
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	size = my_atoll(base);
7767c478bd9Sstevel@tonic-gate 	if ((!mul_factor) || (size == -1)) {
7777c478bd9Sstevel@tonic-gate 		free(base);
7787c478bd9Sstevel@tonic-gate 		return (-1);
7797c478bd9Sstevel@tonic-gate 	}
780342440ecSPrasad Singamsetty 	num_sectors = size * (uint64_t)mul_factor /512;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	free(base);
7837c478bd9Sstevel@tonic-gate 	return (num_sectors);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate int32_t
7887c478bd9Sstevel@tonic-gate valid_slice_file(smedia_handle_t handle, int32_t fd, char *file_name,
789e74ff653SToomas Soome     struct extvtoc *vt)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	struct stat status;
7927c478bd9Sstevel@tonic-gate 	int32_t ret_val;
7937c478bd9Sstevel@tonic-gate 	if (stat(file_name, &status)) {
7947c478bd9Sstevel@tonic-gate 		PERROR(file_name);
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	(void) memset(vt, 0, sizeof (*vt));
7987c478bd9Sstevel@tonic-gate 	/* Set default tag and flag */
7997c478bd9Sstevel@tonic-gate #ifdef sparc
8007c478bd9Sstevel@tonic-gate 	vt->v_part[0].p_tag = V_ROOT;
8017c478bd9Sstevel@tonic-gate 	vt->v_part[1].p_tag = V_SWAP;
8027c478bd9Sstevel@tonic-gate 	vt->v_part[2].p_tag = V_BACKUP;
8037c478bd9Sstevel@tonic-gate 	vt->v_part[6].p_tag = V_USR;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	vt->v_part[1].p_flag = V_UNMNT; /* Unmountable */
8067c478bd9Sstevel@tonic-gate 	vt->v_part[2].p_flag = V_UNMNT; /* Unmountable */
8077c478bd9Sstevel@tonic-gate #endif
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	ret_val = sup_prxfile(file_name, vt);
8107c478bd9Sstevel@tonic-gate 	if (ret_val < 0)
8117c478bd9Sstevel@tonic-gate 		return (-1);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate #ifdef DEBUG
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	int32_t i;
8167c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
8177c478bd9Sstevel@tonic-gate 		DPRINTF1("\npart %d\n", i);
818342440ecSPrasad Singamsetty 		DPRINTF1("\t start %llu",  vt->v_part[i].p_start);
819342440ecSPrasad Singamsetty 		DPRINTF1("\t size %llu ", vt->v_part[i].p_size);
8207c478bd9Sstevel@tonic-gate 		DPRINTF1("\t tag %d", vt->v_part[i].p_tag);
8217c478bd9Sstevel@tonic-gate 		DPRINTF1("\t flag %d", vt->v_part[i].p_flag);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8257c478bd9Sstevel@tonic-gate 	if (check_vtoc_sanity(handle, fd, vt) < 0) {
8267c478bd9Sstevel@tonic-gate 		return (-1);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate #ifdef DEBUG
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	int32_t i;
8317c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++) {
8327c478bd9Sstevel@tonic-gate 		DPRINTF1("\npart %d\n", i);
833342440ecSPrasad Singamsetty 		DPRINTF1("\t start %llu",  vt->v_part[i].p_start);
834342440ecSPrasad Singamsetty 		DPRINTF1("\t size %llu ", vt->v_part[i].p_size);
8357c478bd9Sstevel@tonic-gate 		DPRINTF1("\t tag %d", vt->v_part[i].p_tag);
8367c478bd9Sstevel@tonic-gate 		DPRINTF1("\t flag %d", vt->v_part[i].p_flag);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8407c478bd9Sstevel@tonic-gate 	return (0);
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
843342440ecSPrasad Singamsetty #define	SWAP(a, b)	{diskaddr_t tmp; tmp = (a); (a) = (b); (b) = tmp; }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * On x86 Solaris, the partitioning is done in two levels, fdisk and Solaris
8477c478bd9Sstevel@tonic-gate  * VTOC. Where as, on sparc solaris, it is only VTOC. On floppy and PCMCIA
8487c478bd9Sstevel@tonic-gate  * also it is assumed to be only VTOC, no fdisk.
8497c478bd9Sstevel@tonic-gate  *
8507c478bd9Sstevel@tonic-gate  * On sparc, the back up slice can cover the whole medium. But on x86
8517c478bd9Sstevel@tonic-gate  * (SCSI/ATAPI disks), the backup slice can cover the solaris partition
8527c478bd9Sstevel@tonic-gate  * in fdisk table.
8537c478bd9Sstevel@tonic-gate  *	Following table describes how is it handled
8547c478bd9Sstevel@tonic-gate  * SPARC:
855e74ff653SToomas Soome  *	SCSI/ATAPI, floppy, pcmcia : don't check for fdisk.
8567c478bd9Sstevel@tonic-gate  *				DKIOCGGEOM is sufficient.
8577c478bd9Sstevel@tonic-gate  * x86 : floppy, pcmcia : Don't check for fdisk. DKIOCGGEOM is sufficient.
858e74ff653SToomas Soome  *	SCSI/ATAPI : Check for fdisk.
8597c478bd9Sstevel@tonic-gate  *			if not present, assume that the solaris
8607c478bd9Sstevel@tonic-gate  *				partition covers 100% of the medium
861e74ff653SToomas Soome  *				(minus one cylinder).
8627c478bd9Sstevel@tonic-gate  *
863e74ff653SToomas Soome  *		if present :
8647c478bd9Sstevel@tonic-gate  *				check for active solaris partition.
865e74ff653SToomas Soome  *				if not found, take the first solaris
8667c478bd9Sstevel@tonic-gate  *					partition.
8677c478bd9Sstevel@tonic-gate  *			If there are no solaris partitions, its an error, stop.
8687c478bd9Sstevel@tonic-gate  */
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate static int32_t
871342440ecSPrasad Singamsetty check_vtoc_sanity(smedia_handle_t handle, int32_t fd, struct extvtoc *vt)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	int32_t i, j;
8757c478bd9Sstevel@tonic-gate 	struct dk_geom dkg;
8767c478bd9Sstevel@tonic-gate 	int32_t num_backup = 0;
877342440ecSPrasad Singamsetty 	diskaddr_t backup_size = 0;
8787c478bd9Sstevel@tonic-gate 	struct part_struct {
879342440ecSPrasad Singamsetty 		diskaddr_t start;
880342440ecSPrasad Singamsetty 		diskaddr_t end;
8817c478bd9Sstevel@tonic-gate 		int32_t num;
8827c478bd9Sstevel@tonic-gate 	} part[NDKMAP];
883342440ecSPrasad Singamsetty 	diskaddr_t min_val;
884342440ecSPrasad Singamsetty 	int32_t min_slice, num_slices;
885342440ecSPrasad Singamsetty 	diskaddr_t media_size;
886342440ecSPrasad Singamsetty 	uint32_t cyl_size;
8877c478bd9Sstevel@tonic-gate 	int sparc_style = 0;	/* sparc_style handling ? */
8887c478bd9Sstevel@tonic-gate 	struct fdisk_info fdisk;
8897c478bd9Sstevel@tonic-gate 	int sol_part;
8907c478bd9Sstevel@tonic-gate 	int total_parts = 0;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate #ifdef sparc
8937c478bd9Sstevel@tonic-gate 	sparc_style = 1;
8947c478bd9Sstevel@tonic-gate #endif /* sparc */
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if ((med_info.sm_media_type == SM_FLOPPY) ||
8977c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_PCMCIA_MEM) ||
8987c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_PCMCIA_ATA) ||
8997c478bd9Sstevel@tonic-gate 	    (med_info.sm_media_type == SM_SCSI_FLOPPY)) {
9007c478bd9Sstevel@tonic-gate 		sparc_style = 1;
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (sparc_style) {
9047c478bd9Sstevel@tonic-gate 		DPRINTF("sparc style true\n");
9057c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) {
9067c478bd9Sstevel@tonic-gate 			PERROR("DKIOCGGEOM Failed");
9077c478bd9Sstevel@tonic-gate 			return (-1);
9087c478bd9Sstevel@tonic-gate 		}
909342440ecSPrasad Singamsetty 		media_size = (diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead *
910342440ecSPrasad Singamsetty 		    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"));
941342440ecSPrasad Singamsetty 			media_size = (diskaddr_t)fdisk.part[sol_part].numsect;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 			DPRINTF1("sol_part %d\n", sol_part);
944342440ecSPrasad Singamsetty 			DPRINTF1("media_size %llu\n", 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;
959342440ecSPrasad Singamsetty 			media_size = (diskaddr_t)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) {
971342440ecSPrasad Singamsetty 		media_size = (uint32_t)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,
1009622a268dSPavel Potoplyak 		    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++) {
1034342440ecSPrasad Singamsetty 		DPRINTF4("\n %d (%d) : %llu, %llu", i, part[i].num,
1035342440ecSPrasad Singamsetty 		    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,
1045342440ecSPrasad Singamsetty 			    gettext("media size : %llu sectors \n"),
1046622a268dSPavel Potoplyak 			    media_size);
10477c478bd9Sstevel@tonic-gate 		} else {
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10507c478bd9Sstevel@tonic-gate 			    gettext("Backup slice extends beyond size of FDISK \
10517c478bd9Sstevel@tonic-gate Solaris partition\n"));
10527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10537c478bd9Sstevel@tonic-gate 			    gettext(
1054342440ecSPrasad Singamsetty 			"FDISK Solaris partition size : %llu sectors \n"),
10557c478bd9Sstevel@tonic-gate 			    media_size);
10567c478bd9Sstevel@tonic-gate 		}
10577c478bd9Sstevel@tonic-gate 		return (-1);
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/*
10617c478bd9Sstevel@tonic-gate 	 * If we have only backup slice return success here.
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	if (num_slices == 0)
10647c478bd9Sstevel@tonic-gate 		return (0);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (backup_size) {
10677c478bd9Sstevel@tonic-gate 		if (part[num_slices - 1].end > backup_size) {
10687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10697c478bd9Sstevel@tonic-gate 			    gettext("Slice %d extends beyond backup slice.\n"),
10707c478bd9Sstevel@tonic-gate 			    part[num_slices -1].num);
10717c478bd9Sstevel@tonic-gate 			return (-1);
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate 	} else {
10747c478bd9Sstevel@tonic-gate 		if (part[num_slices - 1].end > media_size) {
10757c478bd9Sstevel@tonic-gate 			if (sparc_style) {
10767c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10777c478bd9Sstevel@tonic-gate 				    gettext(
10787c478bd9Sstevel@tonic-gate 				"Slice %d extends beyond media size\n"),
10797c478bd9Sstevel@tonic-gate 				    part[num_slices -1].num);
10807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1081342440ecSPrasad Singamsetty 				    gettext("media size : %llu sectors \n"),
10827c478bd9Sstevel@tonic-gate 				    media_size);
10837c478bd9Sstevel@tonic-gate 			} else {
10847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1085622a268dSPavel Potoplyak 				    gettext("Slice %d extends beyond FDISK"
1086622a268dSPavel Potoplyak 				    " Solaris partition size\n"),
1087622a268dSPavel Potoplyak 				    part[num_slices -1].num);
1088342440ecSPrasad Singamsetty 				(void) fprintf(stderr, gettext(
1089342440ecSPrasad Singamsetty 				    "FDISK Solaris partition size : %llu "
1090342440ecSPrasad Singamsetty 				    "sectors \n"), media_size);
10917c478bd9Sstevel@tonic-gate 			}
10927c478bd9Sstevel@tonic-gate 			return (-1);
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_slices; i++) {
10997c478bd9Sstevel@tonic-gate 		if (i == 0)
11007c478bd9Sstevel@tonic-gate 			continue;
11017c478bd9Sstevel@tonic-gate 		if (part[i].start <= part[i-1].end) {
11027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1103622a268dSPavel Potoplyak 			    gettext("Overlap between slices %d and %d\n"),
1104622a268dSPavel Potoplyak 			    part[i-1].num, part[i].num);
11057c478bd9Sstevel@tonic-gate 			(void) smedia_release_handle(handle);
11067c478bd9Sstevel@tonic-gate 			(void) close(fd);
11077c478bd9Sstevel@tonic-gate 			exit(1);
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	return (0);
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate static int32_t
11167c478bd9Sstevel@tonic-gate get_fdisk(smedia_handle_t handle, int32_t fd, int32_t offset,
1117e74ff653SToomas Soome     struct fdisk_info *fdisk)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	struct mboot *boot_sec;
11207c478bd9Sstevel@tonic-gate 	struct ipart *part;
11217c478bd9Sstevel@tonic-gate 	char *buf;
11227c478bd9Sstevel@tonic-gate 	int32_t i, ret;
11237c478bd9Sstevel@tonic-gate 	int	save_errno;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/* Read the master boot program */
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	buf = (char *)malloc(med_info.sm_blocksize);
11287c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
11297c478bd9Sstevel@tonic-gate 		PERROR("malloc failed");
11307c478bd9Sstevel@tonic-gate 		exit(1);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	errno = 0;
11337c478bd9Sstevel@tonic-gate 	ret = ioctl(fd, DKIOCGMBOOT, buf);
11347c478bd9Sstevel@tonic-gate 	if (ret < 0) {
11357c478bd9Sstevel@tonic-gate 		if (errno != ENOTTY) {
11367c478bd9Sstevel@tonic-gate 			PERROR("DKIOCGMBOOT ioctl failed");
11377c478bd9Sstevel@tonic-gate 			return (-1);
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 
1140e3397557Szk 		/* Turn on privileges. */
1141e3397557Szk 		(void) __priv_bracket(PRIV_ON);
11427c478bd9Sstevel@tonic-gate 
1143342440ecSPrasad Singamsetty 		ret = smedia_raw_read(handle,
1144342440ecSPrasad Singamsetty 		    (diskaddr_t)offset/med_info.sm_blocksize,
1145342440ecSPrasad Singamsetty 		    buf, med_info.sm_blocksize);
11467c478bd9Sstevel@tonic-gate 
1147e3397557Szk 		/* Turn off privileges. */
1148e3397557Szk 		(void) __priv_bracket(PRIV_OFF);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 		save_errno = errno;
11517c478bd9Sstevel@tonic-gate 		errno = save_errno;
11527c478bd9Sstevel@tonic-gate 		if (ret != med_info.sm_blocksize) {
11537c478bd9Sstevel@tonic-gate 			if (errno == ENOTSUP) {
11547c478bd9Sstevel@tonic-gate 				errno = 0;
11557c478bd9Sstevel@tonic-gate 				if (lseek(fd, offset, SEEK_SET)) {
1156622a268dSPavel Potoplyak 					PERROR("Seek failed:");
1157622a268dSPavel Potoplyak 					free(buf);
1158622a268dSPavel Potoplyak 					return (-1);
11597c478bd9Sstevel@tonic-gate 				}
11607c478bd9Sstevel@tonic-gate 
1161e3397557Szk 				/* Turn on privileges. */
1162e3397557Szk 				(void) __priv_bracket(PRIV_ON);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 				ret = read(fd, buf, sizeof (struct mboot));
11657c478bd9Sstevel@tonic-gate 
1166e3397557Szk 				/* Turn off privileges. */
1167e3397557Szk 				(void) __priv_bracket(PRIV_OFF);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 				if (ret != sizeof (struct mboot)) {
1170622a268dSPavel Potoplyak 					PERROR("Could not read "
1171622a268dSPavel Potoplyak 					    "master boot record");
1172622a268dSPavel Potoplyak 					free(buf);
1173622a268dSPavel Potoplyak 					return (-1);
11747c478bd9Sstevel@tonic-gate 				}
11757c478bd9Sstevel@tonic-gate 			} else {
11767c478bd9Sstevel@tonic-gate 				PERROR("Could not read master boot record");
11777c478bd9Sstevel@tonic-gate 				free(buf);
11787c478bd9Sstevel@tonic-gate 				return (-1);
11797c478bd9Sstevel@tonic-gate 			}
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
11837c478bd9Sstevel@tonic-gate 	boot_sec = (struct mboot *)buf;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	/* Is this really a master boot record? */
11867c478bd9Sstevel@tonic-gate 	if (les(boot_sec->signature) != MBB_MAGIC) {
11877c478bd9Sstevel@tonic-gate 		DPRINTF("fdisk: Invalid master boot file \n");
11887c478bd9Sstevel@tonic-gate 		DPRINTF2("Bad magic number: is %x, should be %x.\n",
1189622a268dSPavel Potoplyak 		    les(boot_sec->signature), MBB_MAGIC);
11907c478bd9Sstevel@tonic-gate 		free(buf);
11917c478bd9Sstevel@tonic-gate 		return (-1);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
11957c478bd9Sstevel@tonic-gate 		DPRINTF1("part %d\n", i);
11967c478bd9Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
11977c478bd9Sstevel@tonic-gate 		part = (struct ipart *)&boot_sec->parts[i *
11987c478bd9Sstevel@tonic-gate 		    sizeof (struct ipart)];
11997c478bd9Sstevel@tonic-gate 		fdisk->part[i].bootid = part->bootid;
12007c478bd9Sstevel@tonic-gate 		if (part->bootid && (part->bootid != ACTIVE)) {
12017c478bd9Sstevel@tonic-gate 			/* Hmmm...not a valid fdisk! */
12027c478bd9Sstevel@tonic-gate 			return (-1);
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 		fdisk->part[i].systid = part->systid;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		/* To avoid the misalign access in sparc */
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		fdisk->part[i].relsect = lel(GET_32(&(part->relsect)));
12097c478bd9Sstevel@tonic-gate 		fdisk->part[i].numsect = lel(GET_32(&(part->numsect)));
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 		DPRINTF1("\tboot id 0x%x\n", part->bootid);
12127c478bd9Sstevel@tonic-gate 		DPRINTF1("\tsystem id 0x%x\n", part->systid);
12137c478bd9Sstevel@tonic-gate 		DPRINTF1("\trel sector 0x%x\n", fdisk->part[i].relsect);
12147c478bd9Sstevel@tonic-gate 		DPRINTF1("\tnum sector 0x%x\n", fdisk->part[i].numsect);
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 	free(buf);
12177c478bd9Sstevel@tonic-gate 	return (0);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * wrrite_defualt_label(int32_t fd)
1223e74ff653SToomas Soome  *	fd = file descriptor for the device.
12247c478bd9Sstevel@tonic-gate  *
12257c478bd9Sstevel@tonic-gate  * For sparc solaris
12267c478bd9Sstevel@tonic-gate  *	Create a vtoc partition with
12277c478bd9Sstevel@tonic-gate  *		slice 0 = slice 2 = medium capacity.
12287c478bd9Sstevel@tonic-gate  *	The cyl, head, sect (CHS) values are computed as done in sd
12297c478bd9Sstevel@tonic-gate  *	capacity <= 1GB,
12307c478bd9Sstevel@tonic-gate  *		nhead = 64, nsect = 32
12317c478bd9Sstevel@tonic-gate  *	capacity > 1gb,
12327c478bd9Sstevel@tonic-gate  *		nhead = 255, nsect = 63
12337c478bd9Sstevel@tonic-gate  *
12347c478bd9Sstevel@tonic-gate  * For x86 solaris
12357c478bd9Sstevel@tonic-gate  *	Create a fdisk partition,
12367c478bd9Sstevel@tonic-gate  *		partition 0 covers the full medium, the partition
12377c478bd9Sstevel@tonic-gate  *		type is set to Solaris.
12387c478bd9Sstevel@tonic-gate  *	Then create solaris vtoc. The algorithm is same as sparc solaris.
12397c478bd9Sstevel@tonic-gate  *	But the capacity is reduced by 1 cyl, to leave space for fdisk table.
12407c478bd9Sstevel@tonic-gate  */
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate #ifdef sparc
12437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12447c478bd9Sstevel@tonic-gate void
12457c478bd9Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 
1248342440ecSPrasad Singamsetty 	struct extvtoc v_toc;
1249342440ecSPrasad Singamsetty 	uint32_t nhead, numcyl, nsect;
1250342440ecSPrasad Singamsetty 	diskaddr_t capacity;
12517c478bd9Sstevel@tonic-gate 	int32_t ret;
12527c478bd9Sstevel@tonic-gate 	char asciilabel[LEN_DKL_ASCII];
12537c478bd9Sstevel@tonic-gate 	char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0";
1254342440ecSPrasad Singamsetty 	uint32_t acyl = 2;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	DPRINTF("Writing default vtoc\n");
12587c478bd9Sstevel@tonic-gate 	(void) memset(&v_toc, 0, sizeof (v_toc));
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	v_toc.v_nparts = V_NUMPAR;
12627c478bd9Sstevel@tonic-gate 	v_toc.v_sanity = VTOC_SANE;
12637c478bd9Sstevel@tonic-gate 	v_toc.v_version = V_VERSION;
12647c478bd9Sstevel@tonic-gate 	v_toc.v_sectorsz = DEV_BSIZE;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	/*
12677c478bd9Sstevel@tonic-gate 	 * For the head, cyl and number of sector per track,
12687c478bd9Sstevel@tonic-gate 	 * if the capacity <= 1GB, head = 64, sect = 32.
12697c478bd9Sstevel@tonic-gate 	 * else head = 255, sect 63
12707c478bd9Sstevel@tonic-gate 	 * NOTE: the capacity should be equal to C*H*S values.
12717c478bd9Sstevel@tonic-gate 	 * This will cause some truncation of size due to
12727c478bd9Sstevel@tonic-gate 	 * round off errors.
12737c478bd9Sstevel@tonic-gate 	 */
1274342440ecSPrasad Singamsetty 	if ((uint32_t)med_info.sm_capacity <= 0x200000) {
12757c478bd9Sstevel@tonic-gate 		nhead = 64;
12767c478bd9Sstevel@tonic-gate 		nsect = 32;
12777c478bd9Sstevel@tonic-gate 	} else {
12787c478bd9Sstevel@tonic-gate 		nhead = 255;
12797c478bd9Sstevel@tonic-gate 		nsect = 63;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
1282342440ecSPrasad Singamsetty 	numcyl = (uint32_t)med_info.sm_capacity / (nhead * nsect);
1283342440ecSPrasad Singamsetty 	capacity = (diskaddr_t)nhead * nsect * numcyl;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_start = 0;
12867c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_size = capacity;
12877c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_tag  = V_ROOT;
12887c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_flag = 0;	/* Mountable */
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_start = 0;
12917c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_size = capacity;
12927c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_tag  = V_BACKUP;
12937c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_flag = V_UNMNT;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/* Create asciilabel for compatibility with format utility */
12967c478bd9Sstevel@tonic-gate 	(void) snprintf(asciilabel, sizeof (asciilabel),
12977c478bd9Sstevel@tonic-gate 	    "%s cyl %d alt %d hd %d sec %d",
12987c478bd9Sstevel@tonic-gate 	    asciilabel2, numcyl, acyl, nhead, nsect);
12997c478bd9Sstevel@tonic-gate 	(void) memcpy(v_toc.v_asciilabel, asciilabel,
1300622a268dSPavel Potoplyak 	    LEN_DKL_ASCII);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	errno = 0;
13037c478bd9Sstevel@tonic-gate 
1304e3397557Szk 	/* Turn on privileges. */
1305e3397557Szk 	(void) __priv_bracket(PRIV_ON);
13067c478bd9Sstevel@tonic-gate 
1307342440ecSPrasad Singamsetty 	ret = write_extvtoc(fd, &v_toc);
13087c478bd9Sstevel@tonic-gate 
1309e3397557Szk 	/* Turn off privileges. */
1310e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	if (ret < 0) {
13137c478bd9Sstevel@tonic-gate 		PERROR("write VTOC failed");
13147c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate #else /* !sparc */
13197c478bd9Sstevel@tonic-gate #ifdef i386
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate void
13227c478bd9Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd)
13237c478bd9Sstevel@tonic-gate {
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	int32_t i, ret;
13267c478bd9Sstevel@tonic-gate 	struct dk_geom  dkg;
1327342440ecSPrasad Singamsetty 	struct extvtoc v_toc;
13287c478bd9Sstevel@tonic-gate 	int tmp_fd;
13297c478bd9Sstevel@tonic-gate 	char *fdisk_buf;
13307c478bd9Sstevel@tonic-gate 	struct mboot boot_code;		/* Buffer for master boot record */
13317c478bd9Sstevel@tonic-gate 	struct ipart parts[FD_NUMPART];
1332342440ecSPrasad Singamsetty 	uint32_t numcyl, nhead, nsect;
1333342440ecSPrasad Singamsetty 	uint32_t unixend;
1334342440ecSPrasad Singamsetty 	uint32_t blocksize;
1335342440ecSPrasad Singamsetty 	diskaddr_t capacity;
13367c478bd9Sstevel@tonic-gate 	int	save_errno;
13377c478bd9Sstevel@tonic-gate 	size_t	bytes_written;
13387c478bd9Sstevel@tonic-gate 	char asciilabel[LEN_DKL_ASCII];
13397c478bd9Sstevel@tonic-gate 	char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0";
1340342440ecSPrasad Singamsetty 	uint32_t acyl = 2;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	DPRINTF("Writing default fdisk table and vtoc\n");
13437c478bd9Sstevel@tonic-gate 	(void) memset(&v_toc, 0, sizeof (v_toc));
13447c478bd9Sstevel@tonic-gate 	/*
13457c478bd9Sstevel@tonic-gate 	 * Try getting disk geometry.
13467c478bd9Sstevel@tonic-gate 	 */
13477c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGGEOM, &dkg) < 0)
13487c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) {
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 			DPRINTF("DKIOCG_PHYGEOM ioctl failed");
13517c478bd9Sstevel@tonic-gate 			return;
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
1354e74ff653SToomas Soome 	tmp_fd = open("/boot/pmbr", O_RDONLY);
13557c478bd9Sstevel@tonic-gate 	if (tmp_fd <= 0) {
13567c478bd9Sstevel@tonic-gate 		return;
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	if (read(tmp_fd, &boot_code, sizeof (struct mboot))
13607c478bd9Sstevel@tonic-gate 			!= sizeof (struct mboot)) {
13617c478bd9Sstevel@tonic-gate 		(void) close(tmp_fd);
13627c478bd9Sstevel@tonic-gate 		return;
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	blocksize = med_info.sm_blocksize;
13667c478bd9Sstevel@tonic-gate 	fdisk_buf = (char *)malloc(blocksize);
13677c478bd9Sstevel@tonic-gate 	if (fdisk_buf == NULL) {
13687c478bd9Sstevel@tonic-gate 		DPRINTF("malloc for fdisk_buf failed\n");
13697c478bd9Sstevel@tonic-gate 		return;
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	(void) memset(&parts, 0, sizeof (parts));
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
13757c478bd9Sstevel@tonic-gate 		parts[i].systid = UNUSED;
13767c478bd9Sstevel@tonic-gate 		parts[i].numsect = lel(UNUSED);
13777c478bd9Sstevel@tonic-gate 		parts[i].relsect = lel(UNUSED);
13787c478bd9Sstevel@tonic-gate 		parts[i].bootid = 0;
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	numcyl = dkg.dkg_ncyl;
13827c478bd9Sstevel@tonic-gate 	nhead = dkg.dkg_nhead;
13837c478bd9Sstevel@tonic-gate 	nsect = dkg.dkg_nsect;
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	parts[0].bootid = ACTIVE;
13867c478bd9Sstevel@tonic-gate 	parts[0].begsect = 1;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	unixend = numcyl;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	parts[0].relsect = lel(nhead * nsect);
1391342440ecSPrasad Singamsetty 	parts[0].numsect = lel(((diskaddr_t)numcyl * nhead * nsect));
13927c478bd9Sstevel@tonic-gate 	parts[0].systid = SUNIXOS2;   /* Solaris */
13937c478bd9Sstevel@tonic-gate 	parts[0].beghead = 0;
13947c478bd9Sstevel@tonic-gate 	parts[0].begcyl = 1;
13957c478bd9Sstevel@tonic-gate 	parts[0].endhead = nhead - 1;
13967c478bd9Sstevel@tonic-gate 	parts[0].endsect = (nsect & 0x3f) |
13977c478bd9Sstevel@tonic-gate 	    (char)((unixend >> 2) & 0x00c0);
13987c478bd9Sstevel@tonic-gate 	parts[0].endcyl = (char)(unixend & 0x00ff);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	(void) memcpy(&(boot_code.parts), parts, sizeof (parts));
14017c478bd9Sstevel@tonic-gate 	(void) memcpy(fdisk_buf, &boot_code, sizeof (boot_code));
14027c478bd9Sstevel@tonic-gate 
1403e3397557Szk 	/* Turn on privileges. */
1404e3397557Szk 	(void) __priv_bracket(PRIV_ON);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	ret = ioctl(fd, DKIOCSMBOOT, fdisk_buf);
14077c478bd9Sstevel@tonic-gate 
1408e3397557Szk 	/* Turn off privileges. */
1409e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (ret == -1) {
14127c478bd9Sstevel@tonic-gate 		if (errno != ENOTTY) {
14137c478bd9Sstevel@tonic-gate 			PERROR("DKIOCSMBOOT ioctl Failed");
14147c478bd9Sstevel@tonic-gate 			return;
14157c478bd9Sstevel@tonic-gate 		}
14167c478bd9Sstevel@tonic-gate 
1417e3397557Szk 		/* Turn on privileges. */
1418e3397557Szk 		(void) __priv_bracket(PRIV_ON);
14197c478bd9Sstevel@tonic-gate 
1420342440ecSPrasad Singamsetty 		bytes_written = smedia_raw_write(handle, (diskaddr_t)0,
1421342440ecSPrasad Singamsetty 		    fdisk_buf, blocksize);
14227c478bd9Sstevel@tonic-gate 
1423e3397557Szk 		/* Turn off privileges. */
1424e3397557Szk 		(void) __priv_bracket(PRIV_OFF);
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		save_errno = errno;
14277c478bd9Sstevel@tonic-gate 		errno = save_errno;
14287c478bd9Sstevel@tonic-gate 		if (bytes_written != blocksize) {
14297c478bd9Sstevel@tonic-gate 			if (errno == ENOTSUP) {
14307c478bd9Sstevel@tonic-gate 
1431e3397557Szk 			    /* Turn on privileges. */
1432622a268dSPavel Potoplyak 				(void) __priv_bracket(PRIV_ON);
14337c478bd9Sstevel@tonic-gate 
1434622a268dSPavel Potoplyak 				ret = write(fd, fdisk_buf, blocksize);
14357c478bd9Sstevel@tonic-gate 
1436e3397557Szk 			    /* Turn off privileges. */
1437622a268dSPavel Potoplyak 				(void) __priv_bracket(PRIV_OFF);
14387c478bd9Sstevel@tonic-gate 
1439622a268dSPavel Potoplyak 				if (ret != blocksize) {
14407c478bd9Sstevel@tonic-gate 					return;
1441622a268dSPavel Potoplyak 				}
14427c478bd9Sstevel@tonic-gate 			} else {
14437c478bd9Sstevel@tonic-gate 				return;
14447c478bd9Sstevel@tonic-gate 			}
14457c478bd9Sstevel@tonic-gate 		}
14467c478bd9Sstevel@tonic-gate 	}
1447342440ecSPrasad Singamsetty 	capacity = (diskaddr_t)(numcyl - 1) * nhead * nsect;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	v_toc.v_nparts = V_NUMPAR;
14507c478bd9Sstevel@tonic-gate 	v_toc.v_sanity = VTOC_SANE;
14517c478bd9Sstevel@tonic-gate 	v_toc.v_version = V_VERSION;
14527c478bd9Sstevel@tonic-gate 	v_toc.v_sectorsz = DEV_BSIZE;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_start = 0;
14557c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_size = capacity;
14567c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_tag  = V_ROOT;
14577c478bd9Sstevel@tonic-gate 	v_toc.v_part[0].p_flag = 0;	/* Mountable */
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_start = 0;
14607c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_size = capacity;
14617c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_tag  = V_BACKUP;
14627c478bd9Sstevel@tonic-gate 	v_toc.v_part[2].p_flag = V_UNMNT;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	/* Create asciilabel for compatibility with format utility */
14657c478bd9Sstevel@tonic-gate 	(void) snprintf(asciilabel, sizeof (asciilabel),
14667c478bd9Sstevel@tonic-gate 	    "%s cyl %d alt %d hd %d sec %d",
14677c478bd9Sstevel@tonic-gate 	    asciilabel2, numcyl, acyl, nhead, nsect);
14687c478bd9Sstevel@tonic-gate 	(void) memcpy(v_toc.v_asciilabel, asciilabel,
1469622a268dSPavel Potoplyak 	    LEN_DKL_ASCII);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	errno = 0;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 
1474e3397557Szk 	/* Turn on privileges. */
1475e3397557Szk 	(void) __priv_bracket(PRIV_ON);
14767c478bd9Sstevel@tonic-gate 
1477342440ecSPrasad Singamsetty 	ret = write_extvtoc(fd, &v_toc);
14787c478bd9Sstevel@tonic-gate 
1479e3397557Szk 	/* Turn off privileges. */
1480e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if (ret < 0) {
14837c478bd9Sstevel@tonic-gate 		PERROR("write VTOC failed");
14847c478bd9Sstevel@tonic-gate 		DPRINTF1("Errno = %d\n", errno);
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate #else	/* !i386 */
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate #error One of sparc or i386 must be defined!
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate #endif /* i386 */
14937c478bd9Sstevel@tonic-gate #endif /* sparc */
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate  * void overwrite_metadata(int32_t fd, smedia_handle_t handle)
14977c478bd9Sstevel@tonic-gate  *
14987c478bd9Sstevel@tonic-gate  * purpose : quick format does not erase the data on Iomega
14997c478bd9Sstevel@tonic-gate  * zip/jaz media. So, the meta data on the disk should be erased.
15007c478bd9Sstevel@tonic-gate  *
15017c478bd9Sstevel@tonic-gate  * If there is a valid fdisk table,
1502e74ff653SToomas Soome  *	erase first 64K of each partition.
15037c478bd9Sstevel@tonic-gate  * If there is a valid vtoc,
1504e74ff653SToomas Soome  *	erase first 64k of each slice.
15057c478bd9Sstevel@tonic-gate  * Then erase the 0th sector (the home for vtoc and fdisk) of the disk.
15067c478bd9Sstevel@tonic-gate  * Note that teh vtoc on x86 resides in one of the fdisk partition.
15077c478bd9Sstevel@tonic-gate  * So delay the erasing of the solaris partition until the vtoc is read.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate void
15117c478bd9Sstevel@tonic-gate overwrite_metadata(int32_t fd, smedia_handle_t handle)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	struct fdisk_info fdisk;
1515342440ecSPrasad Singamsetty 	diskaddr_t sol_offset = 0;
15167c478bd9Sstevel@tonic-gate 	int i, ret;
1517342440ecSPrasad Singamsetty 	struct extvtoc t_vtoc;
15187c478bd9Sstevel@tonic-gate #ifdef i386
1519342440ecSPrasad Singamsetty 	diskaddr_t sol_size = 0;
15207c478bd9Sstevel@tonic-gate 	int32_t active = 0;
15217c478bd9Sstevel@tonic-gate #endif /* i386 */
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/* Get fdisk info. */
15247c478bd9Sstevel@tonic-gate 	if (get_fdisk(handle, fd, 0, &fdisk) >= 0) {
15257c478bd9Sstevel@tonic-gate 		/* Got a valid fdisk */
15267c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 			if (fdisk.part[i].numsect == 0)
15297c478bd9Sstevel@tonic-gate 				continue;
15307c478bd9Sstevel@tonic-gate 			if ((fdisk.part[i].systid == UNUSED) ||
1531622a268dSPavel Potoplyak 			    (fdisk.part[i].systid == 0))
15327c478bd9Sstevel@tonic-gate 				continue;
15337c478bd9Sstevel@tonic-gate #ifdef i386
15347c478bd9Sstevel@tonic-gate 			if (fdisk.part[i].systid == SUNIXOS ||
15357c478bd9Sstevel@tonic-gate 			    fdisk.part[i].systid == SUNIXOS2) {
15367c478bd9Sstevel@tonic-gate 				if (!sol_offset) {
15377c478bd9Sstevel@tonic-gate 					sol_offset = fdisk.part[i].relsect;
15387c478bd9Sstevel@tonic-gate 					sol_size = fdisk.part[i].numsect;
15397c478bd9Sstevel@tonic-gate 					if (fdisk.part[i].bootid == ACTIVE)
15407c478bd9Sstevel@tonic-gate 						active = 1;
15417c478bd9Sstevel@tonic-gate 					continue;
15427c478bd9Sstevel@tonic-gate 				} else if ((fdisk.part[i].bootid == ACTIVE) &&
15437c478bd9Sstevel@tonic-gate 				    (!active)) {
15447c478bd9Sstevel@tonic-gate 					erase(handle, sol_offset, sol_size);
15457c478bd9Sstevel@tonic-gate 					sol_offset = fdisk.part[i].relsect;
15467c478bd9Sstevel@tonic-gate 					sol_size = fdisk.part[i].numsect;
15477c478bd9Sstevel@tonic-gate 					active = 1;
15487c478bd9Sstevel@tonic-gate 					continue;
15497c478bd9Sstevel@tonic-gate 				}
15507c478bd9Sstevel@tonic-gate 			}
15517c478bd9Sstevel@tonic-gate #endif /* i386 */
1552342440ecSPrasad Singamsetty 			erase(handle, (diskaddr_t)fdisk.part[i].relsect,
1553342440ecSPrasad Singamsetty 			    (diskaddr_t)fdisk.part[i].numsect);
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	(void) memset(&t_vtoc, 0, sizeof (t_vtoc));
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (sol_offset) {
15607c478bd9Sstevel@tonic-gate 		/* fdisk x86 Solaris partition */
15617c478bd9Sstevel@tonic-gate 		/* VTOC location in solaris partition is DK_LABEL_LOC */
15627c478bd9Sstevel@tonic-gate 
1563e3397557Szk 		/* Turn on privileges. */
1564e3397557Szk 		(void) __priv_bracket(PRIV_ON);
15657c478bd9Sstevel@tonic-gate 
1566342440ecSPrasad Singamsetty 		ret = read_extvtoc(fd, &t_vtoc);
15677c478bd9Sstevel@tonic-gate 
1568e3397557Szk 		/* Turn off privileges. */
15690c887d22Szk 		(void) __priv_bracket(PRIV_OFF);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 		if (ret < 0) {
15727c478bd9Sstevel@tonic-gate 			/* No valid vtoc, erase fdisk table. */
1573342440ecSPrasad Singamsetty 			erase(handle, (diskaddr_t)0, (diskaddr_t)1);
15747c478bd9Sstevel@tonic-gate 			return;
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 	} else {
15777c478bd9Sstevel@tonic-gate 		/* Sparc Solaris or x86 solaris with faked fdisk */
15787c478bd9Sstevel@tonic-gate 
1579e3397557Szk 		/* Turn on privileges */
1580e3397557Szk 		(void) __priv_bracket(PRIV_ON);
15817c478bd9Sstevel@tonic-gate 
1582342440ecSPrasad Singamsetty 		ret = read_extvtoc(fd, &t_vtoc);
15837c478bd9Sstevel@tonic-gate 
1584e3397557Szk 		/* Turn off privileges. */
1585e3397557Szk 		(void) __priv_bracket(PRIV_OFF);
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		if (ret < 0) {
15887c478bd9Sstevel@tonic-gate 			/* No valid vtoc, erase from 0th sector */
1589342440ecSPrasad Singamsetty 			erase(handle, (diskaddr_t)0,
1590342440ecSPrasad Singamsetty 			    (uint32_t)med_info.sm_capacity);
15917c478bd9Sstevel@tonic-gate 			return;
15927c478bd9Sstevel@tonic-gate 		}
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++) {
15967c478bd9Sstevel@tonic-gate 		if (t_vtoc.v_part[i].p_size != 0) {
15977c478bd9Sstevel@tonic-gate 			erase(handle, sol_offset + t_vtoc.v_part[i].p_start,
1598622a268dSPavel Potoplyak 			    t_vtoc.v_part[i].p_size);
15997c478bd9Sstevel@tonic-gate 			/*
16007c478bd9Sstevel@tonic-gate 			 * To make the udfs not recognise the partition we will
16017c478bd9Sstevel@tonic-gate 			 * erase sectors 256, (p_size-256) and psize.
16027c478bd9Sstevel@tonic-gate 			 */
16037c478bd9Sstevel@tonic-gate 			erase(handle,
1604342440ecSPrasad Singamsetty 			    sol_offset + t_vtoc.v_part[i].p_start + 256,
1605342440ecSPrasad Singamsetty 			    (diskaddr_t)1);
16067c478bd9Sstevel@tonic-gate 			erase(handle,
1607342440ecSPrasad Singamsetty 			    (sol_offset + t_vtoc.v_part[i].p_start +
1608342440ecSPrasad Singamsetty 			    t_vtoc.v_part[i].p_size - 256),
1609342440ecSPrasad Singamsetty 			    (diskaddr_t)1);
16107c478bd9Sstevel@tonic-gate 			erase(handle,
1611342440ecSPrasad Singamsetty 			    (sol_offset + t_vtoc.v_part[i].p_start +
1612342440ecSPrasad Singamsetty 			    t_vtoc.v_part[i].p_size - 1),
1613342440ecSPrasad Singamsetty 			    (diskaddr_t)1);
16147c478bd9Sstevel@tonic-gate 		}
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * If x86 fdisk solaris partition, erase the vtoc also.
16197c478bd9Sstevel@tonic-gate 	 * for sparc, the erasing 0the sector erases vtoc.
16207c478bd9Sstevel@tonic-gate 	 */
16217c478bd9Sstevel@tonic-gate 	if (sol_offset) {
1622342440ecSPrasad Singamsetty 		erase(handle, sol_offset, (diskaddr_t)DK_LABEL_LOC + 2);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	/*
16267c478bd9Sstevel@tonic-gate 	 * erase the 0th sector, it is not guaranteed to be
16277c478bd9Sstevel@tonic-gate 	 * erased in the above sequence.
16287c478bd9Sstevel@tonic-gate 	 */
16297c478bd9Sstevel@tonic-gate 
1630342440ecSPrasad Singamsetty 	erase(handle, (diskaddr_t)0, (diskaddr_t)1);
16317c478bd9Sstevel@tonic-gate }
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate /*
16347c478bd9Sstevel@tonic-gate  * void erase(smedia_handle_t handle, uint32_t offset, uint32_t size)
16357c478bd9Sstevel@tonic-gate  *
16367c478bd9Sstevel@tonic-gate  * Initialize the media with '0' from offset 'offset' upto 'size'
16377c478bd9Sstevel@tonic-gate  * or 128 blocks(64k), whichever is smaller.
16387c478bd9Sstevel@tonic-gate  */
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate static void
1641342440ecSPrasad Singamsetty erase(smedia_handle_t handle, diskaddr_t offset, diskaddr_t size)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	char *buf;
1644342440ecSPrasad Singamsetty 	diskaddr_t nblocks = size;
16457c478bd9Sstevel@tonic-gate 	int32_t ret;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	nblocks = (nblocks < 128) ? nblocks : 128;
16497c478bd9Sstevel@tonic-gate 	buf = (char *)malloc(nblocks * med_info.sm_blocksize);
16507c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
16517c478bd9Sstevel@tonic-gate 		PERROR("malloc failed");
16527c478bd9Sstevel@tonic-gate 		return;
16537c478bd9Sstevel@tonic-gate 	}
1654342440ecSPrasad Singamsetty 	(void) memset(buf, 0, (size_t)nblocks * med_info.sm_blocksize);
16557c478bd9Sstevel@tonic-gate 
1656e3397557Szk 	/* Turn on privileges. */
1657e3397557Szk 	(void) __priv_bracket(PRIV_ON);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	ret = smedia_raw_write(handle, offset, buf,
1660342440ecSPrasad Singamsetty 	    (size_t)nblocks * med_info.sm_blocksize);
16617c478bd9Sstevel@tonic-gate 
1662e3397557Szk 	/* Turn off privileges. */
1663e3397557Szk 	(void) __priv_bracket(PRIV_OFF);
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	if (ret != (nblocks * med_info.sm_blocksize))
16667c478bd9Sstevel@tonic-gate 		PERROR("error in writing\n");
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	free(buf);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate }
1671