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 /* 22*342440ecSPrasad Singamsetty * Copyright 2008 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 : 287c478bd9Sstevel@tonic-gate * This file contains the functions for parsing a slice file 297c478bd9Sstevel@tonic-gate * 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 */ 130*342440ecSPrasad Singamsetty static int32_t sup_prxfile(char *file_name, struct extvtoc *vt); 131*342440ecSPrasad 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); 139*342440ecSPrasad Singamsetty static int32_t check_vtoc_sanity(smedia_handle_t, int32_t fd, 140*342440ecSPrasad Singamsetty struct extvtoc *vt); 141*342440ecSPrasad 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); 145*342440ecSPrasad Singamsetty static void erase(smedia_handle_t handle, diskaddr_t offset, diskaddr_t size); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate extern char *myname; 148*342440ecSPrasad 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 154*342440ecSPrasad 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, 1907c478bd9Sstevel@tonic-gate gettext("Expecting keyword, found '%s'"), 1917c478bd9Sstevel@tonic-gate token); 1927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1937c478bd9Sstevel@tonic-gate 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, 2097c478bd9Sstevel@tonic-gate 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, 2207c478bd9Sstevel@tonic-gate 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 == ':' || 3047c478bd9Sstevel@tonic-gate 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 <= 4127c478bd9Sstevel@tonic-gate (token + strlen(token) - 1)); ptr++); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * Copy it into the clean buffer. 4167c478bd9Sstevel@tonic-gate */ 4177c478bd9Sstevel@tonic-gate (void) strcpy(cleantoken, ptr); 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * Strip off trailing white-space. 4207c478bd9Sstevel@tonic-gate */ 4217c478bd9Sstevel@tonic-gate for (ptr = cleantoken + strlen(cleantoken) - 1; 4227c478bd9Sstevel@tonic-gate isspace(*ptr) && (ptr >= cleantoken); ptr--) { 4237c478bd9Sstevel@tonic-gate *ptr = '\0'; 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate static int32_t 428*342440ecSPrasad Singamsetty sup_setpart(struct extvtoc *vt) 4297c478bd9Sstevel@tonic-gate { 4307c478bd9Sstevel@tonic-gate TOKEN token, cleaned, ident; 431*342440ecSPrasad Singamsetty int32_t i, index, status; 432*342440ecSPrasad Singamsetty uint64_t val1, val2; 4337c478bd9Sstevel@tonic-gate ushort_t vtoc_tag = 0xFFFF; 4347c478bd9Sstevel@tonic-gate ushort_t vtoc_flag = 0xFFFF; 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate /* 4377c478bd9Sstevel@tonic-gate * Pull in some grammar. 4387c478bd9Sstevel@tonic-gate */ 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate if (status != SUP_COLON) { 4437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4447c478bd9Sstevel@tonic-gate gettext("Expecting ':', found '%s'"), token); 4457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4467c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 4477c478bd9Sstevel@tonic-gate return (-1); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate for (;;) { 4517c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 4527c478bd9Sstevel@tonic-gate if (status != SUP_STRING) { 4537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4547c478bd9Sstevel@tonic-gate gettext("Expecting string, found '%s'"), token); 4557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4567c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 4577c478bd9Sstevel@tonic-gate return (-1); 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate clean_token(ident, token); 4607c478bd9Sstevel@tonic-gate /* 4617c478bd9Sstevel@tonic-gate * Also make sure it is within the legal range of letters. 4627c478bd9Sstevel@tonic-gate */ 4637c478bd9Sstevel@tonic-gate /* 4647c478bd9Sstevel@tonic-gate * Here's the index of the partition we're dealing with 4657c478bd9Sstevel@tonic-gate */ 4667c478bd9Sstevel@tonic-gate index = (int32_t)my_atoll(ident); 4677c478bd9Sstevel@tonic-gate if ((index < 0) || (index > NDKMAP)) { 4687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4697c478bd9Sstevel@tonic-gate gettext("Unknown partition '%s'"), index); 4707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4717c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 4727c478bd9Sstevel@tonic-gate return (-1); 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate /* 4757c478bd9Sstevel@tonic-gate * Check for floppy and PCMCIA_MEM cards. 4767c478bd9Sstevel@tonic-gate * for floppy, the partition no. can be 0 1 2. 4777c478bd9Sstevel@tonic-gate * for PCMCIA, the partition no. can be 2 4787c478bd9Sstevel@tonic-gate */ 4797c478bd9Sstevel@tonic-gate if (med_info.sm_media_type == SM_FLOPPY) { 4807c478bd9Sstevel@tonic-gate if ((index < 0) || (index > 2)) { 4817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4827c478bd9Sstevel@tonic-gate "Floppy can have partitions 0 1 and 2\n")); 4837c478bd9Sstevel@tonic-gate return (-1); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate if (med_info.sm_media_type == SM_PCMCIA_MEM) { 4877c478bd9Sstevel@tonic-gate if (index != 2) { 4887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4897c478bd9Sstevel@tonic-gate "PCMCIA Memory cards can have partition 2 only.\n")); 4907c478bd9Sstevel@tonic-gate return (-1); 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate DPRINTF1("\n Partition %d: ", index); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 4977c478bd9Sstevel@tonic-gate if (status != SUP_EQL) { 4987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4997c478bd9Sstevel@tonic-gate gettext("Expecting '=', found '%s'"), token); 5007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5017c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5027c478bd9Sstevel@tonic-gate return (-1); 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 5087c478bd9Sstevel@tonic-gate /* 5097c478bd9Sstevel@tonic-gate * If we hit a key character, it's an error. 5107c478bd9Sstevel@tonic-gate */ 5117c478bd9Sstevel@tonic-gate if (status != SUP_STRING) { 5127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5137c478bd9Sstevel@tonic-gate gettext("Expecting value, found '%s'"), token); 5147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5157c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5167c478bd9Sstevel@tonic-gate return (-1); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate clean_token(cleaned, token); 5197c478bd9Sstevel@tonic-gate /* 5207c478bd9Sstevel@tonic-gate * <tag> may be one of: boot, root, swap, etc. 5217c478bd9Sstevel@tonic-gate * <flag> consists of two characters: 5227c478bd9Sstevel@tonic-gate * W (writable) or R (read-only) 5237c478bd9Sstevel@tonic-gate * M (mountable) or U (unmountable) 5247c478bd9Sstevel@tonic-gate * 5257c478bd9Sstevel@tonic-gate * Start with the defaults assigned above: 5267c478bd9Sstevel@tonic-gate */ 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate /* 5297c478bd9Sstevel@tonic-gate * All other attributes have a pair of numeric values. 5307c478bd9Sstevel@tonic-gate * Convert the first value to a number. This value 5317c478bd9Sstevel@tonic-gate * is the starting cylinder number of the partition. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate /* Check for valid partition, e.g. > 8 or 16 */ 5357c478bd9Sstevel@tonic-gate val1 = str2sector(cleaned); 5367c478bd9Sstevel@tonic-gate if (val1 == -1) { 5377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5387c478bd9Sstevel@tonic-gate gettext("Invalid partition beggining %s \n"), 5397c478bd9Sstevel@tonic-gate cleaned); 5407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5417c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate DPRINTF1(" begins %s", cleaned); 5457c478bd9Sstevel@tonic-gate /* 5467c478bd9Sstevel@tonic-gate * Pull in some grammar. 5477c478bd9Sstevel@tonic-gate */ 5487c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 5497c478bd9Sstevel@tonic-gate if (status != SUP_COMMA) { 5507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5517c478bd9Sstevel@tonic-gate gettext("Expecting ', ', found '%s'"), token); 5527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5537c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5547c478bd9Sstevel@tonic-gate return (-1); 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * Pull in the second value. 5587c478bd9Sstevel@tonic-gate */ 5597c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 5607c478bd9Sstevel@tonic-gate if (status != SUP_STRING) { 5617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5627c478bd9Sstevel@tonic-gate gettext("Expecting value, found '%s'"), token); 5637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5647c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5657c478bd9Sstevel@tonic-gate return (-1); 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate clean_token(cleaned, token); 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate val2 = str2sector(cleaned); 5707c478bd9Sstevel@tonic-gate if (val2 == -1) { 5717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5727c478bd9Sstevel@tonic-gate gettext("Invalid partition size %s \n"), 5737c478bd9Sstevel@tonic-gate cleaned); 5747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5757c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5767c478bd9Sstevel@tonic-gate } 5777c478bd9Sstevel@tonic-gate DPRINTF1(" ends %s ", cleaned); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate /* 5807c478bd9Sstevel@tonic-gate * Pull in some grammar. 5817c478bd9Sstevel@tonic-gate */ 5827c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate if (status == SUP_COMMA) { 5857c478bd9Sstevel@tonic-gate /* tags and flags */ 5867c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 5877c478bd9Sstevel@tonic-gate if (status != SUP_STRING) { 5887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5897c478bd9Sstevel@tonic-gate gettext("Expecting value, found '%s'"), 5907c478bd9Sstevel@tonic-gate token); 5917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5927c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 5937c478bd9Sstevel@tonic-gate return (-1); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate clean_token(cleaned, token); 5967c478bd9Sstevel@tonic-gate if (find_value(pflag_choices, cleaned, &i) == 1) { 5977c478bd9Sstevel@tonic-gate /* 5987c478bd9Sstevel@tonic-gate * Found valid tag. Use it and advance parser 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate DPRINTF1(" flag = %s", cleaned); 6017c478bd9Sstevel@tonic-gate vtoc_flag = (ushort_t)i; 6027c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 6037c478bd9Sstevel@tonic-gate } else if (find_value(ptag_choices, cleaned, &i) == 1) { 6047c478bd9Sstevel@tonic-gate DPRINTF1(" tag = %s", cleaned); 6057c478bd9Sstevel@tonic-gate vtoc_tag = (ushort_t)i; 6067c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 6077c478bd9Sstevel@tonic-gate if (status == SUP_COMMA) { 6087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6097c478bd9Sstevel@tonic-gate gettext("Expecting : got %s\n"), 6107c478bd9Sstevel@tonic-gate token); 6117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6127c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), 6137c478bd9Sstevel@tonic-gate data_lineno); 6147c478bd9Sstevel@tonic-gate return (-1); 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate } else { 6177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6187c478bd9Sstevel@tonic-gate gettext("Invalid flag or tag\n")); 6197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6207c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 6217c478bd9Sstevel@tonic-gate return (-1); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate if (status == SUP_COMMA) { 6267c478bd9Sstevel@tonic-gate /* Can be tag only */ 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 6297c478bd9Sstevel@tonic-gate if (status != SUP_STRING) { 6307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6317c478bd9Sstevel@tonic-gate gettext( 6327c478bd9Sstevel@tonic-gate "Expecting value, found '%s'"), 6337c478bd9Sstevel@tonic-gate token); 6347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6357c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), 6367c478bd9Sstevel@tonic-gate data_lineno); 6377c478bd9Sstevel@tonic-gate return (-1); 6387c478bd9Sstevel@tonic-gate } 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate clean_token(cleaned, token); 6417c478bd9Sstevel@tonic-gate if (find_value(ptag_choices, 6427c478bd9Sstevel@tonic-gate cleaned, &i) == 1) { 6437c478bd9Sstevel@tonic-gate DPRINTF1(" tag = %s", cleaned); 6447c478bd9Sstevel@tonic-gate vtoc_tag = (ushort_t)i; 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate status = sup_gettoken(token); 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate /* 6517c478bd9Sstevel@tonic-gate * Fill in the appropriate map entry with the values. 6527c478bd9Sstevel@tonic-gate */ 6537c478bd9Sstevel@tonic-gate vt->v_part[index].p_start = val1; 6547c478bd9Sstevel@tonic-gate vt->v_part[index].p_size = val2; 6557c478bd9Sstevel@tonic-gate if (vtoc_tag != 0xFFFF) { 6567c478bd9Sstevel@tonic-gate vt->v_part[index].p_tag = vtoc_tag; 6577c478bd9Sstevel@tonic-gate vtoc_tag = 0xFFFF; 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate if (vtoc_flag != 0xFFFF) { 6607c478bd9Sstevel@tonic-gate vt->v_part[index].p_flag = vtoc_flag; 6617c478bd9Sstevel@tonic-gate vtoc_flag = 0xFFFF; 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate if (status == SUP_EOF) { 6647c478bd9Sstevel@tonic-gate DPRINTF("\nEnd of file\n"); 6657c478bd9Sstevel@tonic-gate break; 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate if (status != SUP_COLON) { 6687c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6697c478bd9Sstevel@tonic-gate gettext("Expecting ':', found '%s'"), token); 6707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6717c478bd9Sstevel@tonic-gate gettext("Line no %d\n"), data_lineno); 6727c478bd9Sstevel@tonic-gate return (-1); 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate return (0); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate static int32_t 6807c478bd9Sstevel@tonic-gate find_value(slist_t *slist, char *match_str, int32_t *match_value) 6817c478bd9Sstevel@tonic-gate { 6827c478bd9Sstevel@tonic-gate int32_t i; 6837c478bd9Sstevel@tonic-gate int32_t nmatches; 6847c478bd9Sstevel@tonic-gate int32_t length; 6857c478bd9Sstevel@tonic-gate int32_t match_length; 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate nmatches = 0; 6887c478bd9Sstevel@tonic-gate length = 0; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate match_length = strlen(match_str); 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate for (; slist->str != NULL; slist++) { 6937c478bd9Sstevel@tonic-gate /* 6947c478bd9Sstevel@tonic-gate * See how many characters of the token match 6957c478bd9Sstevel@tonic-gate */ 6967c478bd9Sstevel@tonic-gate i = strcnt(match_str, slist->str); 6977c478bd9Sstevel@tonic-gate /* 6987c478bd9Sstevel@tonic-gate * If it's not the whole token, then it's not a match. 6997c478bd9Sstevel@tonic-gate */ 7007c478bd9Sstevel@tonic-gate if (i < match_length) { 7017c478bd9Sstevel@tonic-gate continue; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate /* 7047c478bd9Sstevel@tonic-gate * If it ties with another input, remember that. 7057c478bd9Sstevel@tonic-gate */ 7067c478bd9Sstevel@tonic-gate if (i == length) 7077c478bd9Sstevel@tonic-gate nmatches++; 7087c478bd9Sstevel@tonic-gate /* 7097c478bd9Sstevel@tonic-gate * If it matches the most so far, record that. 7107c478bd9Sstevel@tonic-gate */ 7117c478bd9Sstevel@tonic-gate if (i > length) { 7127c478bd9Sstevel@tonic-gate *match_value = slist->value; 7137c478bd9Sstevel@tonic-gate nmatches = 1; 7147c478bd9Sstevel@tonic-gate length = i; 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate return (nmatches); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate static int32_t 7227c478bd9Sstevel@tonic-gate strcnt(char *s1, char *s2) 7237c478bd9Sstevel@tonic-gate { 7247c478bd9Sstevel@tonic-gate int32_t i = 0; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate while ((*s1 != '\0') && (*s1++ == *s2++)) 7277c478bd9Sstevel@tonic-gate i++; 7287c478bd9Sstevel@tonic-gate return (i); 7297c478bd9Sstevel@tonic-gate } 7307c478bd9Sstevel@tonic-gate 731*342440ecSPrasad Singamsetty static uint64_t 7327c478bd9Sstevel@tonic-gate str2sector(char *str) 7337c478bd9Sstevel@tonic-gate { 7347c478bd9Sstevel@tonic-gate int32_t mul_factor = 1; 7357c478bd9Sstevel@tonic-gate char *s1, *s2, *base; 736*342440ecSPrasad Singamsetty uint64_t num_sectors; 737*342440ecSPrasad Singamsetty uint64_t size; 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate base = s2 = (char *)malloc(strlen(str) + 1); 7407c478bd9Sstevel@tonic-gate if (s2 == NULL) { 7417c478bd9Sstevel@tonic-gate PERROR("Malloc failed"); 7427c478bd9Sstevel@tonic-gate return (-1); 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate *s2 = '\0'; 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate s1 = str; 7497c478bd9Sstevel@tonic-gate while (*s1) { 7507c478bd9Sstevel@tonic-gate if ((*s1 != 'x') && ((*s1 < 'A') || (*s1 > 'F')) && 7517c478bd9Sstevel@tonic-gate ((*s1 < 'a') || (*s1 > 'f')) && ((*s1 < '0') || 7527c478bd9Sstevel@tonic-gate (*s1 > '9'))) { 7537c478bd9Sstevel@tonic-gate if (*s1 == 'G') { 7547c478bd9Sstevel@tonic-gate mul_factor = 1024*1024*1024; 7557c478bd9Sstevel@tonic-gate s1++; 7567c478bd9Sstevel@tonic-gate } else if (*s1 == 'M') { 7577c478bd9Sstevel@tonic-gate mul_factor = 1024*1024; 7587c478bd9Sstevel@tonic-gate s1++; 7597c478bd9Sstevel@tonic-gate } else if (*s1 == 'K') { 7607c478bd9Sstevel@tonic-gate mul_factor = 1024; 7617c478bd9Sstevel@tonic-gate s1++; 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate if ((*s1 != 'B') || (*(++s1) != NULL)) { 7647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7657c478bd9Sstevel@tonic-gate gettext("Extra chars at the end\n")); 7667c478bd9Sstevel@tonic-gate free(base); 7677c478bd9Sstevel@tonic-gate return (-1); 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate break; 7707c478bd9Sstevel@tonic-gate } else { 7717c478bd9Sstevel@tonic-gate *s2++ = *s1++; 7727c478bd9Sstevel@tonic-gate *s2 = '\0'; 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate *s2 = NULL; 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate size = my_atoll(base); 7787c478bd9Sstevel@tonic-gate if ((!mul_factor) || (size == -1)) { 7797c478bd9Sstevel@tonic-gate free(base); 7807c478bd9Sstevel@tonic-gate return (-1); 7817c478bd9Sstevel@tonic-gate } 782*342440ecSPrasad Singamsetty num_sectors = size * (uint64_t)mul_factor /512; 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate free(base); 7857c478bd9Sstevel@tonic-gate return (num_sectors); 7867c478bd9Sstevel@tonic-gate } 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate int32_t 7907c478bd9Sstevel@tonic-gate valid_slice_file(smedia_handle_t handle, int32_t fd, char *file_name, 791*342440ecSPrasad Singamsetty struct extvtoc *vt) 7927c478bd9Sstevel@tonic-gate { 7937c478bd9Sstevel@tonic-gate struct stat status; 7947c478bd9Sstevel@tonic-gate int32_t ret_val; 7957c478bd9Sstevel@tonic-gate if (stat(file_name, &status)) { 7967c478bd9Sstevel@tonic-gate PERROR(file_name); 7977c478bd9Sstevel@tonic-gate return (-1); 7987c478bd9Sstevel@tonic-gate } 7997c478bd9Sstevel@tonic-gate (void) memset(vt, 0, sizeof (*vt)); 8007c478bd9Sstevel@tonic-gate /* Set default tag and flag */ 8017c478bd9Sstevel@tonic-gate #ifdef sparc 8027c478bd9Sstevel@tonic-gate vt->v_part[0].p_tag = V_ROOT; 8037c478bd9Sstevel@tonic-gate vt->v_part[1].p_tag = V_SWAP; 8047c478bd9Sstevel@tonic-gate vt->v_part[2].p_tag = V_BACKUP; 8057c478bd9Sstevel@tonic-gate vt->v_part[6].p_tag = V_USR; 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate vt->v_part[1].p_flag = V_UNMNT; /* Unmountable */ 8087c478bd9Sstevel@tonic-gate vt->v_part[2].p_flag = V_UNMNT; /* Unmountable */ 8097c478bd9Sstevel@tonic-gate #endif 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate ret_val = sup_prxfile(file_name, vt); 8127c478bd9Sstevel@tonic-gate if (ret_val < 0) 8137c478bd9Sstevel@tonic-gate return (-1); 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate #ifdef DEBUG 8167c478bd9Sstevel@tonic-gate { 8177c478bd9Sstevel@tonic-gate int32_t i; 8187c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++) { 8197c478bd9Sstevel@tonic-gate DPRINTF1("\npart %d\n", i); 820*342440ecSPrasad Singamsetty DPRINTF1("\t start %llu", vt->v_part[i].p_start); 821*342440ecSPrasad Singamsetty DPRINTF1("\t size %llu ", vt->v_part[i].p_size); 8227c478bd9Sstevel@tonic-gate DPRINTF1("\t tag %d", vt->v_part[i].p_tag); 8237c478bd9Sstevel@tonic-gate DPRINTF1("\t flag %d", vt->v_part[i].p_flag); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate } 8267c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 8277c478bd9Sstevel@tonic-gate if (check_vtoc_sanity(handle, fd, vt) < 0) { 8287c478bd9Sstevel@tonic-gate return (-1); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate #ifdef DEBUG 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate int32_t i; 8337c478bd9Sstevel@tonic-gate for (i = 0; i < 8; i++) { 8347c478bd9Sstevel@tonic-gate DPRINTF1("\npart %d\n", i); 835*342440ecSPrasad Singamsetty DPRINTF1("\t start %llu", vt->v_part[i].p_start); 836*342440ecSPrasad Singamsetty DPRINTF1("\t size %llu ", vt->v_part[i].p_size); 8377c478bd9Sstevel@tonic-gate DPRINTF1("\t tag %d", vt->v_part[i].p_tag); 8387c478bd9Sstevel@tonic-gate DPRINTF1("\t flag %d", vt->v_part[i].p_flag); 8397c478bd9Sstevel@tonic-gate } 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 8427c478bd9Sstevel@tonic-gate return (0); 8437c478bd9Sstevel@tonic-gate } 8447c478bd9Sstevel@tonic-gate 845*342440ecSPrasad Singamsetty #define SWAP(a, b) {diskaddr_t tmp; tmp = (a); (a) = (b); (b) = tmp; } 8467c478bd9Sstevel@tonic-gate 8477c478bd9Sstevel@tonic-gate /* 8487c478bd9Sstevel@tonic-gate * On x86 Solaris, the partitioning is done in two levels, fdisk and Solaris 8497c478bd9Sstevel@tonic-gate * VTOC. Where as, on sparc solaris, it is only VTOC. On floppy and PCMCIA 8507c478bd9Sstevel@tonic-gate * also it is assumed to be only VTOC, no fdisk. 8517c478bd9Sstevel@tonic-gate * 8527c478bd9Sstevel@tonic-gate * On sparc, the back up slice can cover the whole medium. But on x86 8537c478bd9Sstevel@tonic-gate * (SCSI/ATAPI disks), the backup slice can cover the solaris partition 8547c478bd9Sstevel@tonic-gate * in fdisk table. 8557c478bd9Sstevel@tonic-gate * Following table describes how is it handled 8567c478bd9Sstevel@tonic-gate * SPARC: 8577c478bd9Sstevel@tonic-gate * SCSI/ATAPI, floppy, pcmcia : don't check for fdisk. 8587c478bd9Sstevel@tonic-gate * DKIOCGGEOM is sufficient. 8597c478bd9Sstevel@tonic-gate * x86 : floppy, pcmcia : Don't check for fdisk. DKIOCGGEOM is sufficient. 8607c478bd9Sstevel@tonic-gate * SCSI/ATAPI : Check for fdisk. 8617c478bd9Sstevel@tonic-gate * if not present, assume that the solaris 8627c478bd9Sstevel@tonic-gate * partition covers 100% of the medium 8637c478bd9Sstevel@tonic-gate * (minus one cylinder). 8647c478bd9Sstevel@tonic-gate * 8657c478bd9Sstevel@tonic-gate * if present : 8667c478bd9Sstevel@tonic-gate * check for active solaris partition. 8677c478bd9Sstevel@tonic-gate * if not found, take the first solaris 8687c478bd9Sstevel@tonic-gate * partition. 8697c478bd9Sstevel@tonic-gate * If there are no solaris partitions, its an error, stop. 8707c478bd9Sstevel@tonic-gate */ 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate static int32_t 873*342440ecSPrasad Singamsetty check_vtoc_sanity(smedia_handle_t handle, int32_t fd, struct extvtoc *vt) 8747c478bd9Sstevel@tonic-gate { 8757c478bd9Sstevel@tonic-gate 8767c478bd9Sstevel@tonic-gate int32_t i, j; 8777c478bd9Sstevel@tonic-gate struct dk_geom dkg; 8787c478bd9Sstevel@tonic-gate int32_t num_backup = 0; 879*342440ecSPrasad Singamsetty diskaddr_t backup_size = 0; 8807c478bd9Sstevel@tonic-gate struct part_struct { 881*342440ecSPrasad Singamsetty diskaddr_t start; 882*342440ecSPrasad Singamsetty diskaddr_t end; 8837c478bd9Sstevel@tonic-gate int32_t num; 8847c478bd9Sstevel@tonic-gate } part[NDKMAP]; 885*342440ecSPrasad Singamsetty diskaddr_t min_val; 886*342440ecSPrasad Singamsetty int32_t min_slice, num_slices; 887*342440ecSPrasad Singamsetty diskaddr_t media_size; 888*342440ecSPrasad Singamsetty uint32_t cyl_size; 8897c478bd9Sstevel@tonic-gate int sparc_style = 0; /* sparc_style handling ? */ 8907c478bd9Sstevel@tonic-gate struct fdisk_info fdisk; 8917c478bd9Sstevel@tonic-gate int sol_part; 8927c478bd9Sstevel@tonic-gate int total_parts = 0; 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate #ifdef sparc 8957c478bd9Sstevel@tonic-gate sparc_style = 1; 8967c478bd9Sstevel@tonic-gate #endif /* sparc */ 8977c478bd9Sstevel@tonic-gate 8987c478bd9Sstevel@tonic-gate if ((med_info.sm_media_type == SM_FLOPPY) || 8997c478bd9Sstevel@tonic-gate (med_info.sm_media_type == SM_PCMCIA_MEM) || 9007c478bd9Sstevel@tonic-gate (med_info.sm_media_type == SM_PCMCIA_ATA) || 9017c478bd9Sstevel@tonic-gate (med_info.sm_media_type == SM_SCSI_FLOPPY)) { 9027c478bd9Sstevel@tonic-gate sparc_style = 1; 9037c478bd9Sstevel@tonic-gate } 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate if (sparc_style) { 9067c478bd9Sstevel@tonic-gate DPRINTF("sparc style true\n"); 9077c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) { 9087c478bd9Sstevel@tonic-gate PERROR("DKIOCGGEOM Failed"); 9097c478bd9Sstevel@tonic-gate return (-1); 9107c478bd9Sstevel@tonic-gate } 911*342440ecSPrasad Singamsetty media_size = (diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead * 912*342440ecSPrasad Singamsetty dkg.dkg_nsect; 9137c478bd9Sstevel@tonic-gate cyl_size = dkg.dkg_nhead * dkg.dkg_nsect; 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate if (!sparc_style) { 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * Try to get the fdisk information if available. 9197c478bd9Sstevel@tonic-gate */ 9207c478bd9Sstevel@tonic-gate if (get_fdisk(handle, fd, 0, &fdisk) >= 0) { 9217c478bd9Sstevel@tonic-gate /* fdisk table on disk */ 9227c478bd9Sstevel@tonic-gate sol_part = 0xFF; 9237c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 9247c478bd9Sstevel@tonic-gate if (fdisk.part[i].systid == SUNIXOS || 9257c478bd9Sstevel@tonic-gate fdisk.part[i].systid == SUNIXOS2) { 9267c478bd9Sstevel@tonic-gate if (sol_part == 0xFF) 9277c478bd9Sstevel@tonic-gate sol_part = i; 9287c478bd9Sstevel@tonic-gate total_parts++; 9297c478bd9Sstevel@tonic-gate if (fdisk.part[i].bootid == ACTIVE) 9307c478bd9Sstevel@tonic-gate sol_part = i; 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate if (sol_part == 0xFF) { 9347c478bd9Sstevel@tonic-gate /* No Solaris partition */ 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("No FDISK \ 9377c478bd9Sstevel@tonic-gate Solaris partition found!\n")); 9387c478bd9Sstevel@tonic-gate return (-1); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate if (total_parts > 1) 9417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Multiple FDISK \ 9427c478bd9Sstevel@tonic-gate Solaris partitions found.\n")); 943*342440ecSPrasad Singamsetty media_size = (diskaddr_t)fdisk.part[sol_part].numsect; 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate DPRINTF1("sol_part %d\n", sol_part); 946*342440ecSPrasad Singamsetty DPRINTF1("media_size %llu\n", media_size); 9477c478bd9Sstevel@tonic-gate } else { 9487c478bd9Sstevel@tonic-gate DPRINTF("Didn't get fdisk\n"); 9497c478bd9Sstevel@tonic-gate /* 9507c478bd9Sstevel@tonic-gate * No fdisk partition available. Assume a 100% Solaris. 9517c478bd9Sstevel@tonic-gate * partition. 9527c478bd9Sstevel@tonic-gate * Try getting disk geometry. 9537c478bd9Sstevel@tonic-gate */ 9547c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) 9557c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) { 9567c478bd9Sstevel@tonic-gate DPRINTF("DKIOCG_PHYGEOM ioctl failed"); 9577c478bd9Sstevel@tonic-gate return (-1); 9587c478bd9Sstevel@tonic-gate } 9597c478bd9Sstevel@tonic-gate /* On x86 platform 1 cylinder is used for fdisk table */ 9607c478bd9Sstevel@tonic-gate dkg.dkg_ncyl = dkg.dkg_ncyl - 1; 961*342440ecSPrasad Singamsetty media_size = (diskaddr_t)dkg.dkg_ncyl * dkg.dkg_nhead * 9627c478bd9Sstevel@tonic-gate dkg.dkg_nsect; 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate #ifdef DEBUG 9677c478bd9Sstevel@tonic-gate DPRINTF1("Ncyl %d\n", dkg.dkg_ncyl); 9687c478bd9Sstevel@tonic-gate DPRINTF1("nhead %d\n", dkg.dkg_nhead); 9697c478bd9Sstevel@tonic-gate DPRINTF1("nsect %d\n", dkg.dkg_nsect); 9707c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 9717c478bd9Sstevel@tonic-gate 9727c478bd9Sstevel@tonic-gate if (media_size == 0) { 973*342440ecSPrasad Singamsetty media_size = (uint32_t)med_info.sm_capacity; 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate (void) memset(&part, 0, sizeof (part)); 9777c478bd9Sstevel@tonic-gate for (i = 0, j = 0; i < NDKMAP; i++) { 9787c478bd9Sstevel@tonic-gate if (vt->v_part[i].p_tag == V_BACKUP) { 9797c478bd9Sstevel@tonic-gate if (vt->v_part[i].p_start != 0) { 9807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9817c478bd9Sstevel@tonic-gate gettext( 9827c478bd9Sstevel@tonic-gate "Backup slice should start at sector 0\n")); 9837c478bd9Sstevel@tonic-gate return (-1); 9847c478bd9Sstevel@tonic-gate } 9857c478bd9Sstevel@tonic-gate backup_size = vt->v_part[i].p_size; 9867c478bd9Sstevel@tonic-gate num_backup++; 9877c478bd9Sstevel@tonic-gate continue; 9887c478bd9Sstevel@tonic-gate } 9897c478bd9Sstevel@tonic-gate if (vt->v_part[i].p_size) { 9907c478bd9Sstevel@tonic-gate 9917c478bd9Sstevel@tonic-gate if (sparc_style) { 9927c478bd9Sstevel@tonic-gate if (vt->v_part[i].p_start % cyl_size) { 9937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9947c478bd9Sstevel@tonic-gate gettext( 9957c478bd9Sstevel@tonic-gate "Slice %d does not start on cylinder boundary\n"), i); 9967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9977c478bd9Sstevel@tonic-gate gettext( 9987c478bd9Sstevel@tonic-gate "Cylinder size %d 512 byte sectors\n"), cyl_size); 9997c478bd9Sstevel@tonic-gate return (-1); 10007c478bd9Sstevel@tonic-gate } 10017c478bd9Sstevel@tonic-gate } 10027c478bd9Sstevel@tonic-gate part[j].start = vt->v_part[i].p_start; 10037c478bd9Sstevel@tonic-gate part[j].end = vt->v_part[i].p_start + 10047c478bd9Sstevel@tonic-gate vt->v_part[i].p_size -1; 10057c478bd9Sstevel@tonic-gate part[j].num = i; 10067c478bd9Sstevel@tonic-gate j++; 10077c478bd9Sstevel@tonic-gate } 10087c478bd9Sstevel@tonic-gate } 10097c478bd9Sstevel@tonic-gate if (num_backup > 1) { 10107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10117c478bd9Sstevel@tonic-gate gettext("Maximum one backup slice is allowed\n")); 10127c478bd9Sstevel@tonic-gate (void) smedia_release_handle(handle); 10137c478bd9Sstevel@tonic-gate (void) close(fd); 10147c478bd9Sstevel@tonic-gate exit(1); 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate num_slices = j; 10177c478bd9Sstevel@tonic-gate 10187c478bd9Sstevel@tonic-gate for (i = 0; i < num_slices; i++) { 10197c478bd9Sstevel@tonic-gate min_val = part[i].start; 10207c478bd9Sstevel@tonic-gate min_slice = i; 10217c478bd9Sstevel@tonic-gate for (j = i+1; j < num_slices; j++) { 10227c478bd9Sstevel@tonic-gate if (part[j].start < min_val) { 10237c478bd9Sstevel@tonic-gate min_val = part[j].start; 10247c478bd9Sstevel@tonic-gate min_slice = j; 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate if (min_slice != i) { 10287c478bd9Sstevel@tonic-gate SWAP(part[i].start, part[min_slice].start) 10297c478bd9Sstevel@tonic-gate SWAP(part[i].end, part[min_slice].end) 10307c478bd9Sstevel@tonic-gate SWAP(part[i].num, part[min_slice].num) 10317c478bd9Sstevel@tonic-gate } 10327c478bd9Sstevel@tonic-gate } 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate #ifdef DEBUG 10357c478bd9Sstevel@tonic-gate for (i = 0; i < num_slices; i++) { 1036*342440ecSPrasad Singamsetty DPRINTF4("\n %d (%d) : %llu, %llu", i, part[i].num, 1037*342440ecSPrasad Singamsetty part[i].start, part[i].end); 10387c478bd9Sstevel@tonic-gate } 10397c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 10407c478bd9Sstevel@tonic-gate 10417c478bd9Sstevel@tonic-gate if (backup_size > media_size) { 10427c478bd9Sstevel@tonic-gate if (sparc_style) { 10437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10447c478bd9Sstevel@tonic-gate gettext( 10457c478bd9Sstevel@tonic-gate "Backup slice extends beyond size of media\n")); 10467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1047*342440ecSPrasad Singamsetty gettext("media size : %llu sectors \n"), 1048*342440ecSPrasad Singamsetty media_size); 10497c478bd9Sstevel@tonic-gate } else { 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10527c478bd9Sstevel@tonic-gate gettext("Backup slice extends beyond size of FDISK \ 10537c478bd9Sstevel@tonic-gate Solaris partition\n")); 10547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10557c478bd9Sstevel@tonic-gate gettext( 1056*342440ecSPrasad Singamsetty "FDISK Solaris partition size : %llu sectors \n"), 10577c478bd9Sstevel@tonic-gate media_size); 10587c478bd9Sstevel@tonic-gate } 10597c478bd9Sstevel@tonic-gate return (-1); 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate /* 10637c478bd9Sstevel@tonic-gate * If we have only backup slice return success here. 10647c478bd9Sstevel@tonic-gate */ 10657c478bd9Sstevel@tonic-gate if (num_slices == 0) 10667c478bd9Sstevel@tonic-gate return (0); 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate if (backup_size) { 10697c478bd9Sstevel@tonic-gate if (part[num_slices - 1].end > backup_size) { 10707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10717c478bd9Sstevel@tonic-gate gettext("Slice %d extends beyond backup slice.\n"), 10727c478bd9Sstevel@tonic-gate part[num_slices -1].num); 10737c478bd9Sstevel@tonic-gate return (-1); 10747c478bd9Sstevel@tonic-gate } 10757c478bd9Sstevel@tonic-gate } else { 10767c478bd9Sstevel@tonic-gate if (part[num_slices - 1].end > media_size) { 10777c478bd9Sstevel@tonic-gate if (sparc_style) { 10787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10797c478bd9Sstevel@tonic-gate gettext( 10807c478bd9Sstevel@tonic-gate "Slice %d extends beyond media size\n"), 10817c478bd9Sstevel@tonic-gate part[num_slices -1].num); 10827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1083*342440ecSPrasad Singamsetty gettext("media size : %llu sectors \n"), 10847c478bd9Sstevel@tonic-gate media_size); 10857c478bd9Sstevel@tonic-gate } else { 10867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10877c478bd9Sstevel@tonic-gate gettext( 10887c478bd9Sstevel@tonic-gate "Slice %d extends beyond FDISK Solaris partition size\n"), 10897c478bd9Sstevel@tonic-gate part[num_slices -1].num); 1090*342440ecSPrasad Singamsetty (void) fprintf(stderr, gettext( 1091*342440ecSPrasad Singamsetty "FDISK Solaris partition size : %llu " 1092*342440ecSPrasad Singamsetty "sectors \n"), media_size); 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate return (-1); 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate 11007c478bd9Sstevel@tonic-gate for (i = 0; i < num_slices; i++) { 11017c478bd9Sstevel@tonic-gate if (i == 0) 11027c478bd9Sstevel@tonic-gate continue; 11037c478bd9Sstevel@tonic-gate if (part[i].start <= part[i-1].end) { 11047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 11057c478bd9Sstevel@tonic-gate gettext("Overlap between slices %d and %d\n"), 11067c478bd9Sstevel@tonic-gate part[i-1].num, part[i].num); 11077c478bd9Sstevel@tonic-gate (void) smedia_release_handle(handle); 11087c478bd9Sstevel@tonic-gate (void) close(fd); 11097c478bd9Sstevel@tonic-gate exit(1); 11107c478bd9Sstevel@tonic-gate } 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate 11137c478bd9Sstevel@tonic-gate return (0); 11147c478bd9Sstevel@tonic-gate } 11157c478bd9Sstevel@tonic-gate 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate static int32_t 11187c478bd9Sstevel@tonic-gate get_fdisk(smedia_handle_t handle, int32_t fd, int32_t offset, 11197c478bd9Sstevel@tonic-gate struct fdisk_info *fdisk) 11207c478bd9Sstevel@tonic-gate { 11217c478bd9Sstevel@tonic-gate struct mboot *boot_sec; 11227c478bd9Sstevel@tonic-gate struct ipart *part; 11237c478bd9Sstevel@tonic-gate char *buf; 11247c478bd9Sstevel@tonic-gate int32_t i, ret; 11257c478bd9Sstevel@tonic-gate int save_errno; 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate /* Read the master boot program */ 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate buf = (char *)malloc(med_info.sm_blocksize); 11307c478bd9Sstevel@tonic-gate if (buf == NULL) { 11317c478bd9Sstevel@tonic-gate PERROR("malloc failed"); 11327c478bd9Sstevel@tonic-gate exit(1); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate errno = 0; 11357c478bd9Sstevel@tonic-gate ret = ioctl(fd, DKIOCGMBOOT, buf); 11367c478bd9Sstevel@tonic-gate if (ret < 0) { 11377c478bd9Sstevel@tonic-gate if (errno != ENOTTY) { 11387c478bd9Sstevel@tonic-gate PERROR("DKIOCGMBOOT ioctl failed"); 11397c478bd9Sstevel@tonic-gate return (-1); 11407c478bd9Sstevel@tonic-gate } 11417c478bd9Sstevel@tonic-gate 1142e3397557Szk /* Turn on privileges. */ 1143e3397557Szk (void) __priv_bracket(PRIV_ON); 11447c478bd9Sstevel@tonic-gate 1145*342440ecSPrasad Singamsetty ret = smedia_raw_read(handle, 1146*342440ecSPrasad Singamsetty (diskaddr_t)offset/med_info.sm_blocksize, 1147*342440ecSPrasad Singamsetty buf, med_info.sm_blocksize); 11487c478bd9Sstevel@tonic-gate 1149e3397557Szk /* Turn off privileges. */ 1150e3397557Szk (void) __priv_bracket(PRIV_OFF); 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate save_errno = errno; 11537c478bd9Sstevel@tonic-gate errno = save_errno; 11547c478bd9Sstevel@tonic-gate if (ret != med_info.sm_blocksize) { 11557c478bd9Sstevel@tonic-gate if (errno == ENOTSUP) { 11567c478bd9Sstevel@tonic-gate errno = 0; 11577c478bd9Sstevel@tonic-gate if (lseek(fd, offset, SEEK_SET)) { 11587c478bd9Sstevel@tonic-gate PERROR("Seek failed:"); 11597c478bd9Sstevel@tonic-gate free(buf); 11607c478bd9Sstevel@tonic-gate return (-1); 11617c478bd9Sstevel@tonic-gate } 11627c478bd9Sstevel@tonic-gate 1163e3397557Szk /* Turn on privileges. */ 1164e3397557Szk (void) __priv_bracket(PRIV_ON); 11657c478bd9Sstevel@tonic-gate 11667c478bd9Sstevel@tonic-gate ret = read(fd, buf, sizeof (struct mboot)); 11677c478bd9Sstevel@tonic-gate 1168e3397557Szk /* Turn off privileges. */ 1169e3397557Szk (void) __priv_bracket(PRIV_OFF); 11707c478bd9Sstevel@tonic-gate 11717c478bd9Sstevel@tonic-gate if (ret != sizeof (struct mboot)) { 11727c478bd9Sstevel@tonic-gate PERROR("Could not read master boot record"); 11737c478bd9Sstevel@tonic-gate free(buf); 11747c478bd9Sstevel@tonic-gate return (-1); 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate } else { 11777c478bd9Sstevel@tonic-gate PERROR("Could not read master boot record"); 11787c478bd9Sstevel@tonic-gate free(buf); 11797c478bd9Sstevel@tonic-gate return (-1); 11807c478bd9Sstevel@tonic-gate } 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate } 11837c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 11847c478bd9Sstevel@tonic-gate boot_sec = (struct mboot *)buf; 11857c478bd9Sstevel@tonic-gate 11867c478bd9Sstevel@tonic-gate /* Is this really a master boot record? */ 11877c478bd9Sstevel@tonic-gate if (les(boot_sec->signature) != MBB_MAGIC) { 11887c478bd9Sstevel@tonic-gate DPRINTF("fdisk: Invalid master boot file \n"); 11897c478bd9Sstevel@tonic-gate DPRINTF2("Bad magic number: is %x, should be %x.\n", 11907c478bd9Sstevel@tonic-gate les(boot_sec->signature), MBB_MAGIC); 11917c478bd9Sstevel@tonic-gate free(buf); 11927c478bd9Sstevel@tonic-gate return (-1); 11937c478bd9Sstevel@tonic-gate } 11947c478bd9Sstevel@tonic-gate 11957c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 11967c478bd9Sstevel@tonic-gate DPRINTF1("part %d\n", i); 11977c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 11987c478bd9Sstevel@tonic-gate part = (struct ipart *)&boot_sec->parts[i * 11997c478bd9Sstevel@tonic-gate sizeof (struct ipart)]; 12007c478bd9Sstevel@tonic-gate fdisk->part[i].bootid = part->bootid; 12017c478bd9Sstevel@tonic-gate if (part->bootid && (part->bootid != ACTIVE)) { 12027c478bd9Sstevel@tonic-gate /* Hmmm...not a valid fdisk! */ 12037c478bd9Sstevel@tonic-gate return (-1); 12047c478bd9Sstevel@tonic-gate } 12057c478bd9Sstevel@tonic-gate fdisk->part[i].systid = part->systid; 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate /* To avoid the misalign access in sparc */ 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate fdisk->part[i].relsect = lel(GET_32(&(part->relsect))); 12107c478bd9Sstevel@tonic-gate fdisk->part[i].numsect = lel(GET_32(&(part->numsect))); 12117c478bd9Sstevel@tonic-gate 12127c478bd9Sstevel@tonic-gate DPRINTF1("\tboot id 0x%x\n", part->bootid); 12137c478bd9Sstevel@tonic-gate DPRINTF1("\tsystem id 0x%x\n", part->systid); 12147c478bd9Sstevel@tonic-gate DPRINTF1("\trel sector 0x%x\n", fdisk->part[i].relsect); 12157c478bd9Sstevel@tonic-gate DPRINTF1("\tnum sector 0x%x\n", fdisk->part[i].numsect); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate free(buf); 12187c478bd9Sstevel@tonic-gate return (0); 12197c478bd9Sstevel@tonic-gate } 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate 12227c478bd9Sstevel@tonic-gate /* 12237c478bd9Sstevel@tonic-gate * wrrite_defualt_label(int32_t fd) 12247c478bd9Sstevel@tonic-gate * fd = file descriptor for the device. 12257c478bd9Sstevel@tonic-gate * 12267c478bd9Sstevel@tonic-gate * For sparc solaris 12277c478bd9Sstevel@tonic-gate * Create a vtoc partition with 12287c478bd9Sstevel@tonic-gate * slice 0 = slice 2 = medium capacity. 12297c478bd9Sstevel@tonic-gate * The cyl, head, sect (CHS) values are computed as done in sd 12307c478bd9Sstevel@tonic-gate * capacity <= 1GB, 12317c478bd9Sstevel@tonic-gate * nhead = 64, nsect = 32 12327c478bd9Sstevel@tonic-gate * capacity > 1gb, 12337c478bd9Sstevel@tonic-gate * nhead = 255, nsect = 63 12347c478bd9Sstevel@tonic-gate * 12357c478bd9Sstevel@tonic-gate * For x86 solaris 12367c478bd9Sstevel@tonic-gate * Create a fdisk partition, 12377c478bd9Sstevel@tonic-gate * partition 0 covers the full medium, the partition 12387c478bd9Sstevel@tonic-gate * type is set to Solaris. 12397c478bd9Sstevel@tonic-gate * Then create solaris vtoc. The algorithm is same as sparc solaris. 12407c478bd9Sstevel@tonic-gate * But the capacity is reduced by 1 cyl, to leave space for fdisk table. 12417c478bd9Sstevel@tonic-gate */ 12427c478bd9Sstevel@tonic-gate 12437c478bd9Sstevel@tonic-gate #ifdef sparc 12447c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 12457c478bd9Sstevel@tonic-gate void 12467c478bd9Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd) 12477c478bd9Sstevel@tonic-gate { 12487c478bd9Sstevel@tonic-gate 1249*342440ecSPrasad Singamsetty struct extvtoc v_toc; 1250*342440ecSPrasad Singamsetty uint32_t nhead, numcyl, nsect; 1251*342440ecSPrasad Singamsetty diskaddr_t capacity; 12527c478bd9Sstevel@tonic-gate int32_t ret; 12537c478bd9Sstevel@tonic-gate char asciilabel[LEN_DKL_ASCII]; 12547c478bd9Sstevel@tonic-gate char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0"; 1255*342440ecSPrasad Singamsetty uint32_t acyl = 2; 12567c478bd9Sstevel@tonic-gate 12577c478bd9Sstevel@tonic-gate 12587c478bd9Sstevel@tonic-gate DPRINTF("Writing default vtoc\n"); 12597c478bd9Sstevel@tonic-gate (void) memset(&v_toc, 0, sizeof (v_toc)); 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate v_toc.v_nparts = V_NUMPAR; 12637c478bd9Sstevel@tonic-gate v_toc.v_sanity = VTOC_SANE; 12647c478bd9Sstevel@tonic-gate v_toc.v_version = V_VERSION; 12657c478bd9Sstevel@tonic-gate v_toc.v_sectorsz = DEV_BSIZE; 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate /* 12687c478bd9Sstevel@tonic-gate * For the head, cyl and number of sector per track, 12697c478bd9Sstevel@tonic-gate * if the capacity <= 1GB, head = 64, sect = 32. 12707c478bd9Sstevel@tonic-gate * else head = 255, sect 63 12717c478bd9Sstevel@tonic-gate * NOTE: the capacity should be equal to C*H*S values. 12727c478bd9Sstevel@tonic-gate * This will cause some truncation of size due to 12737c478bd9Sstevel@tonic-gate * round off errors. 12747c478bd9Sstevel@tonic-gate */ 1275*342440ecSPrasad Singamsetty if ((uint32_t)med_info.sm_capacity <= 0x200000) { 12767c478bd9Sstevel@tonic-gate nhead = 64; 12777c478bd9Sstevel@tonic-gate nsect = 32; 12787c478bd9Sstevel@tonic-gate } else { 12797c478bd9Sstevel@tonic-gate nhead = 255; 12807c478bd9Sstevel@tonic-gate nsect = 63; 12817c478bd9Sstevel@tonic-gate } 12827c478bd9Sstevel@tonic-gate 1283*342440ecSPrasad Singamsetty numcyl = (uint32_t)med_info.sm_capacity / (nhead * nsect); 1284*342440ecSPrasad Singamsetty capacity = (diskaddr_t)nhead * nsect * numcyl; 12857c478bd9Sstevel@tonic-gate 12867c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_start = 0; 12877c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_size = capacity; 12887c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_tag = V_ROOT; 12897c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_flag = 0; /* Mountable */ 12907c478bd9Sstevel@tonic-gate 12917c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_start = 0; 12927c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_size = capacity; 12937c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_tag = V_BACKUP; 12947c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_flag = V_UNMNT; 12957c478bd9Sstevel@tonic-gate 12967c478bd9Sstevel@tonic-gate /* Create asciilabel for compatibility with format utility */ 12977c478bd9Sstevel@tonic-gate (void) snprintf(asciilabel, sizeof (asciilabel), 12987c478bd9Sstevel@tonic-gate "%s cyl %d alt %d hd %d sec %d", 12997c478bd9Sstevel@tonic-gate asciilabel2, numcyl, acyl, nhead, nsect); 13007c478bd9Sstevel@tonic-gate (void) memcpy(v_toc.v_asciilabel, asciilabel, 13017c478bd9Sstevel@tonic-gate LEN_DKL_ASCII); 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate errno = 0; 13047c478bd9Sstevel@tonic-gate 1305e3397557Szk /* Turn on privileges. */ 1306e3397557Szk (void) __priv_bracket(PRIV_ON); 13077c478bd9Sstevel@tonic-gate 1308*342440ecSPrasad Singamsetty ret = write_extvtoc(fd, &v_toc); 13097c478bd9Sstevel@tonic-gate 1310e3397557Szk /* Turn off privileges. */ 1311e3397557Szk (void) __priv_bracket(PRIV_OFF); 13127c478bd9Sstevel@tonic-gate 13137c478bd9Sstevel@tonic-gate if (ret < 0) { 13147c478bd9Sstevel@tonic-gate PERROR("write VTOC failed"); 13157c478bd9Sstevel@tonic-gate DPRINTF1("Errno = %d\n", errno); 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate } 13187c478bd9Sstevel@tonic-gate 13197c478bd9Sstevel@tonic-gate #else /* !sparc */ 13207c478bd9Sstevel@tonic-gate #ifdef i386 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate void 13237c478bd9Sstevel@tonic-gate write_default_label(smedia_handle_t handle, int32_t fd) 13247c478bd9Sstevel@tonic-gate { 13257c478bd9Sstevel@tonic-gate 13267c478bd9Sstevel@tonic-gate int32_t i, ret; 13277c478bd9Sstevel@tonic-gate struct dk_geom dkg; 1328*342440ecSPrasad Singamsetty struct extvtoc v_toc; 13297c478bd9Sstevel@tonic-gate int tmp_fd; 13307c478bd9Sstevel@tonic-gate char *fdisk_buf; 13317c478bd9Sstevel@tonic-gate struct mboot boot_code; /* Buffer for master boot record */ 13327c478bd9Sstevel@tonic-gate struct ipart parts[FD_NUMPART]; 1333*342440ecSPrasad Singamsetty uint32_t numcyl, nhead, nsect; 1334*342440ecSPrasad Singamsetty uint32_t unixend; 1335*342440ecSPrasad Singamsetty uint32_t blocksize; 1336*342440ecSPrasad Singamsetty diskaddr_t capacity; 13377c478bd9Sstevel@tonic-gate int save_errno; 13387c478bd9Sstevel@tonic-gate size_t bytes_written; 13397c478bd9Sstevel@tonic-gate char asciilabel[LEN_DKL_ASCII]; 13407c478bd9Sstevel@tonic-gate char asciilabel2[LEN_DKL_ASCII] = "DEFAULT\0"; 1341*342440ecSPrasad Singamsetty uint32_t acyl = 2; 13427c478bd9Sstevel@tonic-gate 13437c478bd9Sstevel@tonic-gate DPRINTF("Writing default fdisk table and vtoc\n"); 13447c478bd9Sstevel@tonic-gate (void) memset(&v_toc, 0, sizeof (v_toc)); 13457c478bd9Sstevel@tonic-gate /* 13467c478bd9Sstevel@tonic-gate * Try getting disk geometry. 13477c478bd9Sstevel@tonic-gate */ 13487c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) 13497c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCG_PHYGEOM, &dkg) < 0) { 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate DPRINTF("DKIOCG_PHYGEOM ioctl failed"); 13527c478bd9Sstevel@tonic-gate return; 13537c478bd9Sstevel@tonic-gate } 13547c478bd9Sstevel@tonic-gate 13557c478bd9Sstevel@tonic-gate tmp_fd = open("/usr/lib/fs/ufs/mboot", O_RDONLY); 13567c478bd9Sstevel@tonic-gate if (tmp_fd <= 0) { 13577c478bd9Sstevel@tonic-gate return; 13587c478bd9Sstevel@tonic-gate } 13597c478bd9Sstevel@tonic-gate 13607c478bd9Sstevel@tonic-gate if (read(tmp_fd, &boot_code, sizeof (struct mboot)) 13617c478bd9Sstevel@tonic-gate != sizeof (struct mboot)) { 13627c478bd9Sstevel@tonic-gate (void) close(tmp_fd); 13637c478bd9Sstevel@tonic-gate return; 13647c478bd9Sstevel@tonic-gate } 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate blocksize = med_info.sm_blocksize; 13677c478bd9Sstevel@tonic-gate fdisk_buf = (char *)malloc(blocksize); 13687c478bd9Sstevel@tonic-gate if (fdisk_buf == NULL) { 13697c478bd9Sstevel@tonic-gate DPRINTF("malloc for fdisk_buf failed\n"); 13707c478bd9Sstevel@tonic-gate return; 13717c478bd9Sstevel@tonic-gate } 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate (void) memset(&parts, 0, sizeof (parts)); 13747c478bd9Sstevel@tonic-gate 13757c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 13767c478bd9Sstevel@tonic-gate parts[i].systid = UNUSED; 13777c478bd9Sstevel@tonic-gate parts[i].numsect = lel(UNUSED); 13787c478bd9Sstevel@tonic-gate parts[i].relsect = lel(UNUSED); 13797c478bd9Sstevel@tonic-gate parts[i].bootid = 0; 13807c478bd9Sstevel@tonic-gate } 13817c478bd9Sstevel@tonic-gate 13827c478bd9Sstevel@tonic-gate numcyl = dkg.dkg_ncyl; 13837c478bd9Sstevel@tonic-gate nhead = dkg.dkg_nhead; 13847c478bd9Sstevel@tonic-gate nsect = dkg.dkg_nsect; 13857c478bd9Sstevel@tonic-gate 13867c478bd9Sstevel@tonic-gate parts[0].bootid = ACTIVE; 13877c478bd9Sstevel@tonic-gate parts[0].begsect = 1; 13887c478bd9Sstevel@tonic-gate 13897c478bd9Sstevel@tonic-gate unixend = numcyl; 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate parts[0].relsect = lel(nhead * nsect); 1392*342440ecSPrasad Singamsetty parts[0].numsect = lel(((diskaddr_t)numcyl * nhead * nsect)); 13937c478bd9Sstevel@tonic-gate parts[0].systid = SUNIXOS2; /* Solaris */ 13947c478bd9Sstevel@tonic-gate parts[0].beghead = 0; 13957c478bd9Sstevel@tonic-gate parts[0].begcyl = 1; 13967c478bd9Sstevel@tonic-gate parts[0].endhead = nhead - 1; 13977c478bd9Sstevel@tonic-gate parts[0].endsect = (nsect & 0x3f) | 13987c478bd9Sstevel@tonic-gate (char)((unixend >> 2) & 0x00c0); 13997c478bd9Sstevel@tonic-gate parts[0].endcyl = (char)(unixend & 0x00ff); 14007c478bd9Sstevel@tonic-gate 14017c478bd9Sstevel@tonic-gate (void) memcpy(&(boot_code.parts), parts, sizeof (parts)); 14027c478bd9Sstevel@tonic-gate (void) memcpy(fdisk_buf, &boot_code, sizeof (boot_code)); 14037c478bd9Sstevel@tonic-gate 1404e3397557Szk /* Turn on privileges. */ 1405e3397557Szk (void) __priv_bracket(PRIV_ON); 14067c478bd9Sstevel@tonic-gate 14077c478bd9Sstevel@tonic-gate ret = ioctl(fd, DKIOCSMBOOT, fdisk_buf); 14087c478bd9Sstevel@tonic-gate 1409e3397557Szk /* Turn off privileges. */ 1410e3397557Szk (void) __priv_bracket(PRIV_OFF); 14117c478bd9Sstevel@tonic-gate 14127c478bd9Sstevel@tonic-gate if (ret == -1) { 14137c478bd9Sstevel@tonic-gate if (errno != ENOTTY) { 14147c478bd9Sstevel@tonic-gate PERROR("DKIOCSMBOOT ioctl Failed"); 14157c478bd9Sstevel@tonic-gate return; 14167c478bd9Sstevel@tonic-gate } 14177c478bd9Sstevel@tonic-gate 1418e3397557Szk /* Turn on privileges. */ 1419e3397557Szk (void) __priv_bracket(PRIV_ON); 14207c478bd9Sstevel@tonic-gate 1421*342440ecSPrasad Singamsetty bytes_written = smedia_raw_write(handle, (diskaddr_t)0, 1422*342440ecSPrasad Singamsetty fdisk_buf, blocksize); 14237c478bd9Sstevel@tonic-gate 1424e3397557Szk /* Turn off privileges. */ 1425e3397557Szk (void) __priv_bracket(PRIV_OFF); 14267c478bd9Sstevel@tonic-gate 14277c478bd9Sstevel@tonic-gate save_errno = errno; 14287c478bd9Sstevel@tonic-gate errno = save_errno; 14297c478bd9Sstevel@tonic-gate if (bytes_written != blocksize) { 14307c478bd9Sstevel@tonic-gate if (errno == ENOTSUP) { 14317c478bd9Sstevel@tonic-gate 1432e3397557Szk /* Turn on privileges. */ 1433e3397557Szk (void) __priv_bracket(PRIV_ON); 14347c478bd9Sstevel@tonic-gate 14357c478bd9Sstevel@tonic-gate ret = write(fd, fdisk_buf, blocksize); 14367c478bd9Sstevel@tonic-gate 1437e3397557Szk /* Turn off privileges. */ 1438e3397557Szk (void) __priv_bracket(PRIV_OFF); 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate if (ret != blocksize) { 14417c478bd9Sstevel@tonic-gate return; 14427c478bd9Sstevel@tonic-gate } 14437c478bd9Sstevel@tonic-gate } else { 14447c478bd9Sstevel@tonic-gate return; 14457c478bd9Sstevel@tonic-gate } 14467c478bd9Sstevel@tonic-gate } 14477c478bd9Sstevel@tonic-gate } 1448*342440ecSPrasad Singamsetty capacity = (diskaddr_t)(numcyl - 1) * nhead * nsect; 14497c478bd9Sstevel@tonic-gate 14507c478bd9Sstevel@tonic-gate v_toc.v_nparts = V_NUMPAR; 14517c478bd9Sstevel@tonic-gate v_toc.v_sanity = VTOC_SANE; 14527c478bd9Sstevel@tonic-gate v_toc.v_version = V_VERSION; 14537c478bd9Sstevel@tonic-gate v_toc.v_sectorsz = DEV_BSIZE; 14547c478bd9Sstevel@tonic-gate 14557c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_start = 0; 14567c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_size = capacity; 14577c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_tag = V_ROOT; 14587c478bd9Sstevel@tonic-gate v_toc.v_part[0].p_flag = 0; /* Mountable */ 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_start = 0; 14617c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_size = capacity; 14627c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_tag = V_BACKUP; 14637c478bd9Sstevel@tonic-gate v_toc.v_part[2].p_flag = V_UNMNT; 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate /* Create asciilabel for compatibility with format utility */ 14667c478bd9Sstevel@tonic-gate (void) snprintf(asciilabel, sizeof (asciilabel), 14677c478bd9Sstevel@tonic-gate "%s cyl %d alt %d hd %d sec %d", 14687c478bd9Sstevel@tonic-gate asciilabel2, numcyl, acyl, nhead, nsect); 14697c478bd9Sstevel@tonic-gate (void) memcpy(v_toc.v_asciilabel, asciilabel, 14707c478bd9Sstevel@tonic-gate LEN_DKL_ASCII); 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate errno = 0; 14737c478bd9Sstevel@tonic-gate 14747c478bd9Sstevel@tonic-gate 1475e3397557Szk /* Turn on privileges. */ 1476e3397557Szk (void) __priv_bracket(PRIV_ON); 14777c478bd9Sstevel@tonic-gate 1478*342440ecSPrasad Singamsetty ret = write_extvtoc(fd, &v_toc); 14797c478bd9Sstevel@tonic-gate 1480e3397557Szk /* Turn off privileges. */ 1481e3397557Szk (void) __priv_bracket(PRIV_OFF); 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate if (ret < 0) { 14847c478bd9Sstevel@tonic-gate PERROR("write VTOC failed"); 14857c478bd9Sstevel@tonic-gate DPRINTF1("Errno = %d\n", errno); 14867c478bd9Sstevel@tonic-gate } 14877c478bd9Sstevel@tonic-gate } 14887c478bd9Sstevel@tonic-gate 14897c478bd9Sstevel@tonic-gate #else /* !i386 */ 14907c478bd9Sstevel@tonic-gate 14917c478bd9Sstevel@tonic-gate #error One of sparc or i386 must be defined! 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate #endif /* i386 */ 14947c478bd9Sstevel@tonic-gate #endif /* sparc */ 14957c478bd9Sstevel@tonic-gate 14967c478bd9Sstevel@tonic-gate /* 14977c478bd9Sstevel@tonic-gate * void overwrite_metadata(int32_t fd, smedia_handle_t handle) 14987c478bd9Sstevel@tonic-gate * 14997c478bd9Sstevel@tonic-gate * purpose : quick format does not erase the data on Iomega 15007c478bd9Sstevel@tonic-gate * zip/jaz media. So, the meta data on the disk should be erased. 15017c478bd9Sstevel@tonic-gate * 15027c478bd9Sstevel@tonic-gate * If there is a valid fdisk table, 15037c478bd9Sstevel@tonic-gate * erase first 64K of each partition. 15047c478bd9Sstevel@tonic-gate * If there is a valid vtoc, 15057c478bd9Sstevel@tonic-gate * erase first 64k of each slice. 15067c478bd9Sstevel@tonic-gate * Then erase the 0th sector (the home for vtoc and fdisk) of the disk. 15077c478bd9Sstevel@tonic-gate * Note that teh vtoc on x86 resides in one of the fdisk partition. 15087c478bd9Sstevel@tonic-gate * So delay the erasing of the solaris partition until the vtoc is read. 15097c478bd9Sstevel@tonic-gate */ 15107c478bd9Sstevel@tonic-gate 15117c478bd9Sstevel@tonic-gate void 15127c478bd9Sstevel@tonic-gate overwrite_metadata(int32_t fd, smedia_handle_t handle) 15137c478bd9Sstevel@tonic-gate { 15147c478bd9Sstevel@tonic-gate 15157c478bd9Sstevel@tonic-gate struct fdisk_info fdisk; 1516*342440ecSPrasad Singamsetty diskaddr_t sol_offset = 0; 15177c478bd9Sstevel@tonic-gate int i, ret; 1518*342440ecSPrasad Singamsetty struct extvtoc t_vtoc; 15197c478bd9Sstevel@tonic-gate #ifdef i386 1520*342440ecSPrasad Singamsetty diskaddr_t sol_size = 0; 15217c478bd9Sstevel@tonic-gate int32_t active = 0; 15227c478bd9Sstevel@tonic-gate #endif /* i386 */ 15237c478bd9Sstevel@tonic-gate 15247c478bd9Sstevel@tonic-gate /* Get fdisk info. */ 15257c478bd9Sstevel@tonic-gate if (get_fdisk(handle, fd, 0, &fdisk) >= 0) { 15267c478bd9Sstevel@tonic-gate /* Got a valid fdisk */ 15277c478bd9Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate if (fdisk.part[i].numsect == 0) 15307c478bd9Sstevel@tonic-gate continue; 15317c478bd9Sstevel@tonic-gate if ((fdisk.part[i].systid == UNUSED) || 15327c478bd9Sstevel@tonic-gate (fdisk.part[i].systid == 0)) 15337c478bd9Sstevel@tonic-gate continue; 15347c478bd9Sstevel@tonic-gate #ifdef i386 15357c478bd9Sstevel@tonic-gate if (fdisk.part[i].systid == SUNIXOS || 15367c478bd9Sstevel@tonic-gate fdisk.part[i].systid == SUNIXOS2) { 15377c478bd9Sstevel@tonic-gate if (!sol_offset) { 15387c478bd9Sstevel@tonic-gate sol_offset = fdisk.part[i].relsect; 15397c478bd9Sstevel@tonic-gate sol_size = fdisk.part[i].numsect; 15407c478bd9Sstevel@tonic-gate if (fdisk.part[i].bootid == ACTIVE) 15417c478bd9Sstevel@tonic-gate active = 1; 15427c478bd9Sstevel@tonic-gate continue; 15437c478bd9Sstevel@tonic-gate } else if ((fdisk.part[i].bootid == ACTIVE) && 15447c478bd9Sstevel@tonic-gate (!active)) { 15457c478bd9Sstevel@tonic-gate erase(handle, sol_offset, sol_size); 15467c478bd9Sstevel@tonic-gate sol_offset = fdisk.part[i].relsect; 15477c478bd9Sstevel@tonic-gate sol_size = fdisk.part[i].numsect; 15487c478bd9Sstevel@tonic-gate active = 1; 15497c478bd9Sstevel@tonic-gate continue; 15507c478bd9Sstevel@tonic-gate } 15517c478bd9Sstevel@tonic-gate } 15527c478bd9Sstevel@tonic-gate #endif /* i386 */ 1553*342440ecSPrasad Singamsetty erase(handle, (diskaddr_t)fdisk.part[i].relsect, 1554*342440ecSPrasad Singamsetty (diskaddr_t)fdisk.part[i].numsect); 15557c478bd9Sstevel@tonic-gate } 15567c478bd9Sstevel@tonic-gate } 15577c478bd9Sstevel@tonic-gate 15587c478bd9Sstevel@tonic-gate (void) memset(&t_vtoc, 0, sizeof (t_vtoc)); 15597c478bd9Sstevel@tonic-gate 15607c478bd9Sstevel@tonic-gate if (sol_offset) { 15617c478bd9Sstevel@tonic-gate /* fdisk x86 Solaris partition */ 15627c478bd9Sstevel@tonic-gate /* VTOC location in solaris partition is DK_LABEL_LOC */ 15637c478bd9Sstevel@tonic-gate 1564e3397557Szk /* Turn on privileges. */ 1565e3397557Szk (void) __priv_bracket(PRIV_ON); 15667c478bd9Sstevel@tonic-gate 1567*342440ecSPrasad Singamsetty ret = read_extvtoc(fd, &t_vtoc); 15687c478bd9Sstevel@tonic-gate 1569e3397557Szk /* Turn off privileges. */ 15700c887d22Szk (void) __priv_bracket(PRIV_OFF); 15717c478bd9Sstevel@tonic-gate 15727c478bd9Sstevel@tonic-gate if (ret < 0) { 15737c478bd9Sstevel@tonic-gate /* No valid vtoc, erase fdisk table. */ 1574*342440ecSPrasad Singamsetty erase(handle, (diskaddr_t)0, (diskaddr_t)1); 15757c478bd9Sstevel@tonic-gate return; 15767c478bd9Sstevel@tonic-gate } 15777c478bd9Sstevel@tonic-gate } else { 15787c478bd9Sstevel@tonic-gate /* Sparc Solaris or x86 solaris with faked fdisk */ 15797c478bd9Sstevel@tonic-gate 1580e3397557Szk /* Turn on privileges */ 1581e3397557Szk (void) __priv_bracket(PRIV_ON); 15827c478bd9Sstevel@tonic-gate 1583*342440ecSPrasad Singamsetty ret = read_extvtoc(fd, &t_vtoc); 15847c478bd9Sstevel@tonic-gate 1585e3397557Szk /* Turn off privileges. */ 1586e3397557Szk (void) __priv_bracket(PRIV_OFF); 15877c478bd9Sstevel@tonic-gate 15887c478bd9Sstevel@tonic-gate if (ret < 0) { 15897c478bd9Sstevel@tonic-gate /* No valid vtoc, erase from 0th sector */ 1590*342440ecSPrasad Singamsetty erase(handle, (diskaddr_t)0, 1591*342440ecSPrasad Singamsetty (uint32_t)med_info.sm_capacity); 15927c478bd9Sstevel@tonic-gate return; 15937c478bd9Sstevel@tonic-gate } 15947c478bd9Sstevel@tonic-gate } 15957c478bd9Sstevel@tonic-gate 15967c478bd9Sstevel@tonic-gate for (i = 0; i < V_NUMPAR; i++) { 15977c478bd9Sstevel@tonic-gate if (t_vtoc.v_part[i].p_size != 0) { 15987c478bd9Sstevel@tonic-gate erase(handle, sol_offset + t_vtoc.v_part[i].p_start, 15997c478bd9Sstevel@tonic-gate t_vtoc.v_part[i].p_size); 16007c478bd9Sstevel@tonic-gate /* 16017c478bd9Sstevel@tonic-gate * To make the udfs not recognise the partition we will 16027c478bd9Sstevel@tonic-gate * erase sectors 256, (p_size-256) and psize. 16037c478bd9Sstevel@tonic-gate */ 16047c478bd9Sstevel@tonic-gate erase(handle, 1605*342440ecSPrasad Singamsetty sol_offset + t_vtoc.v_part[i].p_start + 256, 1606*342440ecSPrasad Singamsetty (diskaddr_t)1); 16077c478bd9Sstevel@tonic-gate erase(handle, 1608*342440ecSPrasad Singamsetty (sol_offset + t_vtoc.v_part[i].p_start + 1609*342440ecSPrasad Singamsetty t_vtoc.v_part[i].p_size - 256), 1610*342440ecSPrasad Singamsetty (diskaddr_t)1); 16117c478bd9Sstevel@tonic-gate erase(handle, 1612*342440ecSPrasad Singamsetty (sol_offset + t_vtoc.v_part[i].p_start + 1613*342440ecSPrasad Singamsetty t_vtoc.v_part[i].p_size - 1), 1614*342440ecSPrasad Singamsetty (diskaddr_t)1); 16157c478bd9Sstevel@tonic-gate } 16167c478bd9Sstevel@tonic-gate } 16177c478bd9Sstevel@tonic-gate 16187c478bd9Sstevel@tonic-gate /* 16197c478bd9Sstevel@tonic-gate * If x86 fdisk solaris partition, erase the vtoc also. 16207c478bd9Sstevel@tonic-gate * for sparc, the erasing 0the sector erases vtoc. 16217c478bd9Sstevel@tonic-gate */ 16227c478bd9Sstevel@tonic-gate if (sol_offset) { 1623*342440ecSPrasad Singamsetty erase(handle, sol_offset, (diskaddr_t)DK_LABEL_LOC + 2); 16247c478bd9Sstevel@tonic-gate } 16257c478bd9Sstevel@tonic-gate 16267c478bd9Sstevel@tonic-gate /* 16277c478bd9Sstevel@tonic-gate * erase the 0th sector, it is not guaranteed to be 16287c478bd9Sstevel@tonic-gate * erased in the above sequence. 16297c478bd9Sstevel@tonic-gate */ 16307c478bd9Sstevel@tonic-gate 1631*342440ecSPrasad Singamsetty erase(handle, (diskaddr_t)0, (diskaddr_t)1); 16327c478bd9Sstevel@tonic-gate } 16337c478bd9Sstevel@tonic-gate 16347c478bd9Sstevel@tonic-gate /* 16357c478bd9Sstevel@tonic-gate * void erase(smedia_handle_t handle, uint32_t offset, uint32_t size) 16367c478bd9Sstevel@tonic-gate * 16377c478bd9Sstevel@tonic-gate * Initialize the media with '0' from offset 'offset' upto 'size' 16387c478bd9Sstevel@tonic-gate * or 128 blocks(64k), whichever is smaller. 16397c478bd9Sstevel@tonic-gate */ 16407c478bd9Sstevel@tonic-gate 16417c478bd9Sstevel@tonic-gate static void 1642*342440ecSPrasad Singamsetty erase(smedia_handle_t handle, diskaddr_t offset, diskaddr_t size) 16437c478bd9Sstevel@tonic-gate { 16447c478bd9Sstevel@tonic-gate char *buf; 1645*342440ecSPrasad Singamsetty diskaddr_t nblocks = size; 16467c478bd9Sstevel@tonic-gate int32_t ret; 16477c478bd9Sstevel@tonic-gate 16487c478bd9Sstevel@tonic-gate 16497c478bd9Sstevel@tonic-gate nblocks = (nblocks < 128) ? nblocks : 128; 16507c478bd9Sstevel@tonic-gate buf = (char *)malloc(nblocks * med_info.sm_blocksize); 16517c478bd9Sstevel@tonic-gate if (buf == NULL) { 16527c478bd9Sstevel@tonic-gate PERROR("malloc failed"); 16537c478bd9Sstevel@tonic-gate return; 16547c478bd9Sstevel@tonic-gate } 1655*342440ecSPrasad Singamsetty (void) memset(buf, 0, (size_t)nblocks * med_info.sm_blocksize); 16567c478bd9Sstevel@tonic-gate 1657e3397557Szk /* Turn on privileges. */ 1658e3397557Szk (void) __priv_bracket(PRIV_ON); 16597c478bd9Sstevel@tonic-gate 16607c478bd9Sstevel@tonic-gate ret = smedia_raw_write(handle, offset, buf, 1661*342440ecSPrasad Singamsetty (size_t)nblocks * med_info.sm_blocksize); 16627c478bd9Sstevel@tonic-gate 1663e3397557Szk /* Turn off privileges. */ 1664e3397557Szk (void) __priv_bracket(PRIV_OFF); 16657c478bd9Sstevel@tonic-gate 16667c478bd9Sstevel@tonic-gate if (ret != (nblocks * med_info.sm_blocksize)) 16677c478bd9Sstevel@tonic-gate PERROR("error in writing\n"); 16687c478bd9Sstevel@tonic-gate 16697c478bd9Sstevel@tonic-gate free(buf); 16707c478bd9Sstevel@tonic-gate 16717c478bd9Sstevel@tonic-gate } 1672