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
5de1f1563Slh195018 * Common Development and Distribution License (the "License").
6de1f1563Slh195018 * 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*65908c77Syu, larry liu - Sun Microsystems - Beijing China * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * This file contains the main entry point of the program and other
287c478bd9Sstevel@tonic-gate * routines relating to the general flow.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate #include "global.h"
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <memory.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #ifdef sparc
407c478bd9Sstevel@tonic-gate #include <sys/hdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/dkbad.h>
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include <sys/time.h>
457c478bd9Sstevel@tonic-gate #include "main.h"
467c478bd9Sstevel@tonic-gate #include "analyze.h"
477c478bd9Sstevel@tonic-gate #include "menu.h"
487c478bd9Sstevel@tonic-gate #include "param.h"
497c478bd9Sstevel@tonic-gate #include "misc.h"
507c478bd9Sstevel@tonic-gate #include "startup.h"
517c478bd9Sstevel@tonic-gate #include "menu_command.h"
527c478bd9Sstevel@tonic-gate #include "menu_partition.h"
537c478bd9Sstevel@tonic-gate #include "prompts.h"
543e1bd7a2Ssjelinek #include "checkdev.h"
557c478bd9Sstevel@tonic-gate #include "label.h"
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate extern struct menu_item menu_command[];
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #ifdef __STDC__
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * Local prototypes for ANSI C compilers
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate static void get_disk_characteristics(void);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate #else /* __STDC__ */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * Local prototypes for non-ANSI C compilers
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate static void get_disk_characteristics();
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate #endif /* __STDC__ */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * This is the main entry point.
787c478bd9Sstevel@tonic-gate */
79052b6e8aSbg159949 int
main(int argc,char * argv[])80052b6e8aSbg159949 main(int argc, char *argv[])
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate int i;
837c478bd9Sstevel@tonic-gate int ret_code = 1;
847c478bd9Sstevel@tonic-gate char **arglist;
857c478bd9Sstevel@tonic-gate struct disk_info *disk = NULL;
867c478bd9Sstevel@tonic-gate struct disk_type *type, *oldtype;
877c478bd9Sstevel@tonic-gate struct partition_info *parts;
887c478bd9Sstevel@tonic-gate struct sigaction act;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate solaris_offset = 0;
917c478bd9Sstevel@tonic-gate /*
92de1f1563Slh195018 * Initialize cur_ctype to avoid null pointer dereference
93de1f1563Slh195018 * in auto_efi_sense().
94de1f1563Slh195018 */
95de1f1563Slh195018 cur_ctype = (struct ctlr_type *)NULL;
96de1f1563Slh195018 /*
977c478bd9Sstevel@tonic-gate * Decode the command line options.
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate i = do_options(argc, argv);
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * If we are to run from a command file, open it up.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate if (option_f) {
1047c478bd9Sstevel@tonic-gate if (freopen(option_f, "r", stdin) == NULL) {
1057c478bd9Sstevel@tonic-gate err_print("Unable to open command file '%s'.\n",
1067c478bd9Sstevel@tonic-gate option_f);
1077c478bd9Sstevel@tonic-gate fullabort();
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * If we are logging, open the log file.
1127c478bd9Sstevel@tonic-gate */
1137c478bd9Sstevel@tonic-gate if (option_l) {
1147c478bd9Sstevel@tonic-gate if ((log_file = fopen(option_l, "w")) == NULL) {
1157c478bd9Sstevel@tonic-gate err_print("Unable to open log file '%s'.\n",
1167c478bd9Sstevel@tonic-gate option_l);
1177c478bd9Sstevel@tonic-gate fullabort();
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * Read in the data file and initialize the hardware structs.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate sup_init();
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * If there are no disks on the command line, search the
1267c478bd9Sstevel@tonic-gate * appropriate device directory for character devices that
1277c478bd9Sstevel@tonic-gate * look like disks.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate if (i < 0) {
1307c478bd9Sstevel@tonic-gate arglist = (char **)NULL;
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * There were disks on the command line. They comprise the
1337c478bd9Sstevel@tonic-gate * search list.
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate } else {
1367c478bd9Sstevel@tonic-gate arglist = &argv[i];
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate * Perform the search for disks.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate do_search(arglist);
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * Catch ctrl-C and ctrl-Z so critical sections can be
1447c478bd9Sstevel@tonic-gate * implemented. We use sigaction, as this sets up the
1457c478bd9Sstevel@tonic-gate * signal handler permanently, and also automatically
1467c478bd9Sstevel@tonic-gate * restarts any interrupted system call.
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate act.sa_handler = cmdabort;
1497c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1507c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART | SA_NODEFER;
1517c478bd9Sstevel@tonic-gate if (sigaction(SIGINT, &act, (struct sigaction *)NULL) == -1) {
1527c478bd9Sstevel@tonic-gate err_print("sigaction(SIGINT) failed - %s\n",
1537c478bd9Sstevel@tonic-gate strerror(errno));
1547c478bd9Sstevel@tonic-gate fullabort();
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate act.sa_handler = onsusp;
1587c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1597c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART | SA_NODEFER;
1607c478bd9Sstevel@tonic-gate if (sigaction(SIGTSTP, &act, (struct sigaction *)NULL) == -1) {
1617c478bd9Sstevel@tonic-gate err_print("sigaction(SIGTSTP) failed - %s\n",
1627c478bd9Sstevel@tonic-gate strerror(errno));
1637c478bd9Sstevel@tonic-gate fullabort();
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate act.sa_handler = onalarm;
1677c478bd9Sstevel@tonic-gate (void) memset(&act.sa_mask, 0, sizeof (sigset_t));
1687c478bd9Sstevel@tonic-gate act.sa_flags = SA_RESTART;
1697c478bd9Sstevel@tonic-gate if (sigaction(SIGALRM, &act, (struct sigaction *)NULL) == -1) {
1707c478bd9Sstevel@tonic-gate err_print("sigaction(SIGALRM) failed - %s\n",
1717c478bd9Sstevel@tonic-gate strerror(errno));
1727c478bd9Sstevel@tonic-gate fullabort();
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate * If there was only 1 disk on the command line, mark it
1777c478bd9Sstevel@tonic-gate * to be the current disk. If it wasn't found, it's an error.
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate if (i == argc - 1) {
1807c478bd9Sstevel@tonic-gate disk = disk_list;
1817c478bd9Sstevel@tonic-gate if (disk == NULL) {
1827c478bd9Sstevel@tonic-gate err_print("Unable to find specified disk '%s'.\n",
1837c478bd9Sstevel@tonic-gate argv[i]);
1847c478bd9Sstevel@tonic-gate fullabort();
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * A disk was forced on the command line.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate if (option_d) {
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Find it in the list of found disks and mark it to
1937c478bd9Sstevel@tonic-gate * be the current disk.
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate for (disk = disk_list; disk != NULL; disk = disk->disk_next)
1967c478bd9Sstevel@tonic-gate if (diskname_match(option_d, disk))
1977c478bd9Sstevel@tonic-gate break;
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate if (disk == NULL) {
2027c478bd9Sstevel@tonic-gate err_print("Unable to find specified disk '%s'.\n",
2037c478bd9Sstevel@tonic-gate option_d);
2047c478bd9Sstevel@tonic-gate fullabort();
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate * A disk type was forced on the command line.
2097c478bd9Sstevel@tonic-gate */
2107c478bd9Sstevel@tonic-gate if (option_t != NULL) {
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate * Only legal if a disk was also forced.
2137c478bd9Sstevel@tonic-gate */
2147c478bd9Sstevel@tonic-gate if (disk == NULL) {
2157c478bd9Sstevel@tonic-gate err_print("Must specify disk as well as type.\n");
2167c478bd9Sstevel@tonic-gate fullabort();
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate oldtype = disk->disk_type;
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate * Find the specified type in the list of legal types
2217c478bd9Sstevel@tonic-gate * for the disk.
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate for (type = disk->disk_ctlr->ctlr_ctype->ctype_dlist;
2247c478bd9Sstevel@tonic-gate type != NULL; type = type->dtype_next)
2257c478bd9Sstevel@tonic-gate if (strcmp(option_t, type->dtype_asciilabel) == 0)
2267c478bd9Sstevel@tonic-gate break;
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error.
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate if (type == NULL) {
2317c478bd9Sstevel@tonic-gate err_print(
2327c478bd9Sstevel@tonic-gate "Specified type '%s' is not a known type.\n", option_t);
2337c478bd9Sstevel@tonic-gate fullabort();
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate * If the specified type is not the same as the type
2377c478bd9Sstevel@tonic-gate * in the disk label, update the type and nullify the
2387c478bd9Sstevel@tonic-gate * partition map.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate if (type != oldtype) {
2417c478bd9Sstevel@tonic-gate disk->disk_type = type;
2427c478bd9Sstevel@tonic-gate disk->disk_parts = NULL;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate * A partition map was forced on the command line.
2477c478bd9Sstevel@tonic-gate */
2487c478bd9Sstevel@tonic-gate if (option_p) {
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate * Only legal if both disk and type were also forced.
2517c478bd9Sstevel@tonic-gate */
2527c478bd9Sstevel@tonic-gate if (disk == NULL || disk->disk_type == NULL) {
2537c478bd9Sstevel@tonic-gate err_print("Must specify disk and type as well ");
2547c478bd9Sstevel@tonic-gate err_print("as partitiion.\n");
2557c478bd9Sstevel@tonic-gate fullabort();
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate * Find the specified map in the list of legal maps
2597c478bd9Sstevel@tonic-gate * for the type.
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate for (parts = disk->disk_type->dtype_plist; parts != NULL;
2627c478bd9Sstevel@tonic-gate parts = parts->pinfo_next)
2637c478bd9Sstevel@tonic-gate if (strcmp(option_p, parts->pinfo_name) == 0)
2647c478bd9Sstevel@tonic-gate break;
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * If it wasn't found, it's an error.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate if (parts == NULL) {
2697c478bd9Sstevel@tonic-gate err_print(
2707c478bd9Sstevel@tonic-gate "Specified table '%s' is not a known table.\n", option_p);
2717c478bd9Sstevel@tonic-gate fullabort();
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * Update the map.
2757c478bd9Sstevel@tonic-gate */
2767c478bd9Sstevel@tonic-gate disk->disk_parts = parts;
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate * If a disk was marked to become current, initialize the state
2807c478bd9Sstevel@tonic-gate * to make it current. If not, ask user to pick one.
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate if (disk != NULL) {
2837c478bd9Sstevel@tonic-gate init_globals(disk);
2847c478bd9Sstevel@tonic-gate } else if (option_f == 0 && option_d == 0) {
2857c478bd9Sstevel@tonic-gate while (ret_code) {
2867c478bd9Sstevel@tonic-gate ret_code = c_disk();
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate #ifdef BUG1134748
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate * if -f command-file is specified, check for disk and disktype
2937c478bd9Sstevel@tonic-gate * input also. For SCSI disks, the type input may not be needed
2947c478bd9Sstevel@tonic-gate * since format would have figured that using inquiry information.
2957c478bd9Sstevel@tonic-gate */
2967c478bd9Sstevel@tonic-gate if (option_f) {
2977c478bd9Sstevel@tonic-gate if (cur_disk == NULL) {
2987c478bd9Sstevel@tonic-gate err_print("Must specify a disk using -d option.\n");
2997c478bd9Sstevel@tonic-gate fullabort();
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate if (cur_dtype == NULL) {
3027c478bd9Sstevel@tonic-gate err_print("Must specify disk as well as type.\n");
3037c478bd9Sstevel@tonic-gate fullabort();
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate #endif /* BUG1134748 */
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate * Run the command menu.
3107c478bd9Sstevel@tonic-gate */
3117c478bd9Sstevel@tonic-gate cur_menu = last_menu = 0;
3127c478bd9Sstevel@tonic-gate run_menu(menu_command, "FORMAT", "format", 1);
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate /*
315052b6e8aSbg159949 * normal ending. Explicitly return(0);
3167c478bd9Sstevel@tonic-gate */
317052b6e8aSbg159949 return (0);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate * This routine initializes the internal state to ready it for a new
3227c478bd9Sstevel@tonic-gate * current disk. There are a zillion state variables that store
3237c478bd9Sstevel@tonic-gate * information on the current disk, and they must all be updated.
3247c478bd9Sstevel@tonic-gate * We also tell SunOS about the disk, since it may not know if the
3257c478bd9Sstevel@tonic-gate * disk wasn't labeled at boot time.
3267c478bd9Sstevel@tonic-gate */
3277c478bd9Sstevel@tonic-gate void
init_globals(disk)3287c478bd9Sstevel@tonic-gate init_globals(disk)
3297c478bd9Sstevel@tonic-gate struct disk_info *disk;
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate int status;
332af007057Syl194034 int found_mount;
333af007057Syl194034 int found_inuse;
3347c478bd9Sstevel@tonic-gate #ifdef sparc
3357c478bd9Sstevel@tonic-gate int i;
3367c478bd9Sstevel@tonic-gate caddr_t bad_ptr = (caddr_t)&badmap;
3377c478bd9Sstevel@tonic-gate #endif
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate * If there was an old current disk, close the file for it.
3417c478bd9Sstevel@tonic-gate */
3427c478bd9Sstevel@tonic-gate if (cur_disk != NULL)
3437c478bd9Sstevel@tonic-gate (void) close(cur_file);
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * Kill off any defect lists still lying around.
3467c478bd9Sstevel@tonic-gate */
3477c478bd9Sstevel@tonic-gate kill_deflist(&cur_list);
3487c478bd9Sstevel@tonic-gate kill_deflist(&work_list);
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate * If there were any buffers, free them up.
3517c478bd9Sstevel@tonic-gate */
3527c478bd9Sstevel@tonic-gate if ((char *)cur_buf != NULL) {
3537c478bd9Sstevel@tonic-gate destroy_data((char *)cur_buf);
3547c478bd9Sstevel@tonic-gate cur_buf = NULL;
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate if ((char *)pattern_buf != NULL) {
3577c478bd9Sstevel@tonic-gate destroy_data((char *)pattern_buf);
3587c478bd9Sstevel@tonic-gate pattern_buf = NULL;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate * Fill in the hardware struct pointers for the new disk.
3627c478bd9Sstevel@tonic-gate */
3637c478bd9Sstevel@tonic-gate cur_disk = disk;
3647c478bd9Sstevel@tonic-gate cur_dtype = cur_disk->disk_type;
3657c478bd9Sstevel@tonic-gate cur_label = cur_disk->label_type;
3667c478bd9Sstevel@tonic-gate cur_ctlr = cur_disk->disk_ctlr;
3677c478bd9Sstevel@tonic-gate cur_parts = cur_disk->disk_parts;
368*65908c77Syu, larry liu - Sun Microsystems - Beijing China cur_blksz = cur_disk->disk_lbasize;
3697c478bd9Sstevel@tonic-gate cur_ctype = cur_ctlr->ctlr_ctype;
3707c478bd9Sstevel@tonic-gate cur_ops = cur_ctype->ctype_ops;
3717c478bd9Sstevel@tonic-gate cur_flags = 0;
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate * Open a file for the new disk.
3747c478bd9Sstevel@tonic-gate */
3757c478bd9Sstevel@tonic-gate if ((cur_file = open_disk(cur_disk->disk_path,
3767c478bd9Sstevel@tonic-gate O_RDWR | O_NDELAY)) < 0) {
3777c478bd9Sstevel@tonic-gate err_print(
3787c478bd9Sstevel@tonic-gate "Error: can't open selected disk '%s'.\n", cur_disk->disk_name);
3797c478bd9Sstevel@tonic-gate fullabort();
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate #ifdef sparc
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * If the new disk uses bad-144, initialize the bad block table.
3847c478bd9Sstevel@tonic-gate */
3857c478bd9Sstevel@tonic-gate if (cur_ctlr->ctlr_flags & DKI_BAD144) {
3867c478bd9Sstevel@tonic-gate badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0;
3877c478bd9Sstevel@tonic-gate for (i = 0; i < NDKBAD; i++) {
3887c478bd9Sstevel@tonic-gate badmap.bt_bad[i].bt_cyl = -1;
3897c478bd9Sstevel@tonic-gate badmap.bt_bad[i].bt_trksec = -1;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate #endif
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * If the type of the new disk is known...
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate if (cur_dtype != NULL) {
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate * Initialize the physical characteristics.
3997c478bd9Sstevel@tonic-gate * If need disk specs, prompt for undefined disk
4007c478bd9Sstevel@tonic-gate * characteristics. If running from a file,
4017c478bd9Sstevel@tonic-gate * use defaults.
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate if (cur_dtype->dtype_flags & DT_NEED_SPEFS) {
4047c478bd9Sstevel@tonic-gate get_disk_characteristics();
4057c478bd9Sstevel@tonic-gate cur_dtype->dtype_flags &= ~DT_NEED_SPEFS;
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate ncyl = cur_dtype->dtype_ncyl;
4097c478bd9Sstevel@tonic-gate acyl = cur_dtype->dtype_acyl;
4107c478bd9Sstevel@tonic-gate pcyl = cur_dtype->dtype_pcyl;
4117c478bd9Sstevel@tonic-gate nhead = cur_dtype->dtype_nhead;
4127c478bd9Sstevel@tonic-gate nsect = cur_dtype->dtype_nsect;
4137c478bd9Sstevel@tonic-gate phead = cur_dtype->dtype_phead;
4147c478bd9Sstevel@tonic-gate psect = cur_dtype->dtype_psect;
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate * Alternates per cylinder are forced to 0 or 1,
4177c478bd9Sstevel@tonic-gate * independent of what the label says. This works
4187c478bd9Sstevel@tonic-gate * because we know which ctlr we are dealing with.
4197c478bd9Sstevel@tonic-gate */
4207c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_APC)
4217c478bd9Sstevel@tonic-gate apc = 1;
4227c478bd9Sstevel@tonic-gate else
4237c478bd9Sstevel@tonic-gate apc = 0;
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate * Initialize the surface analysis info. We always start
4267c478bd9Sstevel@tonic-gate * out with scan set for the whole disk. Note,
4277c478bd9Sstevel@tonic-gate * for SCSI disks, we can only scan the data area.
4287c478bd9Sstevel@tonic-gate */
4297c478bd9Sstevel@tonic-gate scan_lower = 0;
4307c478bd9Sstevel@tonic-gate scan_size = BUF_SECTS;
4317c478bd9Sstevel@tonic-gate if ((cur_ctype->ctype_flags & CF_SCSI) &&
4327c478bd9Sstevel@tonic-gate (cur_disk->label_type == L_TYPE_SOLARIS)) {
4337c478bd9Sstevel@tonic-gate scan_upper = datasects() - 1;
4347c478bd9Sstevel@tonic-gate } else if (cur_disk->label_type == L_TYPE_SOLARIS) {
4357c478bd9Sstevel@tonic-gate scan_upper = physsects() - 1;
4367c478bd9Sstevel@tonic-gate } else if (cur_disk->label_type == L_TYPE_EFI) {
4377c478bd9Sstevel@tonic-gate scan_upper = cur_parts->etoc->efi_last_lba;
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate * Allocate the buffers.
4427c478bd9Sstevel@tonic-gate */
443*65908c77Syu, larry liu - Sun Microsystems - Beijing China cur_buf = (void *) zalloc(BUF_SECTS * cur_blksz);
444*65908c77Syu, larry liu - Sun Microsystems - Beijing China pattern_buf = (void *) zalloc(BUF_SECTS * cur_blksz);
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate * Tell the user which disk (s)he selected.
4487c478bd9Sstevel@tonic-gate */
4497c478bd9Sstevel@tonic-gate if (chk_volname(cur_disk)) {
4507c478bd9Sstevel@tonic-gate fmt_print("selecting %s: ", cur_disk->disk_name);
4517c478bd9Sstevel@tonic-gate print_volname(cur_disk);
4527c478bd9Sstevel@tonic-gate fmt_print("\n");
4537c478bd9Sstevel@tonic-gate } else {
4547c478bd9Sstevel@tonic-gate fmt_print("selecting %s\n", cur_disk->disk_name);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate * If the drive is formatted...
4597c478bd9Sstevel@tonic-gate */
4607c478bd9Sstevel@tonic-gate if ((*cur_ops->op_ck_format)()) {
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate * Mark it formatted.
4637c478bd9Sstevel@tonic-gate */
4647c478bd9Sstevel@tonic-gate cur_flags |= DISK_FORMATTED;
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Read the defect list, if we have one.
4677c478bd9Sstevel@tonic-gate */
4687c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) {
4697c478bd9Sstevel@tonic-gate read_list(&cur_list);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate #ifdef sparc
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate * If the disk does BAD-144, we do an ioctl to
4747c478bd9Sstevel@tonic-gate * tell SunOS about the bad block table.
4757c478bd9Sstevel@tonic-gate */
4767c478bd9Sstevel@tonic-gate if (cur_ctlr->ctlr_flags & DKI_BAD144) {
4777c478bd9Sstevel@tonic-gate if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr)) {
4787c478bd9Sstevel@tonic-gate err_print(
4797c478bd9Sstevel@tonic-gate "Warning: error telling SunOS bad block map table.\n");
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate #endif
4837c478bd9Sstevel@tonic-gate fmt_print("[disk formatted");
4847c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI) {
4857c478bd9Sstevel@tonic-gate if (cur_list.list != NULL) {
4867c478bd9Sstevel@tonic-gate fmt_print(", defect list found");
4877c478bd9Sstevel@tonic-gate } else {
4887c478bd9Sstevel@tonic-gate fmt_print(", no defect list found");
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate fmt_print("]");
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate * Drive wasn't formatted. Tell the user in case he
4947c478bd9Sstevel@tonic-gate * disagrees.
4957c478bd9Sstevel@tonic-gate */
4967c478bd9Sstevel@tonic-gate } else if (EMBEDDED_SCSI) {
4977c478bd9Sstevel@tonic-gate fmt_print("[disk unformatted]");
4987c478bd9Sstevel@tonic-gate } else {
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate * Make sure the user is serious. Note, for
5017c478bd9Sstevel@tonic-gate * SCSI disks since this is instantaneous, we
5027c478bd9Sstevel@tonic-gate * will just do it and not ask for confirmation.
5037c478bd9Sstevel@tonic-gate */
5047c478bd9Sstevel@tonic-gate status = 0;
5057c478bd9Sstevel@tonic-gate if (!(cur_ctype->ctype_flags & CF_CONFIRM)) {
5067c478bd9Sstevel@tonic-gate if (check("\n\
5077c478bd9Sstevel@tonic-gate Ready to get manufacturer's defect list from unformatted drive.\n\
5087c478bd9Sstevel@tonic-gate This cannot be interrupted and takes a long while.\n\
5097c478bd9Sstevel@tonic-gate Continue"))
5107c478bd9Sstevel@tonic-gate status = 1;
5117c478bd9Sstevel@tonic-gate else
5127c478bd9Sstevel@tonic-gate fmt_print(
5137c478bd9Sstevel@tonic-gate "Extracting manufacturer's defect list...");
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate * Extract manufacturer's defect list.
5177c478bd9Sstevel@tonic-gate */
5187c478bd9Sstevel@tonic-gate if ((status == 0) && (cur_ops->op_ex_man != NULL)) {
5197c478bd9Sstevel@tonic-gate status = (*cur_ops->op_ex_man)(&cur_list);
5207c478bd9Sstevel@tonic-gate } else {
5217c478bd9Sstevel@tonic-gate status = 1;
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate fmt_print("[disk unformatted");
5247c478bd9Sstevel@tonic-gate if (status != 0) {
5257c478bd9Sstevel@tonic-gate fmt_print(", no defect list found]");
5267c478bd9Sstevel@tonic-gate } else {
5277c478bd9Sstevel@tonic-gate fmt_print(", defect list found]");
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate } else {
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate * Disk type is not known.
5337c478bd9Sstevel@tonic-gate * Initialize physical characteristics to 0 and tell the
5347c478bd9Sstevel@tonic-gate * user we don't know what type the disk is.
5357c478bd9Sstevel@tonic-gate */
5367c478bd9Sstevel@tonic-gate ncyl = acyl = nhead = nsect = psect = 0;
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate fmt_print("\n");
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate * Check to see if there are any mounted file systems on the
5437c478bd9Sstevel@tonic-gate * disk. If there are, print a warning.
5447c478bd9Sstevel@tonic-gate */
545342440ecSPrasad Singamsetty if ((found_mount = checkmount((diskaddr_t)-1, (diskaddr_t)-1)) != 0)
5467c478bd9Sstevel@tonic-gate err_print("Warning: Current Disk has mounted partitions.\n");
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate /*
5493e1bd7a2Ssjelinek * If any part of this device is also part of an SVM, VxVM or
5503e1bd7a2Ssjelinek * Live Upgrade device, print a warning.
5513e1bd7a2Ssjelinek */
552af007057Syl194034 found_inuse = checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
5533e1bd7a2Ssjelinek (diskaddr_t)-1, 1, 0);
5543e1bd7a2Ssjelinek
5553e1bd7a2Ssjelinek /*
5567c478bd9Sstevel@tonic-gate * Get the Solaris Fdisk Partition information
5577c478bd9Sstevel@tonic-gate */
5587c478bd9Sstevel@tonic-gate (void) copy_solaris_part(&cur_disk->fdisk_part);
559af007057Syl194034
560af007057Syl194034 if (!found_mount && !found_inuse &&
561af007057Syl194034 cur_disk->label_type == L_TYPE_EFI) {
562af007057Syl194034
563af007057Syl194034 /*
564af007057Syl194034 * If alter_lba is 1, we are using the backup label.
565af007057Syl194034 * Since we can locate the backup label by disk capacity,
566af007057Syl194034 * there must be no space expanded after backup label.
567af007057Syl194034 */
568af007057Syl194034 if ((cur_parts->etoc->efi_altern_lba != 1) &&
569af007057Syl194034 (cur_parts->etoc->efi_altern_lba <
570af007057Syl194034 cur_parts->etoc->efi_last_lba)) {
571af007057Syl194034
572af007057Syl194034 /*
573af007057Syl194034 * Lun expansion detected. Prompt user now and actually
574af007057Syl194034 * adjust the label in <partition> command.
575af007057Syl194034 */
576af007057Syl194034 fmt_print(
577af007057Syl194034 "Note: capacity in disk label is smaller than the real disk capacity.\n\
578af007057Syl194034 Select <partition> <expand> to adjust the label capacity. \n");
579af007057Syl194034 }
580af007057Syl194034 }
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate * Prompt for some undefined disk characteristics.
5867c478bd9Sstevel@tonic-gate * Used when there is no disk definition, but the
5877c478bd9Sstevel@tonic-gate * disk has a valid label, so basically we're
5887c478bd9Sstevel@tonic-gate * prompting for everything that isn't in the label.
5897c478bd9Sstevel@tonic-gate */
5907c478bd9Sstevel@tonic-gate static void
get_disk_characteristics()5917c478bd9Sstevel@tonic-gate get_disk_characteristics()
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate /*
5947c478bd9Sstevel@tonic-gate * The need_spefs flag is used to tell us that this disk
5957c478bd9Sstevel@tonic-gate * is not a known type and the ctlr specific info must
5967c478bd9Sstevel@tonic-gate * be prompted for. We only prompt for the info that applies
5977c478bd9Sstevel@tonic-gate * to this ctlr.
5987c478bd9Sstevel@tonic-gate */
5997c478bd9Sstevel@tonic-gate assert(cur_dtype->dtype_flags & DT_NEED_SPEFS);
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate * If we're running with input from a file, use
6037c478bd9Sstevel@tonic-gate * reasonable defaults, since prompting for the
6047c478bd9Sstevel@tonic-gate * information will probably mess things up.
6057c478bd9Sstevel@tonic-gate */
6067c478bd9Sstevel@tonic-gate if (option_f) {
6077c478bd9Sstevel@tonic-gate cur_dtype->dtype_pcyl = ncyl + acyl;
6087c478bd9Sstevel@tonic-gate cur_dtype->dtype_rpm = AVG_RPM;
6097c478bd9Sstevel@tonic-gate cur_dtype->dtype_bpt = INFINITY;
6107c478bd9Sstevel@tonic-gate cur_dtype->dtype_phead = 0;
6117c478bd9Sstevel@tonic-gate cur_dtype->dtype_psect = 0;
6127c478bd9Sstevel@tonic-gate cur_dtype->dtype_cyl_skew = 0;
6137c478bd9Sstevel@tonic-gate cur_dtype->dtype_trk_skew = 0;
6147c478bd9Sstevel@tonic-gate cur_dtype->dtype_trks_zone = 0;
6157c478bd9Sstevel@tonic-gate cur_dtype->dtype_atrks = 0;
6167c478bd9Sstevel@tonic-gate cur_dtype->dtype_asect = 0;
6177c478bd9Sstevel@tonic-gate cur_dtype->dtype_cache = 0;
6187c478bd9Sstevel@tonic-gate cur_dtype->dtype_threshold = 0;
6197c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_min = 0;
6207c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_max = 0;
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_SMD_DEFS) {
6237c478bd9Sstevel@tonic-gate cur_dtype->dtype_bps = AVG_BPS;
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate } else {
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate cur_dtype->dtype_pcyl = get_pcyl(ncyl, cur_dtype->dtype_acyl);
6287c478bd9Sstevel@tonic-gate cur_dtype->dtype_bpt = get_bpt(cur_dtype->dtype_nsect,
6297c478bd9Sstevel@tonic-gate &cur_dtype->dtype_options);
6307c478bd9Sstevel@tonic-gate cur_dtype->dtype_rpm = get_rpm();
6317c478bd9Sstevel@tonic-gate cur_dtype->dtype_fmt_time =
6327c478bd9Sstevel@tonic-gate get_fmt_time(&cur_dtype->dtype_options);
6337c478bd9Sstevel@tonic-gate cur_dtype->dtype_cyl_skew =
6347c478bd9Sstevel@tonic-gate get_cyl_skew(&cur_dtype->dtype_options);
6357c478bd9Sstevel@tonic-gate cur_dtype->dtype_trk_skew =
6367c478bd9Sstevel@tonic-gate get_trk_skew(&cur_dtype->dtype_options);
6377c478bd9Sstevel@tonic-gate cur_dtype->dtype_trks_zone =
6387c478bd9Sstevel@tonic-gate get_trks_zone(&cur_dtype->dtype_options);
6397c478bd9Sstevel@tonic-gate cur_dtype->dtype_atrks = get_atrks(&cur_dtype->dtype_options);
6407c478bd9Sstevel@tonic-gate cur_dtype->dtype_asect = get_asect(&cur_dtype->dtype_options);
6417c478bd9Sstevel@tonic-gate cur_dtype->dtype_cache = get_cache(&cur_dtype->dtype_options);
6427c478bd9Sstevel@tonic-gate cur_dtype->dtype_threshold =
6437c478bd9Sstevel@tonic-gate get_threshold(&cur_dtype->dtype_options);
6447c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_min =
6457c478bd9Sstevel@tonic-gate get_min_prefetch(&cur_dtype->dtype_options);
6467c478bd9Sstevel@tonic-gate cur_dtype->dtype_prefetch_max =
6477c478bd9Sstevel@tonic-gate get_max_prefetch(cur_dtype->dtype_prefetch_min,
6487c478bd9Sstevel@tonic-gate &cur_dtype->dtype_options);
6497c478bd9Sstevel@tonic-gate cur_dtype->dtype_phead =
6507c478bd9Sstevel@tonic-gate get_phead(nhead, &cur_dtype->dtype_options);
6517c478bd9Sstevel@tonic-gate cur_dtype->dtype_psect = get_psect(&cur_dtype->dtype_options);
6527c478bd9Sstevel@tonic-gate cur_dtype->dtype_bps = get_bps();
6537c478bd9Sstevel@tonic-gate #ifdef sparc
6547c478bd9Sstevel@tonic-gate cur_dtype->dtype_dr_type = 0;
6557c478bd9Sstevel@tonic-gate #endif
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate }
658