10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52160Szk194757 * Common Development and Distribution License (the "License").
62160Szk194757 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9320SPavel.Potoplyak@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * rmf_slice.c :
280Sstevel@tonic-gate * This file contains the functions for parsing a slice file
290Sstevel@tonic-gate * for rmformat.
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <sys/vtoc.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <memory.h>
410Sstevel@tonic-gate #include <dirent.h>
420Sstevel@tonic-gate #include <sys/fcntl.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <sys/dkio.h>
472172Szk194757 #include <priv_utils.h>
480Sstevel@tonic-gate #include "rmformat.h"
490Sstevel@tonic-gate
500Sstevel@tonic-gate extern void my_perror(char *err_string);
510Sstevel@tonic-gate
520Sstevel@tonic-gate static int32_t last_token_type = 0;
530Sstevel@tonic-gate #define spc() (last_token_type)
540Sstevel@tonic-gate
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * This global is used to store the current line # in the
580Sstevel@tonic-gate * data file. It must be global because the I/O routines
590Sstevel@tonic-gate * are allowed to side effect it to keep track of backslashed
600Sstevel@tonic-gate * newlines.
610Sstevel@tonic-gate */
620Sstevel@tonic-gate
630Sstevel@tonic-gate static int32_t data_lineno; /* current line # in data file */
640Sstevel@tonic-gate
650Sstevel@tonic-gate #define CHG_MODE_UNDEFINED (-1) /* undefined value */
660Sstevel@tonic-gate #define CHG_MODE_SET 0 /* set bits by or'ing */
670Sstevel@tonic-gate #define CHG_MODE_CLR 1 /* clr bits by and'ing */
680Sstevel@tonic-gate #define CHG_MODE_ABS 2 /* set absolute value */
690Sstevel@tonic-gate
700Sstevel@tonic-gate
710Sstevel@tonic-gate #define TOKEN_SIZE 36 /* max length of a token */
720Sstevel@tonic-gate typedef char TOKEN[TOKEN_SIZE+1]; /* token type */
730Sstevel@tonic-gate #define DATA_INPUT 0 /* 2 modes of input */
740Sstevel@tonic-gate #define CMD_INPUT 1
750Sstevel@tonic-gate #define WILD_STRING "$" /* wildcard character */
760Sstevel@tonic-gate #define COMMENT_CHAR '#' /* comment character */
770Sstevel@tonic-gate
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate * List of strings with arbitrary matching values
800Sstevel@tonic-gate */
810Sstevel@tonic-gate typedef struct slist {
820Sstevel@tonic-gate char *str;
830Sstevel@tonic-gate char *help;
840Sstevel@tonic-gate int32_t value;
850Sstevel@tonic-gate } slist_t;
860Sstevel@tonic-gate
870Sstevel@tonic-gate static slist_t ptag_choices[] = {
880Sstevel@tonic-gate { "unassigned", "", V_UNASSIGNED },
890Sstevel@tonic-gate { "boot", "", V_BOOT },
900Sstevel@tonic-gate { "root", "", V_ROOT },
910Sstevel@tonic-gate { "swap", "", V_SWAP },
920Sstevel@tonic-gate { "usr", "", V_USR },
930Sstevel@tonic-gate { "backup", "", V_BACKUP },
940Sstevel@tonic-gate { "stand", "", V_STAND },
950Sstevel@tonic-gate { "var", "", V_VAR },
960Sstevel@tonic-gate { "home", "", V_HOME },
970Sstevel@tonic-gate { "alternates", "", V_ALTSCTR },
980Sstevel@tonic-gate { NULL }
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * Choices for the p_flag vtoc field
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate static slist_t pflag_choices[] = {
1060Sstevel@tonic-gate { "wm", "read-write, mountable", 0 },
1070Sstevel@tonic-gate { "wu", "read-write, unmountable", V_UNMNT },
1080Sstevel@tonic-gate { "rm", "read-only, mountable", V_RONLY },
1090Sstevel@tonic-gate { "ru", "read-only, unmountable", V_RONLY|V_UNMNT },
1100Sstevel@tonic-gate { NULL }
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * The definitions are the token types that the data file parser recognizes.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate #define SUP_EOF -1 /* eof token */
1170Sstevel@tonic-gate #define SUP_STRING 0 /* string token */
1180Sstevel@tonic-gate #define SUP_EQL 1 /* equals token */
1190Sstevel@tonic-gate #define SUP_COMMA 2 /* comma token */
1200Sstevel@tonic-gate #define SUP_COLON 3 /* colon token */
1210Sstevel@tonic-gate #define SUP_EOL 4 /* newline token */
1220Sstevel@tonic-gate #define SUP_OR 5 /* vertical bar */
1230Sstevel@tonic-gate #define SUP_AND 6 /* ampersand */
1240Sstevel@tonic-gate #define SUP_TILDE 7 /* tilde */
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * Prototypes for ANSI C compilers
1290Sstevel@tonic-gate */
1307563SPrasad.Singamsetty@Sun.COM static int32_t sup_prxfile(char *file_name, struct extvtoc *vt);
1317563SPrasad.Singamsetty@Sun.COM static int32_t sup_setpart(struct extvtoc *vt);
1320Sstevel@tonic-gate static void sup_pushchar(int32_t c);
1330Sstevel@tonic-gate static void clean_token(char *cleantoken, char *token);
1340Sstevel@tonic-gate static void clean_token(char *cleantoken, char *token);
1350Sstevel@tonic-gate static int32_t sup_inputchar();
1360Sstevel@tonic-gate static int32_t sup_gettoken(char *buf);
1370Sstevel@tonic-gate static int32_t sup_get_token(char *buf);
1380Sstevel@tonic-gate static int32_t find_value(slist_t *slist, char *str, int32_t *value);
1397563SPrasad.Singamsetty@Sun.COM static int32_t check_vtoc_sanity(smedia_handle_t, int32_t fd,
1407563SPrasad.Singamsetty@Sun.COM struct extvtoc *vt);
1417563SPrasad.Singamsetty@Sun.COM static uint64_t str2sector(char *str);
1420Sstevel@tonic-gate static int32_t strcnt(char *s1, char *s2);
1430Sstevel@tonic-gate static int32_t get_fdisk(smedia_handle_t, int32_t fd, int32_t offset,
1440Sstevel@tonic-gate struct fdisk_info *fdisk);
1457563SPrasad.Singamsetty@Sun.COM static void erase(smedia_handle_t handle, diskaddr_t offset, diskaddr_t size);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate extern char *myname;
1487563SPrasad.Singamsetty@Sun.COM extern uint64_t my_atoll(char *ptr);
1490Sstevel@tonic-gate extern smmedium_prop_t med_info;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate static FILE *data_file;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate static int32_t
sup_prxfile(char * file_name,struct extvtoc * vt)1547563SPrasad.Singamsetty@Sun.COM sup_prxfile(char *file_name, struct extvtoc *vt)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate int32_t status, ret_val;
1570Sstevel@tonic-gate TOKEN token;
1580Sstevel@tonic-gate TOKEN cleaned;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate * Open the data file. Return 0 if unable to do so.
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate data_file = fopen(file_name, "r");
1640Sstevel@tonic-gate if (data_file == NULL) {
1650Sstevel@tonic-gate PERROR("Open failed");
1660Sstevel@tonic-gate return (-1);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * Step through the data file a meta-line at a time. There are
1700Sstevel@tonic-gate * typically several backslashed newlines in each meta-line,
1710Sstevel@tonic-gate * so data_lineno will be getting side effected along the way.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate data_lineno = 1;
1740Sstevel@tonic-gate for (;;) {
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Get the keyword.
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate status = sup_gettoken(token);
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate * If we hit the end of the data file, we're done.
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate if (status == SUP_EOF)
1840Sstevel@tonic-gate break;
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * If the line starts with some key character, it's an error.
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate if (status != SUP_STRING) {
1890Sstevel@tonic-gate (void) fprintf(stderr,
190*9320SPavel.Potoplyak@Sun.COM gettext("Expecting keyword, found '%s'"),
1910Sstevel@tonic-gate token);
1920Sstevel@tonic-gate (void) fprintf(stderr,
193*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
1940Sstevel@tonic-gate continue;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Clean up the token and see which keyword it is. Call
1980Sstevel@tonic-gate * the appropriate routine to process the rest of the line.
1990Sstevel@tonic-gate */
2000Sstevel@tonic-gate clean_token(cleaned, token);
2010Sstevel@tonic-gate if (strcmp(cleaned, "slices") == 0) {
2020Sstevel@tonic-gate ret_val = sup_setpart(vt);
2030Sstevel@tonic-gate (void) fclose(data_file);
2040Sstevel@tonic-gate return (ret_val);
2050Sstevel@tonic-gate } else {
2060Sstevel@tonic-gate (void) fprintf(stderr, gettext("Unknown keyword '%s'"),
2070Sstevel@tonic-gate cleaned);
2080Sstevel@tonic-gate (void) fprintf(stderr,
209*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
2100Sstevel@tonic-gate (void) fclose(data_file);
2110Sstevel@tonic-gate return (-1);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * Close the data file.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate (void) fclose(data_file);
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate (void) fprintf(stderr,
220*9320SPavel.Potoplyak@Sun.COM gettext("Unexpected end of file (line no %d)\n"), data_lineno);
2210Sstevel@tonic-gate return (-1);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate static int32_t
sup_gettoken(char * buf)2250Sstevel@tonic-gate sup_gettoken(char *buf)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate * Skip end of lines and blank lines.
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate while ((last_token_type = sup_get_token(buf)) == SUP_EOL)
2310Sstevel@tonic-gate ;
2320Sstevel@tonic-gate return (last_token_type);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate static int32_t
sup_get_token(char * buf)2360Sstevel@tonic-gate sup_get_token(char *buf)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate char *ptr = buf;
2390Sstevel@tonic-gate int32_t c, quoted = 0;
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate * Was an end of file detected last try?
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (feof(data_file)) {
2460Sstevel@tonic-gate return (SUP_EOF);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * Zero out the returned token buffer
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate bzero(buf, TOKEN_SIZE + 1);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * Strip off leading white-space.
2570Sstevel@tonic-gate */
2580Sstevel@tonic-gate while (isspace(c = sup_inputchar()))
2590Sstevel@tonic-gate ;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * Only white spaces and then end of file?
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate if (feof(data_file)) {
2660Sstevel@tonic-gate return (SUP_EOF);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate * Read in characters until we hit unquoted white-space.
2710Sstevel@tonic-gate */
2720Sstevel@tonic-gate for (; !isspace(c) || quoted; c = sup_inputchar()) {
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate * If we hit eof, check if we have anything in buffer.
2760Sstevel@tonic-gate * if we have, return STRING, next time we will return EOF
2770Sstevel@tonic-gate * else, return EOF here...should not happen.
2780Sstevel@tonic-gate */
2790Sstevel@tonic-gate if (feof(data_file)) {
2800Sstevel@tonic-gate if (ptr - buf > 0) {
2810Sstevel@tonic-gate return (SUP_STRING);
2820Sstevel@tonic-gate } else {
2830Sstevel@tonic-gate return (SUP_EOF);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * If we hit a double quote, change the state of quoting.
2890Sstevel@tonic-gate */
2900Sstevel@tonic-gate if (c == '"') {
2910Sstevel@tonic-gate quoted = !quoted;
2920Sstevel@tonic-gate continue;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate * If we hit a newline, that delimits a token.
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate if (c == '\n')
2980Sstevel@tonic-gate break;
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * If we hit any nonquoted special delimiters, that delimits
3010Sstevel@tonic-gate * a token.
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate if (!quoted && (c == '=' || c == ',' || c == ':' ||
304*9320SPavel.Potoplyak@Sun.COM c == '#' || c == '|' || c == '&' || c == '~'))
3050Sstevel@tonic-gate break;
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate * Store the character if there's room left.
3080Sstevel@tonic-gate */
3090Sstevel@tonic-gate if (ptr - buf < TOKEN_SIZE)
3100Sstevel@tonic-gate *ptr++ = (char)c;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate * If we stored characters in the buffer, then we inputted a string.
3140Sstevel@tonic-gate * Push the delimiter back into the pipe and return the string.
3150Sstevel@tonic-gate */
3160Sstevel@tonic-gate if (ptr - buf > 0) {
3170Sstevel@tonic-gate sup_pushchar(c);
3180Sstevel@tonic-gate return (SUP_STRING);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * We didn't input a string, so we must have inputted a known delimiter.
3220Sstevel@tonic-gate * store the delimiter in the buffer, so it will get returned.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate buf[0] = c;
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate * Switch on the delimiter. Return the appropriate value for each one.
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate switch (c) {
3290Sstevel@tonic-gate case '=':
3300Sstevel@tonic-gate return (SUP_EQL);
3310Sstevel@tonic-gate case ':':
3320Sstevel@tonic-gate return (SUP_COLON);
3330Sstevel@tonic-gate case ',':
3340Sstevel@tonic-gate return (SUP_COMMA);
3350Sstevel@tonic-gate case '\n':
3360Sstevel@tonic-gate return (SUP_EOL);
3370Sstevel@tonic-gate case '|':
3380Sstevel@tonic-gate return (SUP_OR);
3390Sstevel@tonic-gate case '&':
3400Sstevel@tonic-gate return (SUP_AND);
3410Sstevel@tonic-gate case '~':
3420Sstevel@tonic-gate return (SUP_TILDE);
3430Sstevel@tonic-gate case '#':
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate * For comments, we flush out the rest of the line and return
3460Sstevel@tonic-gate * an eol.
3470Sstevel@tonic-gate */
3480Sstevel@tonic-gate while ((c = sup_inputchar()) != '\n' && !feof(data_file))
3490Sstevel@tonic-gate ;
3500Sstevel@tonic-gate if (feof(data_file))
3510Sstevel@tonic-gate return (SUP_EOF);
3520Sstevel@tonic-gate else
3530Sstevel@tonic-gate return (SUP_EOL);
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate * Shouldn't ever get here.
3560Sstevel@tonic-gate */
3570Sstevel@tonic-gate default:
3580Sstevel@tonic-gate return (SUP_STRING);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate static int32_t
sup_inputchar()3620Sstevel@tonic-gate sup_inputchar()
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate int32_t c;
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate * Input the character.
3680Sstevel@tonic-gate */
3690Sstevel@tonic-gate c = getc(data_file);
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate * If it's not a backslash, return it.
3720Sstevel@tonic-gate */
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate /*
3750Sstevel@tonic-gate * It was a backslash. Get the next character.
3760Sstevel@tonic-gate */
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (c == '\\')
3790Sstevel@tonic-gate c = getc(data_file);
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate /*
3820Sstevel@tonic-gate * If it was a newline, update the line counter and get the next
3830Sstevel@tonic-gate * character.
3840Sstevel@tonic-gate */
3850Sstevel@tonic-gate if (c == '\n') {
3860Sstevel@tonic-gate data_lineno++;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * Return the character.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate return (c);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate static void
sup_pushchar(int32_t c)3950Sstevel@tonic-gate sup_pushchar(int32_t c)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate (void) ungetc(c, data_file);
3990Sstevel@tonic-gate if (c == '\n')
4000Sstevel@tonic-gate data_lineno--;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate static void
clean_token(char * cleantoken,char * token)4040Sstevel@tonic-gate clean_token(char *cleantoken, char *token)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate char *ptr;
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate * Strip off leading white-space.
4100Sstevel@tonic-gate */
4110Sstevel@tonic-gate for (ptr = token; isspace(*ptr) && (ptr <=
412*9320SPavel.Potoplyak@Sun.COM (token + strlen(token) - 1)); ptr++)
413*9320SPavel.Potoplyak@Sun.COM ;
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate * Copy it into the clean buffer.
4170Sstevel@tonic-gate */
4180Sstevel@tonic-gate (void) strcpy(cleantoken, ptr);
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Strip off trailing white-space.
4210Sstevel@tonic-gate */
4220Sstevel@tonic-gate for (ptr = cleantoken + strlen(cleantoken) - 1;
423*9320SPavel.Potoplyak@Sun.COM isspace(*ptr) && (ptr >= cleantoken); ptr--) {
4240Sstevel@tonic-gate *ptr = '\0';
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate static int32_t
sup_setpart(struct extvtoc * vt)4297563SPrasad.Singamsetty@Sun.COM sup_setpart(struct extvtoc *vt)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate TOKEN token, cleaned, ident;
4327563SPrasad.Singamsetty@Sun.COM int32_t i, index, status;
4337563SPrasad.Singamsetty@Sun.COM uint64_t val1, val2;
4340Sstevel@tonic-gate ushort_t vtoc_tag = 0xFFFF;
4350Sstevel@tonic-gate ushort_t vtoc_flag = 0xFFFF;
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * Pull in some grammar.
4390Sstevel@tonic-gate */
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate status = sup_gettoken(token);
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (status != SUP_COLON) {
4440Sstevel@tonic-gate (void) fprintf(stderr,
445*9320SPavel.Potoplyak@Sun.COM gettext("Expecting ':', found '%s'"), token);
4460Sstevel@tonic-gate (void) fprintf(stderr,
447*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
4480Sstevel@tonic-gate return (-1);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate for (;;) {
4520Sstevel@tonic-gate status = sup_gettoken(token);
4530Sstevel@tonic-gate if (status != SUP_STRING) {
4540Sstevel@tonic-gate (void) fprintf(stderr,
455*9320SPavel.Potoplyak@Sun.COM gettext("Expecting string, found '%s'"), token);
4560Sstevel@tonic-gate (void) fprintf(stderr,
457*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
4580Sstevel@tonic-gate return (-1);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate clean_token(ident, token);
4610Sstevel@tonic-gate /*
4620Sstevel@tonic-gate * Here's the index of the partition we're dealing with
4630Sstevel@tonic-gate */
4640Sstevel@tonic-gate index = (int32_t)my_atoll(ident);
465*9320SPavel.Potoplyak@Sun.COM if ((index < 0) || (index >= NDKMAP)) {
4660Sstevel@tonic-gate (void) fprintf(stderr,
467*9320SPavel.Potoplyak@Sun.COM gettext("Unknown partition %d"), index);
4680Sstevel@tonic-gate (void) fprintf(stderr,
469*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
4700Sstevel@tonic-gate return (-1);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate /*
4730Sstevel@tonic-gate * Check for floppy and PCMCIA_MEM cards.
4740Sstevel@tonic-gate * for floppy, the partition no. can be 0 1 2.
4750Sstevel@tonic-gate * for PCMCIA, the partition no. can be 2
4760Sstevel@tonic-gate */
4770Sstevel@tonic-gate if (med_info.sm_media_type == SM_FLOPPY) {
4780Sstevel@tonic-gate if ((index < 0) || (index > 2)) {
4790Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4800Sstevel@tonic-gate "Floppy can have partitions 0 1 and 2\n"));
4810Sstevel@tonic-gate return (-1);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate if (med_info.sm_media_type == SM_PCMCIA_MEM) {
4850Sstevel@tonic-gate if (index != 2) {
4860Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4870Sstevel@tonic-gate "PCMCIA Memory cards can have partition 2 only.\n"));
4880Sstevel@tonic-gate return (-1);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate DPRINTF1("\n Partition %d: ", index);
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate status = sup_gettoken(token);
4950Sstevel@tonic-gate if (status != SUP_EQL) {
4960Sstevel@tonic-gate (void) fprintf(stderr,
497*9320SPavel.Potoplyak@Sun.COM gettext("Expecting '=', found '%s'"), token);
4980Sstevel@tonic-gate (void) fprintf(stderr,
499*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5000Sstevel@tonic-gate return (-1);
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate status = sup_gettoken(token);
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate * If we hit a key character, it's an error.
5080Sstevel@tonic-gate */
5090Sstevel@tonic-gate if (status != SUP_STRING) {
5100Sstevel@tonic-gate (void) fprintf(stderr,
511*9320SPavel.Potoplyak@Sun.COM gettext("Expecting value, found '%s'"), token);
5120Sstevel@tonic-gate (void) fprintf(stderr,
513*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5140Sstevel@tonic-gate return (-1);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate clean_token(cleaned, token);
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate * <tag> may be one of: boot, root, swap, etc.
5190Sstevel@tonic-gate * <flag> consists of two characters:
5200Sstevel@tonic-gate * W (writable) or R (read-only)
5210Sstevel@tonic-gate * M (mountable) or U (unmountable)
5220Sstevel@tonic-gate *
5230Sstevel@tonic-gate * Start with the defaults assigned above:
5240Sstevel@tonic-gate */
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate * All other attributes have a pair of numeric values.
5280Sstevel@tonic-gate * Convert the first value to a number. This value
5290Sstevel@tonic-gate * is the starting cylinder number of the partition.
5300Sstevel@tonic-gate */
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate /* Check for valid partition, e.g. > 8 or 16 */
5330Sstevel@tonic-gate val1 = str2sector(cleaned);
5340Sstevel@tonic-gate if (val1 == -1) {
5350Sstevel@tonic-gate (void) fprintf(stderr,
536*9320SPavel.Potoplyak@Sun.COM gettext("Invalid partition beggining %s \n"),
5370Sstevel@tonic-gate cleaned);
5380Sstevel@tonic-gate (void) fprintf(stderr,
539*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate DPRINTF1(" begins %s", cleaned);
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate * Pull in some grammar.
5450Sstevel@tonic-gate */
5460Sstevel@tonic-gate status = sup_gettoken(token);
5470Sstevel@tonic-gate if (status != SUP_COMMA) {
5480Sstevel@tonic-gate (void) fprintf(stderr,
549*9320SPavel.Potoplyak@Sun.COM gettext("Expecting ', ', found '%s'"), token);
5500Sstevel@tonic-gate (void) fprintf(stderr,
551*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5520Sstevel@tonic-gate return (-1);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate /*
5550Sstevel@tonic-gate * Pull in the second value.
5560Sstevel@tonic-gate */
5570Sstevel@tonic-gate status = sup_gettoken(token);
5580Sstevel@tonic-gate if (status != SUP_STRING) {
5590Sstevel@tonic-gate (void) fprintf(stderr,
560*9320SPavel.Potoplyak@Sun.COM gettext("Expecting value, found '%s'"), token);
5610Sstevel@tonic-gate (void) fprintf(stderr,
562*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5630Sstevel@tonic-gate return (-1);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate clean_token(cleaned, token);
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate val2 = str2sector(cleaned);
5680Sstevel@tonic-gate if (val2 == -1) {
5690Sstevel@tonic-gate (void) fprintf(stderr,
570*9320SPavel.Potoplyak@Sun.COM gettext("Invalid partition size %s \n"),
5710Sstevel@tonic-gate cleaned);
5720Sstevel@tonic-gate (void) fprintf(stderr,
573*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate DPRINTF1(" ends %s ", cleaned);
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate * Pull in some grammar.
5790Sstevel@tonic-gate */
5800Sstevel@tonic-gate status = sup_gettoken(token);
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate if (status == SUP_COMMA) {
5830Sstevel@tonic-gate /* tags and flags */
5840Sstevel@tonic-gate status = sup_gettoken(token);
5850Sstevel@tonic-gate if (status != SUP_STRING) {
5860Sstevel@tonic-gate (void) fprintf(stderr,
587*9320SPavel.Potoplyak@Sun.COM gettext("Expecting value, found '%s'"),
5880Sstevel@tonic-gate token);
5890Sstevel@tonic-gate (void) fprintf(stderr,
590*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
5910Sstevel@tonic-gate return (-1);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate clean_token(cleaned, token);
5940Sstevel@tonic-gate if (find_value(pflag_choices, cleaned, &i) == 1) {
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate * Found valid tag. Use it and advance parser
5970Sstevel@tonic-gate */
5980Sstevel@tonic-gate DPRINTF1(" flag = %s", cleaned);
5990Sstevel@tonic-gate vtoc_flag = (ushort_t)i;
6000Sstevel@tonic-gate status = sup_gettoken(token);
6010Sstevel@tonic-gate } else if (find_value(ptag_choices, cleaned, &i) == 1) {
6020Sstevel@tonic-gate DPRINTF1(" tag = %s", cleaned);
6030Sstevel@tonic-gate vtoc_tag = (ushort_t)i;
6040Sstevel@tonic-gate status = sup_gettoken(token);
6050Sstevel@tonic-gate if (status == SUP_COMMA) {
6060Sstevel@tonic-gate (void) fprintf(stderr,
607*9320SPavel.Potoplyak@Sun.COM gettext("Expecting : got %s\n"),
6080Sstevel@tonic-gate token);
6090Sstevel@tonic-gate (void) fprintf(stderr,
610*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"),
6110Sstevel@tonic-gate data_lineno);
6120Sstevel@tonic-gate return (-1);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate } else {
6150Sstevel@tonic-gate (void) fprintf(stderr,
616*9320SPavel.Potoplyak@Sun.COM gettext("Invalid flag or tag\n"));
6170Sstevel@tonic-gate (void) fprintf(stderr,
618*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
6190Sstevel@tonic-gate return (-1);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate if (status == SUP_COMMA) {
6240Sstevel@tonic-gate /* Can be tag only */
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate status = sup_gettoken(token);
6270Sstevel@tonic-gate if (status != SUP_STRING) {
6280Sstevel@tonic-gate (void) fprintf(stderr,
629*9320SPavel.Potoplyak@Sun.COM gettext("Expecting value"
630*9320SPavel.Potoplyak@Sun.COM ", found '%s'"),
631*9320SPavel.Potoplyak@Sun.COM token);
6320Sstevel@tonic-gate (void) fprintf(stderr,
633*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"),
6340Sstevel@tonic-gate data_lineno);
6350Sstevel@tonic-gate return (-1);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate clean_token(cleaned, token);
6390Sstevel@tonic-gate if (find_value(ptag_choices,
6400Sstevel@tonic-gate cleaned, &i) == 1) {
6410Sstevel@tonic-gate DPRINTF1(" tag = %s", cleaned);
6420Sstevel@tonic-gate vtoc_tag = (ushort_t)i;
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate status = sup_gettoken(token);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate /*
6490Sstevel@tonic-gate * Fill in the appropriate map entry with the values.
6500Sstevel@tonic-gate */
6510Sstevel@tonic-gate vt->v_part[index].p_start = val1;
6520Sstevel@tonic-gate vt->v_part[index].p_size = val2;
6530Sstevel@tonic-gate if (vtoc_tag != 0xFFFF) {
6540Sstevel@tonic-gate vt->v_part[index].p_tag = vtoc_tag;
6550Sstevel@tonic-gate vtoc_tag = 0xFFFF;
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate if (vtoc_flag != 0xFFFF) {
6580Sstevel@tonic-gate vt->v_part[index].p_flag = vtoc_flag;
6590Sstevel@tonic-gate vtoc_flag = 0xFFFF;
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate if (status == SUP_EOF) {
6620Sstevel@tonic-gate DPRINTF("\nEnd of file\n");
6630Sstevel@tonic-gate break;
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate if (status != SUP_COLON) {
6660Sstevel@tonic-gate (void) fprintf(stderr,
667*9320SPavel.Potoplyak@Sun.COM gettext("Expecting ':', found '%s'"), token);
6680Sstevel@tonic-gate (void) fprintf(stderr,
669*9320SPavel.Potoplyak@Sun.COM gettext("Line no %d\n"), data_lineno);
6700Sstevel@tonic-gate return (-1);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate return (0);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate static int32_t
find_value(slist_t * slist,char * match_str,int32_t * match_value)6780Sstevel@tonic-gate find_value(slist_t *slist, char *match_str, int32_t *match_value)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate int32_t i;
6810Sstevel@tonic-gate int32_t nmatches;
6820Sstevel@tonic-gate int32_t length;
6830Sstevel@tonic-gate int32_t match_length;
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate nmatches = 0;
6860Sstevel@tonic-gate length = 0;
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate match_length = strlen(match_str);
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate for (; slist->str != NULL; slist++) {
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate * See how many characters of the token match
6930Sstevel@tonic-gate */
6940Sstevel@tonic-gate i = strcnt(match_str, slist->str);
6950Sstevel@tonic-gate /*
6960Sstevel@tonic-gate * If it's not the whole token, then it's not a match.
6970Sstevel@tonic-gate */
6980Sstevel@tonic-gate if (i < match_length) {
6990Sstevel@tonic-gate continue;
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate /*
7020Sstevel@tonic-gate * If it ties with another input, remember that.
7030Sstevel@tonic-gate */
7040Sstevel@tonic-gate if (i == length)
7050Sstevel@tonic-gate nmatches++;
7060Sstevel@tonic-gate /*
7070Sstevel@tonic-gate * If it matches the most so far, record that.
7080Sstevel@tonic-gate */
7090Sstevel@tonic-gate if (i > length) {
7100Sstevel@tonic-gate *match_value = slist->value;
7110Sstevel@tonic-gate nmatches = 1;
7120Sstevel@tonic-gate length = i;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate return (nmatches);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate static int32_t
strcnt(char * s1,char * s2)7200Sstevel@tonic-gate strcnt(char *s1, char *s2)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate int32_t i = 0;
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate while ((*s1 != '\0') && (*s1++ == *s2++))
7250Sstevel@tonic-gate i++;
7260Sstevel@tonic-gate return (i);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate
7297563SPrasad.Singamsetty@Sun.COM static uint64_t
str2sector(char * str)7300Sstevel@tonic-gate str2sector(char *str)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate int32_t mul_factor = 1;
7330Sstevel@tonic-gate char *s1, *s2, *base;
7347563SPrasad.Singamsetty@Sun.COM uint64_t num_sectors;
7357563SPrasad.Singamsetty@Sun.COM uint64_t size;
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate base = s2 = (char *)malloc(strlen(str) + 1);
7380Sstevel@tonic-gate if (s2 == NULL) {
7390Sstevel@tonic-gate PERROR("Malloc failed");
7400Sstevel@tonic-gate return (-1);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate *s2 = '\0';
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate s1 = str;
7470Sstevel@tonic-gate while (*s1) {
7480Sstevel@tonic-gate if ((*s1 != 'x') && ((*s1 < 'A') || (*s1 > 'F')) &&
749*9320SPavel.Potoplyak@Sun.COM ((*s1 < 'a') || (*s1 > 'f')) && ((*s1 < '0') ||
750*9320SPavel.Potoplyak@Sun.COM (*s1 > '9'))) {
7510Sstevel@tonic-gate if (*s1 == 'G') {
7520Sstevel@tonic-gate mul_factor = 1024*1024*1024;
7530Sstevel@tonic-gate s1++;
7540Sstevel@tonic-gate } else if (*s1 == 'M') {
7550Sstevel@tonic-gate mul_factor = 1024*1024;
7560Sstevel@tonic-gate s1++;
7570Sstevel@tonic-gate } else if (*s1 == 'K') {
7580Sstevel@tonic-gate mul_factor = 1024;
7590Sstevel@tonic-gate s1++;
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate if ((*s1 != 'B') || (*(++s1) != NULL)) {
7620Sstevel@tonic-gate (void) fprintf(stderr,
763*9320SPavel.Potoplyak@Sun.COM gettext("Extra chars at the end\n"));
7640Sstevel@tonic-gate free(base);
7650Sstevel@tonic-gate return (-1);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate break;
7680Sstevel@tonic-gate } else {
7690Sstevel@tonic-gate *s2++ = *s1++;
7700Sstevel@tonic-gate *s2 = '\0';
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate *s2 = NULL;
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate size = my_atoll(base);
7760Sstevel@tonic-gate if ((!mul_factor) || (size == -1)) {
7770Sstevel@tonic-gate free(base);
7780Sstevel@tonic-gate return (-1);
7790Sstevel@tonic-gate }
7807563SPrasad.Singamsetty@Sun.COM num_sectors = size * (uint64_t)mul_factor /512;
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate free(base);
7830Sstevel@tonic-gate return (num_sectors);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate int32_t
valid_slice_file(smedia_handle_t handle,int32_t fd,char * file_name,struct extvtoc * vt)7880Sstevel@tonic-gate valid_slice_file(smedia_handle_t handle, int32_t fd, char *file_name,
7897563SPrasad.Singamsetty@Sun.COM struct extvtoc *vt)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate struct stat status;
7920Sstevel@tonic-gate int32_t ret_val;
7930Sstevel@tonic-gate if (stat(file_name, &status)) {
7940Sstevel@tonic-gate PERROR(file_name);
7950Sstevel@tonic-gate return (-1);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate (void) memset(vt, 0, sizeof (*vt));
7980Sstevel@tonic-gate /* Set default tag and flag */
7990Sstevel@tonic-gate #ifdef sparc
8000Sstevel@tonic-gate vt->v_part[0].p_tag = V_ROOT;
8010Sstevel@tonic-gate vt->v_part[1].p_tag = V_SWAP;
8020Sstevel@tonic-gate vt->v_part[2].p_tag = V_BACKUP;
8030Sstevel@tonic-gate vt->v_part[6].p_tag = V_USR;
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate vt->v_part[1].p_flag = V_UNMNT; /* Unmountable */
8060Sstevel@tonic-gate vt->v_part[2].p_flag = V_UNMNT; /* Unmountable */
8070Sstevel@tonic-gate #endif
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate ret_val = sup_prxfile(file_name, vt);
8100Sstevel@tonic-gate if (ret_val < 0)
8110Sstevel@tonic-gate return (-1);
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate #ifdef DEBUG
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate int32_t i;
8160Sstevel@tonic-gate for (i = 0; i < 8; i++) {
8170Sstevel@tonic-gate DPRINTF1("\npart %d\n", i);
8187563SPrasad.Singamsetty@Sun.COM DPRINTF1("\t start %llu", vt->v_part[i].p_start);
8197563SPrasad.Singamsetty@Sun.COM DPRINTF1("\t size %llu ", vt->v_part[i].p_size);
8200Sstevel@tonic-gate DPRINTF1("\t tag %d", vt->v_part[i].p_tag);
8210Sstevel@tonic-gate DPRINTF1("\t flag %d", vt->v_part[i].p_flag);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate #endif /* DEBUG */
8250Sstevel@tonic-gate if (check_vtoc_sanity(handle, fd, vt) < 0) {
8260Sstevel@tonic-gate return (-1);
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate #ifdef DEBUG
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate int32_t i;
8310Sstevel@tonic-gate for (i = 0; i < 8; i++) {
8320Sstevel@tonic-gate DPRINTF1("\npart %d\n", i);
8337563SPrasad.Singamsetty@Sun.COM DPRINTF1("\t start %llu", vt->v_part[i].p_start);
8347563SPrasad.Singamsetty@Sun.COM DPRINTF1("\t size %llu ", vt->v_part[i].p_size);
8350Sstevel@tonic-gate DPRINTF1("\t tag %d", vt->v_part[i].p_tag);
8360Sstevel@tonic-gate DPRINTF1("\t flag %d", vt->v_part[i].p_flag);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate #endif /* DEBUG */
8400Sstevel@tonic-gate return (0);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate
8437563SPrasad.Singamsetty@Sun.COM #define SWAP(a, b) {diskaddr_t tmp; tmp = (a); (a) = (b); (b) = tmp; }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate * On x86 Solaris, the partitioning is done in two levels, fdisk and Solaris
8470Sstevel@tonic-gate * VTOC. Where as, on sparc solaris, it is only VTOC. On floppy and PCMCIA
8480Sstevel@tonic-gate * also it is assumed to be only VTOC, no fdisk.
8490Sstevel@tonic-gate *
8500Sstevel@tonic-gate * On sparc, the back up slice can cover the whole medium. But on x86
8510Sstevel@tonic-gate * (SCSI/ATAPI disks), the backup slice can cover the solaris partition
8520Sstevel@tonic-gate * in fdisk table.
8530Sstevel@tonic-gate * Following table describes how is it handled
8540Sstevel@tonic-gate * SPARC:
8550Sstevel@tonic-gate * SCSI/ATAPI, floppy, pcmcia : don't check for fdisk.
8560Sstevel@tonic-gate * DKIOCGGEOM is sufficient.
8570Sstevel@tonic-gate * x86 : floppy, pcmcia : Don't check for fdisk. DKIOCGGEOM is sufficient.
8580Sstevel@tonic-gate * SCSI/ATAPI : Check for fdisk.
8590Sstevel@tonic-gate * if not present, assume that the solaris
8600Sstevel@tonic-gate * partition covers 100% of the medium
8610Sstevel@tonic-gate * (minus one cylinder).
8620Sstevel@tonic-gate *
8630Sstevel@tonic-gate * if present :
8640Sstevel@tonic-gate * check for active solaris partition.
8650Sstevel@tonic-gate * if not found, take the first solaris
8660Sstevel@tonic-gate * partition.
8670Sstevel@tonic-gate * If there are no solaris partitions, its an error, stop.
8680Sstevel@tonic-gate */
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate static int32_t
check_vtoc_sanity(smedia_handle_t handle,int32_t fd,struct extvtoc * vt)8717563SPrasad.Singamsetty@Sun.COM check_vtoc_sanity(smedia_handle_t handle, int32_t fd, struct extvtoc *vt)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate int32_t i, j;
8750Sstevel@tonic-gate struct dk_geom dkg;
8760Sstevel@tonic-gate int32_t num_backup = 0;
8777563SPrasad.Singamsetty@Sun.COM diskaddr_t backup_size = 0;
8780Sstevel@tonic-gate struct part_struct {
8797563SPrasad.Singamsetty@Sun.COM diskaddr_t start;
8807563SPrasad.Singamsetty@Sun.COM diskaddr_t end;
8810Sstevel@tonic-gate int32_t num;
8820Sstevel@tonic-gate } part[NDKMAP];
8837563SPrasad.Singamsetty@Sun.COM diskaddr_t min_val;
8847563SPrasad.Singamsetty@Sun.COM int32_t min_slice, num_slices;
8857563SPrasad.Singamsetty@Sun.COM diskaddr_t media_size;
8867563SPrasad.Singamsetty@Sun.COM uint32_t cyl_size;
8870Sstevel@tonic-gate int sparc_style = 0; /* sparc_style handling ? */
8880Sstevel@tonic-gate struct fdisk_info fdisk;
8890Sstevel@tonic-gate int sol_part;
8900Sstevel@tonic-gate int total_parts = 0;
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate #ifdef sparc
8930Sstevel@tonic-gate sparc_style = 1;
8940Sstevel@tonic-gate #endif /* sparc */
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate if ((med_info.sm_media_type == SM_FLOPPY) ||
8970Sstevel@tonic-gate (med_info.sm_media_type == SM_PCMCIA_MEM) ||
8980Sstevel@tonic-gate (med_info.sm_media_type == SM_PCMCIA_ATA) ||
8990Sstevel@tonic-gate (med_info.sm_media_type == SM_SCSI_FLOPPY)) {
9000Sstevel@tonic-gate sparc_style = 1;
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate if (sparc_style) {
9040Sstevel@tonic-gate DPRINTF("sparc style true\n");
9050Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) {
9060Sstevel@tonic-gate PERROR("DKIOCGGEOM Failed");
9070Sstevel@tonic-gate return (-1);
9080Sstevel@tonic-gate }
9097563SPrasad.Singamsetty@Sun.COM media_size = (diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead *
9107563SPrasad.Singamsetty@Sun.COM dkg.dkg_nsect;
9110Sstevel@tonic-gate cyl_size = dkg.dkg_nhead * dkg.dkg_nsect;
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate if (!sparc_style) {
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate * Try to get the fdisk information if available.
9170Sstevel@tonic-gate */
9180Sstevel@tonic-gate if (get_fdisk(handle, fd, 0, &fdisk) >= 0) {
9190Sstevel@tonic-gate /* fdisk table on disk */
9200Sstevel@tonic-gate sol_part = 0xFF;
9210Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) {
9220Sstevel@tonic-gate if (fdisk.part[i].systid == SUNIXOS ||
9230Sstevel@tonic-gate fdisk.part[i].systid == SUNIXOS2) {
9240Sstevel@tonic-gate if (sol_part == 0xFF)
9250Sstevel@tonic-gate sol_part = i;
9260Sstevel@tonic-gate total_parts++;
9270Sstevel@tonic-gate if (fdisk.part[i].bootid == ACTIVE)
9280Sstevel@tonic-gate sol_part = i;
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate if (sol_part == 0xFF) {
9320Sstevel@tonic-gate /* No Solaris partition */
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate (void) fprintf(stderr, gettext("No FDISK \
9350Sstevel@tonic-gate Solaris partition found!\n"));
9360Sstevel@tonic-gate return (-1);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate if (total_parts > 1)
9390Sstevel@tonic-gate (void) fprintf(stderr, gettext("Multiple FDISK \
9400Sstevel@tonic-gate Solaris partitions found.\n"));
9417563SPrasad.Singamsetty@Sun.COM media_size = (diskaddr_t)fdisk.part[sol_part].numsect;
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate DPRINTF1("sol_part %d\n", sol_part);
9447563SPrasad.Singamsetty@Sun.COM DPRINTF1("media_size %llu\n", media_size);
9450Sstevel@tonic-gate } else {
9460Sstevel@tonic-gate DPRINTF("Didn't get fdisk\n");
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate * No fdisk partition available. Assume a 100% Solaris.
9490Sstevel@tonic-gate * partition.
9500Sstevel@tonic-gate * Try getting disk geometry.
9510Sstevel@tonic-gate */
9520Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &dkg) < 0)
9530Sstevel@tonic-gate if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) {
9540Sstevel@tonic-gate DPRINTF("DKIOCG_PHYGEOM ioctl failed");
9550Sstevel@tonic-gate return (-1);
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate /* On x86 platform 1 cylinder is used for fdisk table */
9580Sstevel@tonic-gate dkg.dkg_ncyl = dkg.dkg_ncyl - 1;
9597563SPrasad.Singamsetty@Sun.COM media_size = (diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead *
9600Sstevel@tonic-gate dkg.dkg_nsect;
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate #ifdef DEBUG
9650Sstevel@tonic-gate DPRINTF1("Ncyl %d\n", dkg.dkg_ncyl);
9660Sstevel@tonic-gate DPRINTF1("nhead %d\n", dkg.dkg_nhead);
9670Sstevel@tonic-gate DPRINTF1("nsect %d\n", dkg.dkg_nsect);
9680Sstevel@tonic-gate #endif /* DEBUG */
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate if (media_size == 0) {
9717563SPrasad.Singamsetty@Sun.COM media_size = (uint32_t)med_info.sm_capacity;
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate (void) memset(&part, 0, sizeof (part));
9750Sstevel@tonic-gate for (i = 0, j = 0; i < NDKMAP; i++) {
9760Sstevel@tonic-gate if (vt->v_part[i].p_tag == V_BACKUP) {
9770Sstevel@tonic-gate if (vt->v_part[i].p_start != 0) {
9780Sstevel@tonic-gate (void) fprintf(stderr,
9790Sstevel@tonic-gate gettext(
9800Sstevel@tonic-gate "Backup slice should start at sector 0\n"));
9810Sstevel@tonic-gate return (-1);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate backup_size = vt->v_part[i].p_size;
9840Sstevel@tonic-gate num_backup++;
9850Sstevel@tonic-gate continue;
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate if (vt->v_part[i].p_size) {
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate if (sparc_style) {
9900Sstevel@tonic-gate if (vt->v_part[i].p_start % cyl_size) {
9910Sstevel@tonic-gate (void) fprintf(stderr,
9920Sstevel@tonic-gate gettext(
9930Sstevel@tonic-gate "Slice %d does not start on cylinder boundary\n"), i);
9940Sstevel@tonic-gate (void) fprintf(stderr,
9950Sstevel@tonic-gate gettext(
9960Sstevel@tonic-gate "Cylinder size %d 512 byte sectors\n"), cyl_size);
9970Sstevel@tonic-gate return (-1);
9980Sstevel@tonic-gate }
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate part[j].start = vt->v_part[i].p_start;
10010Sstevel@tonic-gate part[j].end = vt->v_part[i].p_start +
10020Sstevel@tonic-gate vt->v_part[i].p_size -1;
10030Sstevel@tonic-gate part[j].num = i;
10040Sstevel@tonic-gate j++;
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate if (num_backup > 1) {
10080Sstevel@tonic-gate (void) fprintf(stderr,
1009*9320SPavel.Potoplyak@Sun.COM gettext("Maximum one backup slice is allowed\n"));
10100Sstevel@tonic-gate (void) smedia_release_handle(handle);
10110Sstevel@tonic-gate (void) close(fd);
10120Sstevel@tonic-gate exit(1);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate num_slices = j;
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate for (i = 0; i < num_slices; i++) {
10170Sstevel@tonic-gate min_val = part[i].start;
10180Sstevel@tonic-gate min_slice = i;
10190Sstevel@tonic-gate for (j = i+1; j < num_slices; j++) {
10200Sstevel@tonic-gate if (part[j].start < min_val) {
10210Sstevel@tonic-gate min_val = part[j].start;
10220Sstevel@tonic-gate min_slice = j;
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate if (min_slice != i) {
10260Sstevel@tonic-gate SWAP(part[i].start, part[min_slice].start)
10270Sstevel@tonic-gate SWAP(part[i].end, part[min_slice].end)
10280Sstevel@tonic-gate SWAP(part[i].num, part[min_slice].num)
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate #ifdef DEBUG
10330Sstevel@tonic-gate for (i = 0; i < num_slices; i++) {
10347563SPrasad.Singamsetty@Sun.COM DPRINTF4("\n %d (%d) : %llu, %llu", i, part[i].num,
10357563SPrasad.Singamsetty@Sun.COM part[i].start, part[i].end);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate #endif /* DEBUG */
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate if (backup_size > media_size) {
10400Sstevel@tonic-gate if (sparc_style) {
10410Sstevel@tonic-gate (void) fprintf(stderr,
10420Sstevel@tonic-gate gettext(
10430Sstevel@tonic-gate "Backup slice extends beyond size of media\n"));
10440Sstevel@tonic-gate (void) fprintf(stderr,
10457563SPrasad.Singamsetty@Sun.COM gettext("media size : %llu sectors \n"),
1046*9320SPavel.Potoplyak@Sun.COM media_size);
10470Sstevel@tonic-gate } else {
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate (void) fprintf(stderr,
10500Sstevel@tonic-gate gettext("Backup slice extends beyond size of FDISK \
10510Sstevel@tonic-gate Solaris partition\n"));
10520Sstevel@tonic-gate (void) fprintf(stderr,
10530Sstevel@tonic-gate gettext(
10547563SPrasad.Singamsetty@Sun.COM "FDISK Solaris partition size : %llu sectors \n"),
10550Sstevel@tonic-gate media_size);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate return (-1);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate * If we have only backup slice return success here.
10620Sstevel@tonic-gate */
10630Sstevel@tonic-gate if (num_slices == 0)
10640Sstevel@tonic-gate return (0);
10650Sstevel@tonic-gate
10660Sstevel@tonic-gate if (backup_size) {
10670Sstevel@tonic-gate if (part[num_slices - 1].end > backup_size) {
10680Sstevel@tonic-gate (void) fprintf(stderr,
10690Sstevel@tonic-gate gettext("Slice %d extends beyond backup slice.\n"),
10700Sstevel@tonic-gate part[num_slices -1].num);
10710Sstevel@tonic-gate return (-1);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate } else {
10740Sstevel@tonic-gate if (part[num_slices - 1].end > media_size) {
10750Sstevel@tonic-gate if (sparc_style) {
10760Sstevel@tonic-gate (void) fprintf(stderr,
10770Sstevel@tonic-gate gettext(
10780Sstevel@tonic-gate "Slice %d extends beyond media size\n"),
10790Sstevel@tonic-gate part[num_slices -1].num);
10800Sstevel@tonic-gate (void) fprintf(stderr,
10817563SPrasad.Singamsetty@Sun.COM gettext("media size : %llu sectors \n"),
10820Sstevel@tonic-gate media_size);
10830Sstevel@tonic-gate } else {
10840Sstevel@tonic-gate (void) fprintf(stderr,
1085*9320SPavel.Potoplyak@Sun.COM gettext("Slice %d extends beyond FDISK"
1086*9320SPavel.Potoplyak@Sun.COM " Solaris partition size\n"),
1087*9320SPavel.Potoplyak@Sun.COM part[num_slices -1].num);
10887563SPrasad.Singamsetty@Sun.COM (void) fprintf(stderr, gettext(
10897563SPrasad.Singamsetty@Sun.COM "FDISK Solaris partition size : %llu "
10907563SPrasad.Singamsetty@Sun.COM "sectors \n"), media_size);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate return (-1);
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate for (i = 0; i < num_slices; i++) {
10990Sstevel@tonic-gate if (i == 0)
11000Sstevel@tonic-gate continue;
11010Sstevel@tonic-gate if (part[i].start <= part[i-1].end) {
11020Sstevel@tonic-gate (void) fprintf(stderr,
1103*9320SPavel.Potoplyak@Sun.COM gettext("Overlap between slices %d and %d\n"),
1104*9320SPavel.Potoplyak@Sun.COM part[i-1].num, part[i].num);
11050Sstevel@tonic-gate (void) smedia_release_handle(handle);
11060Sstevel@tonic-gate (void) close(fd);
11070Sstevel@tonic-gate exit(1);
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate return (0);
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate static int32_t
get_fdisk(smedia_handle_t handle,int32_t fd,int32_t offset,struct fdisk_info * fdisk)11160Sstevel@tonic-gate get_fdisk(smedia_handle_t handle, int32_t fd, int32_t offset,
11170Sstevel@tonic-gate struct fdisk_info *fdisk)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate struct mboot *boot_sec;
11200Sstevel@tonic-gate struct ipart *part;
11210Sstevel@tonic-gate char *buf;
11220Sstevel@tonic-gate int32_t i, ret;
11230Sstevel@tonic-gate int save_errno;
11240Sstevel@tonic-gate
11250Sstevel@tonic-gate /* Read the master boot program */
11260Sstevel@tonic-gate
11270Sstevel@tonic-gate buf = (char *)malloc(med_info.sm_blocksize);
11280Sstevel@tonic-gate if (buf == NULL) {
11290Sstevel@tonic-gate PERROR("malloc failed");
11300Sstevel@tonic-gate exit(1);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate errno = 0;
11330Sstevel@tonic-gate ret = ioctl(fd, DKIOCGMBOOT, buf);
11340Sstevel@tonic-gate if (ret < 0) {
11350Sstevel@tonic-gate if (errno != ENOTTY) {
11360Sstevel@tonic-gate PERROR("DKIOCGMBOOT ioctl failed");
11370Sstevel@tonic-gate return (-1);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate
11402160Szk194757 /* Turn on privileges. */
11412160Szk194757 (void) __priv_bracket(PRIV_ON);
11420Sstevel@tonic-gate
11437563SPrasad.Singamsetty@Sun.COM ret = smedia_raw_read(handle,
11447563SPrasad.Singamsetty@Sun.COM (diskaddr_t)offset/med_info.sm_blocksize,
11457563SPrasad.Singamsetty@Sun.COM buf, med_info.sm_blocksize);
11460Sstevel@tonic-gate
11472160Szk194757 /* Turn off privileges. */
11482160Szk194757 (void) __priv_bracket(PRIV_OFF);
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate save_errno = errno;
11510Sstevel@tonic-gate errno = save_errno;
11520Sstevel@tonic-gate if (ret != med_info.sm_blocksize) {
11530Sstevel@tonic-gate if (errno == ENOTSUP) {
11540Sstevel@tonic-gate errno = 0;
11550Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET)) {
1156*9320SPavel.Potoplyak@Sun.COM PERROR("Seek failed:");
1157*9320SPavel.Potoplyak@Sun.COM free(buf);
1158*9320SPavel.Potoplyak@Sun.COM return (-1);
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate
11612160Szk194757 /* Turn on privileges. */
11622160Szk194757 (void) __priv_bracket(PRIV_ON);
11630Sstevel@tonic-gate
11640Sstevel@tonic-gate ret = read(fd, buf, sizeof (struct mboot));
11650Sstevel@tonic-gate
11662160Szk194757 /* Turn off privileges. */
11672160Szk194757 (void) __priv_bracket(PRIV_OFF);
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate if (ret != sizeof (struct mboot)) {
1170*9320SPavel.Potoplyak@Sun.COM PERROR("Could not read "
1171*9320SPavel.Potoplyak@Sun.COM "master boot record");
1172*9320SPavel.Potoplyak@Sun.COM free(buf);
1173*9320SPavel.Potoplyak@Sun.COM return (-1);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate } else {
11760Sstevel@tonic-gate PERROR("Could not read master boot record");
11770Sstevel@tonic-gate free(buf);
11780Sstevel@tonic-gate return (-1);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
11830Sstevel@tonic-gate boot_sec = (struct mboot *)buf;
11840Sstevel@tonic-gate
11850Sstevel@tonic-gate /* Is this really a master boot record? */
11860Sstevel@tonic-gate if (les(boot_sec->signature) != MBB_MAGIC) {
11870Sstevel@tonic-gate DPRINTF("fdisk: Invalid master boot file \n");
11880Sstevel@tonic-gate DPRINTF2("Bad magic number: is %x, should be %x.\n",
1189*9320SPavel.Potoplyak@Sun.COM les(boot_sec->signature), MBB_MAGIC);
11900Sstevel@tonic-gate free(buf);
11910Sstevel@tonic-gate return (-1);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) {
11950Sstevel@tonic-gate DPRINTF1("part %d\n", i);
11960Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
11970Sstevel@tonic-gate part = (struct ipart *)&boot_sec->parts[i *
11980Sstevel@tonic-gate sizeof (struct ipart)];
11990Sstevel@tonic-gate fdisk->part[i].bootid = part->bootid;
12000Sstevel@tonic-gate if (part->bootid && (part->bootid != ACTIVE)) {
12010Sstevel@tonic-gate /* Hmmm...not a valid fdisk! */
12020Sstevel@tonic-gate return (-1);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate fdisk->part[i].systid = part->systid;
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate /* To avoid the misalign access in sparc */
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate fdisk->part[i].relsect = lel(GET_32(&(part->relsect)));
12090Sstevel@tonic-gate fdisk->part[i].numsect = lel(GET_32(&(part->numsect)));
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate DPRINTF1("\tboot id 0x%x\n", part->bootid);
12120Sstevel@tonic-gate DPRINTF1("\tsystem id 0x%x\n", part->systid);
12130Sstevel@tonic-gate DPRINTF1("\trel sector 0x%x\n", fdisk->part[i].relsect);
12140Sstevel@tonic-gate DPRINTF1("\tnum sector 0x%x\n", fdisk->part[i].numsect);
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate free(buf);
12170Sstevel@tonic-gate return (0);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate /*
12220Sstevel@tonic-gate * wrrite_defualt_label(int32_t fd)
12230Sstevel@tonic-gate * fd = file descriptor for the device.
12240Sstevel@tonic-gate *
12250Sstevel@tonic-gate * For sparc solaris
12260Sstevel@tonic-gate * Create a vtoc partition with
12270Sstevel@tonic-gate * slice 0 = slice 2 = medium capacity.
12280Sstevel@tonic-gate * The cyl, head, sect (CHS) values are computed as done in sd
12290Sstevel@tonic-gate * capacity <= 1GB,
12300Sstevel@tonic-gate * nhead = 64, nsect = 32
12310Sstevel@tonic-gate * capacity > 1gb,
12320Sstevel@tonic-gate * nhead = 255, nsect = 63
12330Sstevel@tonic-gate *
12340Sstevel@tonic-gate * For x86 solaris
12350Sstevel@tonic-gate * Create a fdisk partition,
12360Sstevel@tonic-gate * partition 0 covers the full medium, the partition
12370Sstevel@tonic-gate * type is set to Solaris.
12380Sstevel@tonic-gate * Then create solaris vtoc. The algorithm is same as sparc solaris.
12390Sstevel@tonic-gate * But the capacity is reduced by 1 cyl, to leave space for fdisk table.
12400Sstevel@tonic-gate */
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate #ifdef sparc
12430Sstevel@tonic-gate /*ARGSUSED*/
12440Sstevel@tonic-gate void
write_default_label(smedia_handle_t handle,int32_t fd)12450Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate
12487563SPrasad.Singamsetty@Sun.COM struct extvtoc v_toc;
12497563SPrasad.Singamsetty@Sun.COM uint32_t nhead, numcyl, nsect;
12507563SPrasad.Singamsetty@Sun.COM diskaddr_t capacity;
12510Sstevel@tonic-gate int32_t ret;
12520Sstevel@tonic-gate char asciilabel[LEN_DKL_ASCII];
12530Sstevel@tonic-gate char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0";
12547563SPrasad.Singamsetty@Sun.COM uint32_t acyl = 2;
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate DPRINTF("Writing default vtoc\n");
12580Sstevel@tonic-gate (void) memset(&v_toc, 0, sizeof (v_toc));
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate v_toc.v_nparts = V_NUMPAR;
12620Sstevel@tonic-gate v_toc.v_sanity = VTOC_SANE;
12630Sstevel@tonic-gate v_toc.v_version = V_VERSION;
12640Sstevel@tonic-gate v_toc.v_sectorsz = DEV_BSIZE;
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate * For the head, cyl and number of sector per track,
12680Sstevel@tonic-gate * if the capacity <= 1GB, head = 64, sect = 32.
12690Sstevel@tonic-gate * else head = 255, sect 63
12700Sstevel@tonic-gate * NOTE: the capacity should be equal to C*H*S values.
12710Sstevel@tonic-gate * This will cause some truncation of size due to
12720Sstevel@tonic-gate * round off errors.
12730Sstevel@tonic-gate */
12747563SPrasad.Singamsetty@Sun.COM if ((uint32_t)med_info.sm_capacity <= 0x200000) {
12750Sstevel@tonic-gate nhead = 64;
12760Sstevel@tonic-gate nsect = 32;
12770Sstevel@tonic-gate } else {
12780Sstevel@tonic-gate nhead = 255;
12790Sstevel@tonic-gate nsect = 63;
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate
12827563SPrasad.Singamsetty@Sun.COM numcyl = (uint32_t)med_info.sm_capacity / (nhead * nsect);
12837563SPrasad.Singamsetty@Sun.COM capacity = (diskaddr_t)nhead * nsect * numcyl;
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate v_toc.v_part[0].p_start = 0;
12860Sstevel@tonic-gate v_toc.v_part[0].p_size = capacity;
12870Sstevel@tonic-gate v_toc.v_part[0].p_tag = V_ROOT;
12880Sstevel@tonic-gate v_toc.v_part[0].p_flag = 0; /* Mountable */
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate v_toc.v_part[2].p_start = 0;
12910Sstevel@tonic-gate v_toc.v_part[2].p_size = capacity;
12920Sstevel@tonic-gate v_toc.v_part[2].p_tag = V_BACKUP;
12930Sstevel@tonic-gate v_toc.v_part[2].p_flag = V_UNMNT;
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate /* Create asciilabel for compatibility with format utility */
12960Sstevel@tonic-gate (void) snprintf(asciilabel, sizeof (asciilabel),
12970Sstevel@tonic-gate "%s cyl %d alt %d hd %d sec %d",
12980Sstevel@tonic-gate asciilabel2, numcyl, acyl, nhead, nsect);
12990Sstevel@tonic-gate (void) memcpy(v_toc.v_asciilabel, asciilabel,
1300*9320SPavel.Potoplyak@Sun.COM LEN_DKL_ASCII);
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate errno = 0;
13030Sstevel@tonic-gate
13042160Szk194757 /* Turn on privileges. */
13052160Szk194757 (void) __priv_bracket(PRIV_ON);
13060Sstevel@tonic-gate
13077563SPrasad.Singamsetty@Sun.COM ret = write_extvtoc(fd, &v_toc);
13080Sstevel@tonic-gate
13092160Szk194757 /* Turn off privileges. */
13102160Szk194757 (void) __priv_bracket(PRIV_OFF);
13110Sstevel@tonic-gate
13120Sstevel@tonic-gate if (ret < 0) {
13130Sstevel@tonic-gate PERROR("write VTOC failed");
13140Sstevel@tonic-gate DPRINTF1("Errno = %d\n", errno);
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate
13180Sstevel@tonic-gate #else /* !sparc */
13190Sstevel@tonic-gate #ifdef i386
13200Sstevel@tonic-gate
13210Sstevel@tonic-gate void
write_default_label(smedia_handle_t handle,int32_t fd)13220Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate int32_t i, ret;
13260Sstevel@tonic-gate struct dk_geom dkg;
13277563SPrasad.Singamsetty@Sun.COM struct extvtoc v_toc;
13280Sstevel@tonic-gate int tmp_fd;
13290Sstevel@tonic-gate char *fdisk_buf;
13300Sstevel@tonic-gate struct mboot boot_code; /* Buffer for master boot record */
13310Sstevel@tonic-gate struct ipart parts[FD_NUMPART];
13327563SPrasad.Singamsetty@Sun.COM uint32_t numcyl, nhead, nsect;
13337563SPrasad.Singamsetty@Sun.COM uint32_t unixend;
13347563SPrasad.Singamsetty@Sun.COM uint32_t blocksize;
13357563SPrasad.Singamsetty@Sun.COM diskaddr_t capacity;
13360Sstevel@tonic-gate int save_errno;
13370Sstevel@tonic-gate size_t bytes_written;
13380Sstevel@tonic-gate char asciilabel[LEN_DKL_ASCII];
13390Sstevel@tonic-gate char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0";
13407563SPrasad.Singamsetty@Sun.COM uint32_t acyl = 2;
13410Sstevel@tonic-gate
13420Sstevel@tonic-gate DPRINTF("Writing default fdisk table and vtoc\n");
13430Sstevel@tonic-gate (void) memset(&v_toc, 0, sizeof (v_toc));
13440Sstevel@tonic-gate /*
13450Sstevel@tonic-gate * Try getting disk geometry.
13460Sstevel@tonic-gate */
13470Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &dkg) < 0)
13480Sstevel@tonic-gate if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) {
13490Sstevel@tonic-gate
13500Sstevel@tonic-gate DPRINTF("DKIOCG_PHYGEOM ioctl failed");
13510Sstevel@tonic-gate return;
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate tmp_fd = open("/usr/lib/fs/ufs/mboot", O_RDONLY);
13550Sstevel@tonic-gate if (tmp_fd <= 0) {
13560Sstevel@tonic-gate return;
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate
13590Sstevel@tonic-gate if (read(tmp_fd, &boot_code, sizeof (struct mboot))
13600Sstevel@tonic-gate != sizeof (struct mboot)) {
13610Sstevel@tonic-gate (void) close(tmp_fd);
13620Sstevel@tonic-gate return;
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate blocksize = med_info.sm_blocksize;
13660Sstevel@tonic-gate fdisk_buf = (char *)malloc(blocksize);
13670Sstevel@tonic-gate if (fdisk_buf == NULL) {
13680Sstevel@tonic-gate DPRINTF("malloc for fdisk_buf failed\n");
13690Sstevel@tonic-gate return;
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate (void) memset(&parts, 0, sizeof (parts));
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) {
13750Sstevel@tonic-gate parts[i].systid = UNUSED;
13760Sstevel@tonic-gate parts[i].numsect = lel(UNUSED);
13770Sstevel@tonic-gate parts[i].relsect = lel(UNUSED);
13780Sstevel@tonic-gate parts[i].bootid = 0;
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate numcyl = dkg.dkg_ncyl;
13820Sstevel@tonic-gate nhead = dkg.dkg_nhead;
13830Sstevel@tonic-gate nsect = dkg.dkg_nsect;
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate parts[0].bootid = ACTIVE;
13860Sstevel@tonic-gate parts[0].begsect = 1;
13870Sstevel@tonic-gate
13880Sstevel@tonic-gate unixend = numcyl;
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate parts[0].relsect = lel(nhead * nsect);
13917563SPrasad.Singamsetty@Sun.COM parts[0].numsect = lel(((diskaddr_t)numcyl * nhead * nsect));
13920Sstevel@tonic-gate parts[0].systid = SUNIXOS2; /* Solaris */
13930Sstevel@tonic-gate parts[0].beghead = 0;
13940Sstevel@tonic-gate parts[0].begcyl = 1;
13950Sstevel@tonic-gate parts[0].endhead = nhead - 1;
13960Sstevel@tonic-gate parts[0].endsect = (nsect & 0x3f) |
13970Sstevel@tonic-gate (char)((unixend >> 2) & 0x00c0);
13980Sstevel@tonic-gate parts[0].endcyl = (char)(unixend & 0x00ff);
13990Sstevel@tonic-gate
14000Sstevel@tonic-gate (void) memcpy(&(boot_code.parts), parts, sizeof (parts));
14010Sstevel@tonic-gate (void) memcpy(fdisk_buf, &boot_code, sizeof (boot_code));
14020Sstevel@tonic-gate
14032160Szk194757 /* Turn on privileges. */
14042160Szk194757 (void) __priv_bracket(PRIV_ON);
14050Sstevel@tonic-gate
14060Sstevel@tonic-gate ret = ioctl(fd, DKIOCSMBOOT, fdisk_buf);
14070Sstevel@tonic-gate
14082160Szk194757 /* Turn off privileges. */
14092160Szk194757 (void) __priv_bracket(PRIV_OFF);
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate if (ret == -1) {
14120Sstevel@tonic-gate if (errno != ENOTTY) {
14130Sstevel@tonic-gate PERROR("DKIOCSMBOOT ioctl Failed");
14140Sstevel@tonic-gate return;
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate
14172160Szk194757 /* Turn on privileges. */
14182160Szk194757 (void) __priv_bracket(PRIV_ON);
14190Sstevel@tonic-gate
14207563SPrasad.Singamsetty@Sun.COM bytes_written = smedia_raw_write(handle, (diskaddr_t)0,
14217563SPrasad.Singamsetty@Sun.COM fdisk_buf, blocksize);
14220Sstevel@tonic-gate
14232160Szk194757 /* Turn off privileges. */
14242160Szk194757 (void) __priv_bracket(PRIV_OFF);
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate save_errno = errno;
14270Sstevel@tonic-gate errno = save_errno;
14280Sstevel@tonic-gate if (bytes_written != blocksize) {
14290Sstevel@tonic-gate if (errno == ENOTSUP) {
14300Sstevel@tonic-gate
14312160Szk194757 /* Turn on privileges. */
1432*9320SPavel.Potoplyak@Sun.COM (void) __priv_bracket(PRIV_ON);
14330Sstevel@tonic-gate
1434*9320SPavel.Potoplyak@Sun.COM ret = write(fd, fdisk_buf, blocksize);
14350Sstevel@tonic-gate
14362160Szk194757 /* Turn off privileges. */
1437*9320SPavel.Potoplyak@Sun.COM (void) __priv_bracket(PRIV_OFF);
14380Sstevel@tonic-gate
1439*9320SPavel.Potoplyak@Sun.COM if (ret != blocksize) {
14400Sstevel@tonic-gate return;
1441*9320SPavel.Potoplyak@Sun.COM }
14420Sstevel@tonic-gate } else {
14430Sstevel@tonic-gate return;
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate }
14477563SPrasad.Singamsetty@Sun.COM capacity = (diskaddr_t)(numcyl - 1) * nhead * nsect;
14480Sstevel@tonic-gate
14490Sstevel@tonic-gate v_toc.v_nparts = V_NUMPAR;
14500Sstevel@tonic-gate v_toc.v_sanity = VTOC_SANE;
14510Sstevel@tonic-gate v_toc.v_version = V_VERSION;
14520Sstevel@tonic-gate v_toc.v_sectorsz = DEV_BSIZE;
14530Sstevel@tonic-gate
14540Sstevel@tonic-gate v_toc.v_part[0].p_start = 0;
14550Sstevel@tonic-gate v_toc.v_part[0].p_size = capacity;
14560Sstevel@tonic-gate v_toc.v_part[0].p_tag = V_ROOT;
14570Sstevel@tonic-gate v_toc.v_part[0].p_flag = 0; /* Mountable */
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate v_toc.v_part[2].p_start = 0;
14600Sstevel@tonic-gate v_toc.v_part[2].p_size = capacity;
14610Sstevel@tonic-gate v_toc.v_part[2].p_tag = V_BACKUP;
14620Sstevel@tonic-gate v_toc.v_part[2].p_flag = V_UNMNT;
14630Sstevel@tonic-gate
14640Sstevel@tonic-gate /* Create asciilabel for compatibility with format utility */
14650Sstevel@tonic-gate (void) snprintf(asciilabel, sizeof (asciilabel),
14660Sstevel@tonic-gate "%s cyl %d alt %d hd %d sec %d",
14670Sstevel@tonic-gate asciilabel2, numcyl, acyl, nhead, nsect);
14680Sstevel@tonic-gate (void) memcpy(v_toc.v_asciilabel, asciilabel,
1469*9320SPavel.Potoplyak@Sun.COM LEN_DKL_ASCII);
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate errno = 0;
14720Sstevel@tonic-gate
14730Sstevel@tonic-gate
14742160Szk194757 /* Turn on privileges. */
14752160Szk194757 (void) __priv_bracket(PRIV_ON);
14760Sstevel@tonic-gate
14777563SPrasad.Singamsetty@Sun.COM ret = write_extvtoc(fd, &v_toc);
14780Sstevel@tonic-gate
14792160Szk194757 /* Turn off privileges. */
14802160Szk194757 (void) __priv_bracket(PRIV_OFF);
14810Sstevel@tonic-gate
14820Sstevel@tonic-gate if (ret < 0) {
14830Sstevel@tonic-gate PERROR("write VTOC failed");
14840Sstevel@tonic-gate DPRINTF1("Errno = %d\n", errno);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate
14880Sstevel@tonic-gate #else /* !i386 */
14890Sstevel@tonic-gate
14900Sstevel@tonic-gate #error One of sparc or i386 must be defined!
14910Sstevel@tonic-gate
14920Sstevel@tonic-gate #endif /* i386 */
14930Sstevel@tonic-gate #endif /* sparc */
14940Sstevel@tonic-gate
14950Sstevel@tonic-gate /*
14960Sstevel@tonic-gate * void overwrite_metadata(int32_t fd, smedia_handle_t handle)
14970Sstevel@tonic-gate *
14980Sstevel@tonic-gate * purpose : quick format does not erase the data on Iomega
14990Sstevel@tonic-gate * zip/jaz media. So, the meta data on the disk should be erased.
15000Sstevel@tonic-gate *
15010Sstevel@tonic-gate * If there is a valid fdisk table,
15020Sstevel@tonic-gate * erase first 64K of each partition.
15030Sstevel@tonic-gate * If there is a valid vtoc,
15040Sstevel@tonic-gate * erase first 64k of each slice.
15050Sstevel@tonic-gate * Then erase the 0th sector (the home for vtoc and fdisk) of the disk.
15060Sstevel@tonic-gate * Note that teh vtoc on x86 resides in one of the fdisk partition.
15070Sstevel@tonic-gate * So delay the erasing of the solaris partition until the vtoc is read.
15080Sstevel@tonic-gate */
15090Sstevel@tonic-gate
15100Sstevel@tonic-gate void
overwrite_metadata(int32_t fd,smedia_handle_t handle)15110Sstevel@tonic-gate overwrite_metadata(int32_t fd, smedia_handle_t handle)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate
15140Sstevel@tonic-gate struct fdisk_info fdisk;
15157563SPrasad.Singamsetty@Sun.COM diskaddr_t sol_offset = 0;
15160Sstevel@tonic-gate int i, ret;
15177563SPrasad.Singamsetty@Sun.COM struct extvtoc t_vtoc;
15180Sstevel@tonic-gate #ifdef i386
15197563SPrasad.Singamsetty@Sun.COM diskaddr_t sol_size = 0;
15200Sstevel@tonic-gate int32_t active = 0;
15210Sstevel@tonic-gate #endif /* i386 */
15220Sstevel@tonic-gate
15230Sstevel@tonic-gate /* Get fdisk info. */
15240Sstevel@tonic-gate if (get_fdisk(handle, fd, 0, &fdisk) >= 0) {
15250Sstevel@tonic-gate /* Got a valid fdisk */
15260Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) {
15270Sstevel@tonic-gate
15280Sstevel@tonic-gate if (fdisk.part[i].numsect == 0)
15290Sstevel@tonic-gate continue;
15300Sstevel@tonic-gate if ((fdisk.part[i].systid == UNUSED) ||
1531*9320SPavel.Potoplyak@Sun.COM (fdisk.part[i].systid == 0))
15320Sstevel@tonic-gate continue;
15330Sstevel@tonic-gate #ifdef i386
15340Sstevel@tonic-gate if (fdisk.part[i].systid == SUNIXOS ||
15350Sstevel@tonic-gate fdisk.part[i].systid == SUNIXOS2) {
15360Sstevel@tonic-gate if (!sol_offset) {
15370Sstevel@tonic-gate sol_offset = fdisk.part[i].relsect;
15380Sstevel@tonic-gate sol_size = fdisk.part[i].numsect;
15390Sstevel@tonic-gate if (fdisk.part[i].bootid == ACTIVE)
15400Sstevel@tonic-gate active = 1;
15410Sstevel@tonic-gate continue;
15420Sstevel@tonic-gate } else if ((fdisk.part[i].bootid == ACTIVE) &&
15430Sstevel@tonic-gate (!active)) {
15440Sstevel@tonic-gate erase(handle, sol_offset, sol_size);
15450Sstevel@tonic-gate sol_offset = fdisk.part[i].relsect;
15460Sstevel@tonic-gate sol_size = fdisk.part[i].numsect;
15470Sstevel@tonic-gate active = 1;
15480Sstevel@tonic-gate continue;
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate #endif /* i386 */
15527563SPrasad.Singamsetty@Sun.COM erase(handle, (diskaddr_t)fdisk.part[i].relsect,
15537563SPrasad.Singamsetty@Sun.COM (diskaddr_t)fdisk.part[i].numsect);
15540Sstevel@tonic-gate }
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate (void) memset(&t_vtoc, 0, sizeof (t_vtoc));
15580Sstevel@tonic-gate
15590Sstevel@tonic-gate if (sol_offset) {
15600Sstevel@tonic-gate /* fdisk x86 Solaris partition */
15610Sstevel@tonic-gate /* VTOC location in solaris partition is DK_LABEL_LOC */
15620Sstevel@tonic-gate
15632160Szk194757 /* Turn on privileges. */
15642160Szk194757 (void) __priv_bracket(PRIV_ON);
15650Sstevel@tonic-gate
15667563SPrasad.Singamsetty@Sun.COM ret = read_extvtoc(fd, &t_vtoc);
15670Sstevel@tonic-gate
15682160Szk194757 /* Turn off privileges. */
15692161Szk194757 (void) __priv_bracket(PRIV_OFF);
15700Sstevel@tonic-gate
15710Sstevel@tonic-gate if (ret < 0) {
15720Sstevel@tonic-gate /* No valid vtoc, erase fdisk table. */
15737563SPrasad.Singamsetty@Sun.COM erase(handle, (diskaddr_t)0, (diskaddr_t)1);
15740Sstevel@tonic-gate return;
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate } else {
15770Sstevel@tonic-gate /* Sparc Solaris or x86 solaris with faked fdisk */
15780Sstevel@tonic-gate
15792160Szk194757 /* Turn on privileges */
15802160Szk194757 (void) __priv_bracket(PRIV_ON);
15810Sstevel@tonic-gate
15827563SPrasad.Singamsetty@Sun.COM ret = read_extvtoc(fd, &t_vtoc);
15830Sstevel@tonic-gate
15842160Szk194757 /* Turn off privileges. */
15852160Szk194757 (void) __priv_bracket(PRIV_OFF);
15860Sstevel@tonic-gate
15870Sstevel@tonic-gate if (ret < 0) {
15880Sstevel@tonic-gate /* No valid vtoc, erase from 0th sector */
15897563SPrasad.Singamsetty@Sun.COM erase(handle, (diskaddr_t)0,
15907563SPrasad.Singamsetty@Sun.COM (uint32_t)med_info.sm_capacity);
15910Sstevel@tonic-gate return;
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) {
15960Sstevel@tonic-gate if (t_vtoc.v_part[i].p_size != 0) {
15970Sstevel@tonic-gate erase(handle, sol_offset + t_vtoc.v_part[i].p_start,
1598*9320SPavel.Potoplyak@Sun.COM t_vtoc.v_part[i].p_size);
15990Sstevel@tonic-gate /*
16000Sstevel@tonic-gate * To make the udfs not recognise the partition we will
16010Sstevel@tonic-gate * erase sectors 256, (p_size-256) and psize.
16020Sstevel@tonic-gate */
16030Sstevel@tonic-gate erase(handle,
16047563SPrasad.Singamsetty@Sun.COM sol_offset + t_vtoc.v_part[i].p_start + 256,
16057563SPrasad.Singamsetty@Sun.COM (diskaddr_t)1);
16060Sstevel@tonic-gate erase(handle,
16077563SPrasad.Singamsetty@Sun.COM (sol_offset + t_vtoc.v_part[i].p_start +
16087563SPrasad.Singamsetty@Sun.COM t_vtoc.v_part[i].p_size - 256),
16097563SPrasad.Singamsetty@Sun.COM (diskaddr_t)1);
16100Sstevel@tonic-gate erase(handle,
16117563SPrasad.Singamsetty@Sun.COM (sol_offset + t_vtoc.v_part[i].p_start +
16127563SPrasad.Singamsetty@Sun.COM t_vtoc.v_part[i].p_size - 1),
16137563SPrasad.Singamsetty@Sun.COM (diskaddr_t)1);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate
16170Sstevel@tonic-gate /*
16180Sstevel@tonic-gate * If x86 fdisk solaris partition, erase the vtoc also.
16190Sstevel@tonic-gate * for sparc, the erasing 0the sector erases vtoc.
16200Sstevel@tonic-gate */
16210Sstevel@tonic-gate if (sol_offset) {
16227563SPrasad.Singamsetty@Sun.COM erase(handle, sol_offset, (diskaddr_t)DK_LABEL_LOC + 2);
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate
16250Sstevel@tonic-gate /*
16260Sstevel@tonic-gate * erase the 0th sector, it is not guaranteed to be
16270Sstevel@tonic-gate * erased in the above sequence.
16280Sstevel@tonic-gate */
16290Sstevel@tonic-gate
16307563SPrasad.Singamsetty@Sun.COM erase(handle, (diskaddr_t)0, (diskaddr_t)1);
16310Sstevel@tonic-gate }
16320Sstevel@tonic-gate
16330Sstevel@tonic-gate /*
16340Sstevel@tonic-gate * void erase(smedia_handle_t handle, uint32_t offset, uint32_t size)
16350Sstevel@tonic-gate *
16360Sstevel@tonic-gate * Initialize the media with '0' from offset 'offset' upto 'size'
16370Sstevel@tonic-gate * or 128 blocks(64k), whichever is smaller.
16380Sstevel@tonic-gate */
16390Sstevel@tonic-gate
16400Sstevel@tonic-gate static void
erase(smedia_handle_t handle,diskaddr_t offset,diskaddr_t size)16417563SPrasad.Singamsetty@Sun.COM erase(smedia_handle_t handle, diskaddr_t offset, diskaddr_t size)
16420Sstevel@tonic-gate {
16430Sstevel@tonic-gate char *buf;
16447563SPrasad.Singamsetty@Sun.COM diskaddr_t nblocks = size;
16450Sstevel@tonic-gate int32_t ret;
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate nblocks = (nblocks < 128) ? nblocks : 128;
16490Sstevel@tonic-gate buf = (char *)malloc(nblocks * med_info.sm_blocksize);
16500Sstevel@tonic-gate if (buf == NULL) {
16510Sstevel@tonic-gate PERROR("malloc failed");
16520Sstevel@tonic-gate return;
16530Sstevel@tonic-gate }
16547563SPrasad.Singamsetty@Sun.COM (void) memset(buf, 0, (size_t)nblocks * med_info.sm_blocksize);
16550Sstevel@tonic-gate
16562160Szk194757 /* Turn on privileges. */
16572160Szk194757 (void) __priv_bracket(PRIV_ON);
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate ret = smedia_raw_write(handle, offset, buf,
16607563SPrasad.Singamsetty@Sun.COM (size_t)nblocks * med_info.sm_blocksize);
16610Sstevel@tonic-gate
16622160Szk194757 /* Turn off privileges. */
16632160Szk194757 (void) __priv_bracket(PRIV_OFF);
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate if (ret != (nblocks * med_info.sm_blocksize))
16660Sstevel@tonic-gate PERROR("error in writing\n");
16670Sstevel@tonic-gate
16680Sstevel@tonic-gate free(buf);
16690Sstevel@tonic-gate
16700Sstevel@tonic-gate }
1671