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
5de1f1563Slh * Common Development and Distribution License (the "License").
6de1f1563Slh * 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 /*
2265908c77Syu, larry liu - Sun Microsystems - Beijing China * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
2448bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * This file contains the main entry point of the program and other
297c478bd9Sstevel@tonic-gate * routines relating to the general flow.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate #include "global.h"
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <memory.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #ifdef sparc
417c478bd9Sstevel@tonic-gate #include <sys/hdio.h>
427c478bd9Sstevel@tonic-gate #include <sys/dkbad.h>
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include <sys/time.h>
467c478bd9Sstevel@tonic-gate #include "main.h"
477c478bd9Sstevel@tonic-gate #include "analyze.h"
487c478bd9Sstevel@tonic-gate #include "menu.h"
497c478bd9Sstevel@tonic-gate #include "param.h"
507c478bd9Sstevel@tonic-gate #include "misc.h"
517c478bd9Sstevel@tonic-gate #include "startup.h"
527c478bd9Sstevel@tonic-gate #include "menu_command.h"
537c478bd9Sstevel@tonic-gate #include "menu_partition.h"
547c478bd9Sstevel@tonic-gate #include "prompts.h"
553e1bd7a2Ssjelinek #include "checkdev.h"
567c478bd9Sstevel@tonic-gate #include "label.h"
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate extern struct menu_item menu_command[];
59a11d78d6SToomas Soome uint_t apc;
60a11d78d6SToomas Soome uint_t solaris_offset;
61a11d78d6SToomas Soome char cur_menu;
62a11d78d6SToomas Soome char last_menu;
63a11d78d6SToomas Soome void *pattern_buf;
64a11d78d6SToomas Soome FILE *log_file;
65a11d78d6SToomas Soome void *cur_buf;
66a11d78d6SToomas Soome struct disk_info *cur_disk;
67a11d78d6SToomas Soome struct ctlr_ops *cur_ops;
68a11d78d6SToomas Soome struct ctlr_info *cur_ctlr;
69a11d78d6SToomas Soome struct ctlr_type *cur_ctype;
70a11d78d6SToomas Soome struct disk_type *cur_dtype;
71a11d78d6SToomas Soome struct partition_info *cur_parts;
72a11d78d6SToomas Soome struct defect_list cur_list;
73a11d78d6SToomas Soome int cur_file;
74a11d78d6SToomas Soome int cur_flags;
75a11d78d6SToomas Soome int cur_label;
76a11d78d6SToomas Soome uint_t pcyl;
77a11d78d6SToomas Soome uint_t ncyl;
78a11d78d6SToomas Soome uint_t acyl;
79a11d78d6SToomas Soome uint_t bcyl;
80a11d78d6SToomas Soome uint_t nhead;
81a11d78d6SToomas Soome uint_t phead;
82a11d78d6SToomas Soome uint_t nsect;
83a11d78d6SToomas Soome uint_t psect;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate #ifdef __STDC__
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * Local prototypes for ANSI C compilers
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate static void get_disk_characteristics(void);
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate #else /* __STDC__ */
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * Local prototypes for non-ANSI C compilers
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate static void get_disk_characteristics();
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * This is the main entry point.
1047c478bd9Sstevel@tonic-gate */
105052b6e8aSbg int
main(int argc,char * argv[])106052b6e8aSbg main(int argc, char *argv[])
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate int i;
1097c478bd9Sstevel@tonic-gate int ret_code = 1;
1107c478bd9Sstevel@tonic-gate char **arglist;
1117c478bd9Sstevel@tonic-gate struct disk_info *disk = NULL;
1127c478bd9Sstevel@tonic-gate struct disk_type *type, *oldtype;
1137c478bd9Sstevel@tonic-gate struct partition_info *parts;
1147c478bd9Sstevel@tonic-gate struct sigaction act;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate solaris_offset = 0;
117de1f1563Slh /*
118de1f1563Slh * Initialize cur_ctype to avoid null pointer dereference
119de1f1563Slh * in auto_efi_sense().
120de1f1563Slh */
121*b12aaafbSToomas Soome cur_ctype = NULL;
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Decode the command line options.
1247c478bd9Sstevel@tonic-gate */
1257c478bd9Sstevel@tonic-gate i = do_options(argc, argv);
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate * If we are to run from a command file, open it up.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate if (option_f) {
1307c478bd9Sstevel@tonic-gate if (freopen(option_f, "r", stdin) == NULL) {
1317c478bd9Sstevel@tonic-gate err_print("Unable to open command file '%s'.\n",
132af007057Syl option_f);
1337c478bd9Sstevel@tonic-gate fullabort();
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * If we are logging, open the log file.
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate if (option_l) {
1407c478bd9Sstevel@tonic-gate if ((log_file = fopen(option_l, "w")) == NULL) {
1417c478bd9Sstevel@tonic-gate err_print("Unable to open log file '%s'.\n",
142af007057Syl option_l);
1437c478bd9Sstevel@tonic-gate fullabort();
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate * Read in the data file and initialize the hardware structs.
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate sup_init();
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * If there are no disks on the command line, search the
1527c478bd9Sstevel@tonic-gate * appropriate device directory for character devices that
1537c478bd9Sstevel@tonic-gate * look like disks.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate if (i < 0) {
156*b12aaafbSToomas Soome arglist = NULL;
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * There were disks on the command line. They comprise the
1597c478bd9Sstevel@tonic-gate * search list.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate } else {
1627c478bd9Sstevel@tonic-gate arglist = &argv[i];
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * Perform the search for disks.
1667c478bd9Sstevel@tonic-gate */
1677c478bd9Sstevel@tonic-gate do_search(arglist);
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate * Catch ctrl-C and ctrl-Z so critical sections can be
1707c478bd9Sstevel@tonic-gate * implemented. We use sigaction, as this sets up the
1717c478bd9Sstevel@tonic-gate * signal handler permanently, and also automatically
1727c478bd9Sstevel@tonic-gate * restarts any interrupted system call.
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate act.sa_handler = cmdabort;
1757c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1767c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART | SA_NODEFER;
177*b12aaafbSToomas Soome if (sigaction(SIGINT, &act, NULL) == -1) {
1787c478bd9Sstevel@tonic-gate err_print("sigaction(SIGINT) failed - %s\n",
179af007057Syl strerror(errno));
1807c478bd9Sstevel@tonic-gate fullabort();
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate act.sa_handler = onsusp;
1847c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1857c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART | SA_NODEFER;
186*b12aaafbSToomas Soome if (sigaction(SIGTSTP, &act, NULL) == -1) {
1877c478bd9Sstevel@tonic-gate err_print("sigaction(SIGTSTP) failed - %s\n",
188af007057Syl strerror(errno));
1897c478bd9Sstevel@tonic-gate fullabort();
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate act.sa_handler = onalarm;
1937c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1947c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART;
195*b12aaafbSToomas Soome if (sigaction(SIGALRM, &act, NULL) == -1) {
1967c478bd9Sstevel@tonic-gate err_print("sigaction(SIGALRM) failed - %s\n",
197af007057Syl strerror(errno));
1987c478bd9Sstevel@tonic-gate fullabort();
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate * If there was only 1 disk on the command line, mark it
2037c478bd9Sstevel@tonic-gate * to be the current disk. If it wasn't found, it's an error.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate if (i == argc - 1) {
2067c478bd9Sstevel@tonic-gate disk = disk_list;
2077c478bd9Sstevel@tonic-gate if (disk == NULL) {
2087c478bd9Sstevel@tonic-gate err_print("Unable to find specified disk '%s'.\n",
2097c478bd9Sstevel@tonic-gate argv[i]);
2107c478bd9Sstevel@tonic-gate fullabort();
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * A disk was forced on the command line.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate if (option_d) {
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate * Find it in the list of found disks and mark it to
2197c478bd9Sstevel@tonic-gate * be the current disk.
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate for (disk = disk_list; disk != NULL; disk = disk->disk_next)
2227c478bd9Sstevel@tonic-gate if (diskname_match(option_d, disk))
2237c478bd9Sstevel@tonic-gate break;
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error.
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate if (disk == NULL) {
2287c478bd9Sstevel@tonic-gate err_print("Unable to find specified disk '%s'.\n",
2297c478bd9Sstevel@tonic-gate option_d);
2307c478bd9Sstevel@tonic-gate fullabort();
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * A disk type was forced on the command line.
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate if (option_t != NULL) {
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * Only legal if a disk was also forced.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate if (disk == NULL) {
2417c478bd9Sstevel@tonic-gate err_print("Must specify disk as well as type.\n");
2427c478bd9Sstevel@tonic-gate fullabort();
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate oldtype = disk->disk_type;
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * Find the specified type in the list of legal types
2477c478bd9Sstevel@tonic-gate * for the disk.
2487c478bd9Sstevel@tonic-gate */
2497c478bd9Sstevel@tonic-gate for (type = disk->disk_ctlr->ctlr_ctype->ctype_dlist;
2507c478bd9Sstevel@tonic-gate type != NULL; type = type->dtype_next)
2517c478bd9Sstevel@tonic-gate if (strcmp(option_t, type->dtype_asciilabel) == 0)
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error.
2557c478bd9Sstevel@tonic-gate */
2567c478bd9Sstevel@tonic-gate if (type == NULL) {
2577c478bd9Sstevel@tonic-gate err_print(
2587c478bd9Sstevel@tonic-gate "Specified type '%s' is not a known type.\n", option_t);
2597c478bd9Sstevel@tonic-gate fullabort();
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate * If the specified type is not the same as the type
2637c478bd9Sstevel@tonic-gate * in the disk label, update the type and nullify the
2647c478bd9Sstevel@tonic-gate * partition map.
2657c478bd9Sstevel@tonic-gate */
2667c478bd9Sstevel@tonic-gate if (type != oldtype) {
2677c478bd9Sstevel@tonic-gate disk->disk_type = type;
2687c478bd9Sstevel@tonic-gate disk->disk_parts = NULL;
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate * A partition map was forced on the command line.
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate if (option_p) {
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate * Only legal if both disk and type were also forced.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate if (disk == NULL || disk->disk_type == NULL) {
2797c478bd9Sstevel@tonic-gate err_print("Must specify disk and type as well ");
2807c478bd9Sstevel@tonic-gate err_print("as partitiion.\n");
2817c478bd9Sstevel@tonic-gate fullabort();
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate * Find the specified map in the list of legal maps
2857c478bd9Sstevel@tonic-gate * for the type.
2867c478bd9Sstevel@tonic-gate */
2877c478bd9Sstevel@tonic-gate for (parts = disk->disk_type->dtype_plist; parts != NULL;
2887c478bd9Sstevel@tonic-gate parts = parts->pinfo_next)
2897c478bd9Sstevel@tonic-gate if (strcmp(option_p, parts->pinfo_name) == 0)
2907c478bd9Sstevel@tonic-gate break;
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error.
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate if (parts == NULL) {
2957c478bd9Sstevel@tonic-gate err_print(
2967c478bd9Sstevel@tonic-gate "Specified table '%s' is not a known table.\n", option_p);
2977c478bd9Sstevel@tonic-gate fullabort();
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * Update the map.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate disk->disk_parts = parts;
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * If a disk was marked to become current, initialize the state
3067c478bd9Sstevel@tonic-gate * to make it current. If not, ask user to pick one.
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate if (disk != NULL) {
3097c478bd9Sstevel@tonic-gate init_globals(disk);
3107c478bd9Sstevel@tonic-gate } else if (option_f == 0 && option_d == 0) {
3117c478bd9Sstevel@tonic-gate while (ret_code) {
3127c478bd9Sstevel@tonic-gate ret_code = c_disk();
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate #ifdef BUG1134748
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate * if -f command-file is specified, check for disk and disktype
3197c478bd9Sstevel@tonic-gate * input also. For SCSI disks, the type input may not be needed
3207c478bd9Sstevel@tonic-gate * since format would have figured that using inquiry information.
3217c478bd9Sstevel@tonic-gate */
3227c478bd9Sstevel@tonic-gate if (option_f) {
3237c478bd9Sstevel@tonic-gate if (cur_disk == NULL) {
3247c478bd9Sstevel@tonic-gate err_print("Must specify a disk using -d option.\n");
3257c478bd9Sstevel@tonic-gate fullabort();
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate if (cur_dtype == NULL) {
3287c478bd9Sstevel@tonic-gate err_print("Must specify disk as well as type.\n");
3297c478bd9Sstevel@tonic-gate fullabort();
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate #endif /* BUG1134748 */
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate * Run the command menu.
3367c478bd9Sstevel@tonic-gate */
3377c478bd9Sstevel@tonic-gate cur_menu = last_menu = 0;
3387c478bd9Sstevel@tonic-gate run_menu(menu_command, "FORMAT", "format", 1);
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /*
341052b6e8aSbg * normal ending. Explicitly return(0);
3427c478bd9Sstevel@tonic-gate */
343052b6e8aSbg return (0);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate * This routine initializes the internal state to ready it for a new
3487c478bd9Sstevel@tonic-gate * current disk. There are a zillion state variables that store
3497c478bd9Sstevel@tonic-gate * information on the current disk, and they must all be updated.
3507c478bd9Sstevel@tonic-gate * We also tell SunOS about the disk, since it may not know if the
3517c478bd9Sstevel@tonic-gate * disk wasn't labeled at boot time.
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate void
init_globals(struct disk_info * disk)354a11d78d6SToomas Soome init_globals(struct disk_info *disk)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate int status;
357af007057Syl int found_mount;
358af007057Syl int found_inuse;
3597c478bd9Sstevel@tonic-gate #ifdef sparc
3607c478bd9Sstevel@tonic-gate int i;
3617c478bd9Sstevel@tonic-gate caddr_t bad_ptr = (caddr_t)&badmap;
3627c478bd9Sstevel@tonic-gate #endif
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * If there was an old current disk, close the file for it.
3667c478bd9Sstevel@tonic-gate */
3677c478bd9Sstevel@tonic-gate if (cur_disk != NULL)
3687c478bd9Sstevel@tonic-gate (void) close(cur_file);
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate * Kill off any defect lists still lying around.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate kill_deflist(&cur_list);
3737c478bd9Sstevel@tonic-gate kill_deflist(&work_list);
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate * If there were any buffers, free them up.
3767c478bd9Sstevel@tonic-gate */
3777c478bd9Sstevel@tonic-gate if ((char *)cur_buf != NULL) {
3787c478bd9Sstevel@tonic-gate destroy_data((char *)cur_buf);
3797c478bd9Sstevel@tonic-gate cur_buf = NULL;
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate if ((char *)pattern_buf != NULL) {
3827c478bd9Sstevel@tonic-gate destroy_data((char *)pattern_buf);
3837c478bd9Sstevel@tonic-gate pattern_buf = NULL;
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate * Fill in the hardware struct pointers for the new disk.
3877c478bd9Sstevel@tonic-gate */
3887c478bd9Sstevel@tonic-gate cur_disk = disk;
3897c478bd9Sstevel@tonic-gate cur_dtype = cur_disk->disk_type;
3907c478bd9Sstevel@tonic-gate cur_label = cur_disk->label_type;
3917c478bd9Sstevel@tonic-gate cur_ctlr = cur_disk->disk_ctlr;
3927c478bd9Sstevel@tonic-gate cur_parts = cur_disk->disk_parts;
39365908c77Syu, larry liu - Sun Microsystems - Beijing China cur_blksz = cur_disk->disk_lbasize;
3947c478bd9Sstevel@tonic-gate cur_ctype = cur_ctlr->ctlr_ctype;
3957c478bd9Sstevel@tonic-gate cur_ops = cur_ctype->ctype_ops;
3967c478bd9Sstevel@tonic-gate cur_flags = 0;
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate * Open a file for the new disk.
3997c478bd9Sstevel@tonic-gate */
4007c478bd9Sstevel@tonic-gate if ((cur_file = open_disk(cur_disk->disk_path,
401a11d78d6SToomas Soome O_RDWR | O_NDELAY)) < 0) {
4027c478bd9Sstevel@tonic-gate err_print(
4037c478bd9Sstevel@tonic-gate "Error: can't open selected disk '%s'.\n", cur_disk->disk_name);
4047c478bd9Sstevel@tonic-gate fullabort();
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate #ifdef sparc
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * If the new disk uses bad-144, initialize the bad block table.
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate if (cur_ctlr->ctlr_flags & DKI_BAD144) {
4117c478bd9Sstevel@tonic-gate badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0;
4127c478bd9Sstevel@tonic-gate for (i = 0; i < NDKBAD; i++) {
4137c478bd9Sstevel@tonic-gate badmap.bt_bad[i].bt_cyl = -1;
4147c478bd9Sstevel@tonic-gate badmap.bt_bad[i].bt_trksec = -1;
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate #endif
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate * If the type of the new disk is known...
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate if (cur_dtype != NULL) {
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * Initialize the physical characteristics.
4247c478bd9Sstevel@tonic-gate * If need disk specs, prompt for undefined disk
4257c478bd9Sstevel@tonic-gate * characteristics. If running from a file,
4267c478bd9Sstevel@tonic-gate * use defaults.
4277c478bd9Sstevel@tonic-gate */
4287c478bd9Sstevel@tonic-gate if (cur_dtype->dtype_flags & DT_NEED_SPEFS) {
4297c478bd9Sstevel@tonic-gate get_disk_characteristics();
4307c478bd9Sstevel@tonic-gate cur_dtype->dtype_flags &= ~DT_NEED_SPEFS;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate ncyl = cur_dtype->dtype_ncyl;
4347c478bd9Sstevel@tonic-gate acyl = cur_dtype->dtype_acyl;
4357c478bd9Sstevel@tonic-gate pcyl = cur_dtype->dtype_pcyl;
4367c478bd9Sstevel@tonic-gate nhead = cur_dtype->dtype_nhead;
4377c478bd9Sstevel@tonic-gate nsect = cur_dtype->dtype_nsect;
4387c478bd9Sstevel@tonic-gate phead = cur_dtype->dtype_phead;
4397c478bd9Sstevel@tonic-gate psect = cur_dtype->dtype_psect;
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate * Alternates per cylinder are forced to 0 or 1,
4427c478bd9Sstevel@tonic-gate * independent of what the label says. This works
4437c478bd9Sstevel@tonic-gate * because we know which ctlr we are dealing with.
4447c478bd9Sstevel@tonic-gate */
4457c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_APC)
4467c478bd9Sstevel@tonic-gate apc = 1;
4477c478bd9Sstevel@tonic-gate else
4487c478bd9Sstevel@tonic-gate apc = 0;
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate * Initialize the surface analysis info. We always start
4517c478bd9Sstevel@tonic-gate * out with scan set for the whole disk. Note,
4527c478bd9Sstevel@tonic-gate * for SCSI disks, we can only scan the data area.
4537c478bd9Sstevel@tonic-gate */
4547c478bd9Sstevel@tonic-gate scan_lower = 0;
4557c478bd9Sstevel@tonic-gate scan_size = BUF_SECTS;
4567c478bd9Sstevel@tonic-gate if ((cur_ctype->ctype_flags & CF_SCSI) &&
4577c478bd9Sstevel@tonic-gate (cur_disk->label_type == L_TYPE_SOLARIS)) {
4587c478bd9Sstevel@tonic-gate scan_upper = datasects() - 1;
4597c478bd9Sstevel@tonic-gate } else if (cur_disk->label_type == L_TYPE_SOLARIS) {
4607c478bd9Sstevel@tonic-gate scan_upper = physsects() - 1;
4617c478bd9Sstevel@tonic-gate } else if (cur_disk->label_type == L_TYPE_EFI) {
4627c478bd9Sstevel@tonic-gate scan_upper = cur_parts->etoc->efi_last_lba;
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Allocate the buffers.
4677c478bd9Sstevel@tonic-gate */
46865908c77Syu, larry liu - Sun Microsystems - Beijing China cur_buf = (void *) zalloc(BUF_SECTS * cur_blksz);
46965908c77Syu, larry liu - Sun Microsystems - Beijing China pattern_buf = (void *) zalloc(BUF_SECTS * cur_blksz);
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate /*
47248bbca81SDaniel Hoffman * Tell the user which disk they selected.
4737c478bd9Sstevel@tonic-gate */
4747c478bd9Sstevel@tonic-gate if (chk_volname(cur_disk)) {
4757c478bd9Sstevel@tonic-gate fmt_print("selecting %s: ", cur_disk->disk_name);
4767c478bd9Sstevel@tonic-gate print_volname(cur_disk);
4777c478bd9Sstevel@tonic-gate fmt_print("\n");
4787c478bd9Sstevel@tonic-gate } else {
4797c478bd9Sstevel@tonic-gate fmt_print("selecting %s\n", cur_disk->disk_name);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate * If the drive is formatted...
4847c478bd9Sstevel@tonic-gate */
4857c478bd9Sstevel@tonic-gate if ((*cur_ops->op_ck_format)()) {
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate * Mark it formatted.
4887c478bd9Sstevel@tonic-gate */
4897c478bd9Sstevel@tonic-gate cur_flags |= DISK_FORMATTED;
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * Read the defect list, if we have one.
4927c478bd9Sstevel@tonic-gate */
4937c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) {
4947c478bd9Sstevel@tonic-gate read_list(&cur_list);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate #ifdef sparc
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate * If the disk does BAD-144, we do an ioctl to
4997c478bd9Sstevel@tonic-gate * tell SunOS about the bad block table.
5007c478bd9Sstevel@tonic-gate */
5017c478bd9Sstevel@tonic-gate if (cur_ctlr->ctlr_flags & DKI_BAD144) {
5027c478bd9Sstevel@tonic-gate if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr)) {
5037c478bd9Sstevel@tonic-gate err_print(
5047c478bd9Sstevel@tonic-gate "Warning: error telling SunOS bad block map table.\n");
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate #endif
5087c478bd9Sstevel@tonic-gate fmt_print("[disk formatted");
5097c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) {
5107c478bd9Sstevel@tonic-gate if (cur_list.list != NULL) {
5117c478bd9Sstevel@tonic-gate fmt_print(", defect list found");
5127c478bd9Sstevel@tonic-gate } else {
5137c478bd9Sstevel@tonic-gate fmt_print(", no defect list found");
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate fmt_print("]");
5177c478bd9Sstevel@tonic-gate /*
51848bbca81SDaniel Hoffman * Drive wasn't formatted. Tell the user in case they
519a141dbd6SAlison C * disagree.
5207c478bd9Sstevel@tonic-gate */
5217c478bd9Sstevel@tonic-gate } else if (EMBEDDED_SCSI) {
5227c478bd9Sstevel@tonic-gate fmt_print("[disk unformatted]");
5237c478bd9Sstevel@tonic-gate } else {
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * Make sure the user is serious. Note, for
5267c478bd9Sstevel@tonic-gate * SCSI disks since this is instantaneous, we
5277c478bd9Sstevel@tonic-gate * will just do it and not ask for confirmation.
5287c478bd9Sstevel@tonic-gate */
5297c478bd9Sstevel@tonic-gate status = 0;
5307c478bd9Sstevel@tonic-gate if (!(cur_ctype->ctype_flags & CF_CONFIRM)) {
5317c478bd9Sstevel@tonic-gate if (check("\n\
5327c478bd9Sstevel@tonic-gate Ready to get manufacturer's defect list from unformatted drive.\n\
5337c478bd9Sstevel@tonic-gate This cannot be interrupted and takes a long while.\n\
5347c478bd9Sstevel@tonic-gate Continue"))
5357c478bd9Sstevel@tonic-gate status = 1;
5367c478bd9Sstevel@tonic-gate else
5377c478bd9Sstevel@tonic-gate fmt_print(
5387c478bd9Sstevel@tonic-gate "Extracting manufacturer's defect list...");
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate * Extract manufacturer's defect list.
5427c478bd9Sstevel@tonic-gate */
5437c478bd9Sstevel@tonic-gate if ((status == 0) && (cur_ops->op_ex_man != NULL)) {
5447c478bd9Sstevel@tonic-gate status = (*cur_ops->op_ex_man)(&cur_list);
5457c478bd9Sstevel@tonic-gate } else {
5467c478bd9Sstevel@tonic-gate status = 1;
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate fmt_print("[disk unformatted");
5497c478bd9Sstevel@tonic-gate if (status != 0) {
5507c478bd9Sstevel@tonic-gate fmt_print(", no defect list found]");
5517c478bd9Sstevel@tonic-gate } else {
5527c478bd9Sstevel@tonic-gate fmt_print(", defect list found]");
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate } else {
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate * Disk type is not known.
5587c478bd9Sstevel@tonic-gate * Initialize physical characteristics to 0 and tell the
5597c478bd9Sstevel@tonic-gate * user we don't know what type the disk is.
5607c478bd9Sstevel@tonic-gate */
5617c478bd9Sstevel@tonic-gate ncyl = acyl = nhead = nsect = psect = 0;
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate fmt_print("\n");
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate * Check to see if there are any mounted file systems on the
5687c478bd9Sstevel@tonic-gate * disk. If there are, print a warning.
5697c478bd9Sstevel@tonic-gate */
570342440ecSPrasad Singamsetty if ((found_mount = checkmount((diskaddr_t)-1, (diskaddr_t)-1)) != 0)
5717c478bd9Sstevel@tonic-gate err_print("Warning: Current Disk has mounted partitions.\n");
5727c478bd9Sstevel@tonic-gate
5733e1bd7a2Ssjelinek /*
5743e1bd7a2Ssjelinek * If any part of this device is also part of an SVM, VxVM or
5753e1bd7a2Ssjelinek * Live Upgrade device, print a warning.
5763e1bd7a2Ssjelinek */
577af007057Syl found_inuse = checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
5783e1bd7a2Ssjelinek (diskaddr_t)-1, 1, 0);
5793e1bd7a2Ssjelinek
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * Get the Solaris Fdisk Partition information
5827c478bd9Sstevel@tonic-gate */
5837c478bd9Sstevel@tonic-gate (void) copy_solaris_part(&cur_disk->fdisk_part);
584af007057Syl
585af007057Syl if (!found_mount && !found_inuse &&
586af007057Syl cur_disk->label_type == L_TYPE_EFI) {
587af007057Syl
588af007057Syl /*
589af007057Syl * If alter_lba is 1, we are using the backup label.
590af007057Syl * Since we can locate the backup label by disk capacity,
591af007057Syl * there must be no space expanded after backup label.
592af007057Syl */
593af007057Syl if ((cur_parts->etoc->efi_altern_lba != 1) &&
594af007057Syl (cur_parts->etoc->efi_altern_lba <
595af007057Syl cur_parts->etoc->efi_last_lba)) {
596af007057Syl
597af007057Syl /*
598af007057Syl * Lun expansion detected. Prompt user now and actually
599af007057Syl * adjust the label in <partition> command.
600af007057Syl */
601af007057Syl fmt_print(
602af007057Syl "Note: capacity in disk label is smaller than the real disk capacity.\n\
603af007057Syl Select <partition> <expand> to adjust the label capacity. \n");
604af007057Syl }
605af007057Syl }
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate * Prompt for some undefined disk characteristics.
6117c478bd9Sstevel@tonic-gate * Used when there is no disk definition, but the
6127c478bd9Sstevel@tonic-gate * disk has a valid label, so basically we're
6137c478bd9Sstevel@tonic-gate * prompting for everything that isn't in the label.
6147c478bd9Sstevel@tonic-gate */
6157c478bd9Sstevel@tonic-gate static void
get_disk_characteristics(void)616*b12aaafbSToomas Soome get_disk_characteristics(void)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate * The need_spefs flag is used to tell us that this disk
6207c478bd9Sstevel@tonic-gate * is not a known type and the ctlr specific info must
6217c478bd9Sstevel@tonic-gate * be prompted for. We only prompt for the info that applies
6227c478bd9Sstevel@tonic-gate * to this ctlr.
6237c478bd9Sstevel@tonic-gate */
6247c478bd9Sstevel@tonic-gate assert(cur_dtype->dtype_flags & DT_NEED_SPEFS);
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate * If we're running with input from a file, use
6287c478bd9Sstevel@tonic-gate * reasonable defaults, since prompting for the
6297c478bd9Sstevel@tonic-gate * information will probably mess things up.
6307c478bd9Sstevel@tonic-gate */
6317c478bd9Sstevel@tonic-gate if (option_f) {
6327c478bd9Sstevel@tonic-gate cur_dtype->dtype_pcyl = ncyl + acyl;
6337c478bd9Sstevel@tonic-gate cur_dtype->dtype_rpm = AVG_RPM;
6347c478bd9Sstevel@tonic-gate cur_dtype->dtype_bpt = INFINITY;
6357c478bd9Sstevel@tonic-gate cur_dtype->dtype_phead = 0;
6367c478bd9Sstevel@tonic-gate cur_dtype->dtype_psect = 0;
6377c478bd9Sstevel@tonic-gate cur_dtype->dtype_cyl_skew = 0;
6387c478bd9Sstevel@tonic-gate cur_dtype->dtype_trk_skew = 0;
6397c478bd9Sstevel@tonic-gate cur_dtype->dtype_trks_zone = 0;
6407c478bd9Sstevel@tonic-gate cur_dtype->dtype_atrks = 0;
6417c478bd9Sstevel@tonic-gate cur_dtype->dtype_asect = 0;
6427c478bd9Sstevel@tonic-gate cur_dtype->dtype_cache = 0;
6437c478bd9Sstevel@tonic-gate cur_dtype->dtype_threshold = 0;
6447c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_min = 0;
6457c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_max = 0;
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_SMD_DEFS) {
6487c478bd9Sstevel@tonic-gate cur_dtype->dtype_bps = AVG_BPS;
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate } else {
6517c478bd9Sstevel@tonic-gate
6527c478bd9Sstevel@tonic-gate cur_dtype->dtype_pcyl = get_pcyl(ncyl, cur_dtype->dtype_acyl);
6537c478bd9Sstevel@tonic-gate cur_dtype->dtype_bpt = get_bpt(cur_dtype->dtype_nsect,
654af007057Syl &cur_dtype->dtype_options);
6557c478bd9Sstevel@tonic-gate cur_dtype->dtype_rpm = get_rpm();
6567c478bd9Sstevel@tonic-gate cur_dtype->dtype_fmt_time =
657af007057Syl get_fmt_time(&cur_dtype->dtype_options);
6587c478bd9Sstevel@tonic-gate cur_dtype->dtype_cyl_skew =
659af007057Syl get_cyl_skew(&cur_dtype->dtype_options);
6607c478bd9Sstevel@tonic-gate cur_dtype->dtype_trk_skew =
661af007057Syl get_trk_skew(&cur_dtype->dtype_options);
6627c478bd9Sstevel@tonic-gate cur_dtype->dtype_trks_zone =
663af007057Syl get_trks_zone(&cur_dtype->dtype_options);
6647c478bd9Sstevel@tonic-gate cur_dtype->dtype_atrks = get_atrks(&cur_dtype->dtype_options);
6657c478bd9Sstevel@tonic-gate cur_dtype->dtype_asect = get_asect(&cur_dtype->dtype_options);
6667c478bd9Sstevel@tonic-gate cur_dtype->dtype_cache = get_cache(&cur_dtype->dtype_options);
6677c478bd9Sstevel@tonic-gate cur_dtype->dtype_threshold =
668af007057Syl get_threshold(&cur_dtype->dtype_options);
6697c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_min =
670af007057Syl get_min_prefetch(&cur_dtype->dtype_options);
6717c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_max =
672af007057Syl get_max_prefetch(cur_dtype->dtype_prefetch_min,
673af007057Syl &cur_dtype->dtype_options);
6747c478bd9Sstevel@tonic-gate cur_dtype->dtype_phead =
675af007057Syl get_phead(nhead, &cur_dtype->dtype_options);
6767c478bd9Sstevel@tonic-gate cur_dtype->dtype_psect = get_psect(&cur_dtype->dtype_options);
6777c478bd9Sstevel@tonic-gate cur_dtype->dtype_bps = get_bps();
6787c478bd9Sstevel@tonic-gate #ifdef sparc
6797c478bd9Sstevel@tonic-gate cur_dtype->dtype_dr_type = 0;
6807c478bd9Sstevel@tonic-gate #endif
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate }
683