xref: /titanic_50/usr/src/cmd/format/menu_command.c (revision 42516a0c6ebf6e259c2abcd1ca315fec43268f39)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file contains functions that implement the command menu commands.
28  */
29 
30 #include "global.h"
31 #include <time.h>
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <sys/wait.h>
35 #include <strings.h>
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #if defined(sparc)
41 #include <sys/hdio.h>
42 #endif /* defined(sparc) */
43 
44 #include "main.h"
45 #include "analyze.h"
46 #include "menu.h"
47 #include "menu_command.h"
48 #include "menu_defect.h"
49 #include "menu_partition.h"
50 #include "param.h"
51 #include "misc.h"
52 #include "label.h"
53 #include "startup.h"
54 #include "partition.h"
55 #include "prompts.h"
56 #include "checkdev.h"
57 #include "io.h"
58 #include "ctlr_scsi.h"
59 #include "auto_sense.h"
60 #include "modify_partition.h"
61 
62 
63 extern	struct menu_item menu_partition[];
64 extern	struct menu_item menu_analyze[];
65 extern	struct menu_item menu_defect[];
66 
67 /*
68  * Choices for the p_tag vtoc field
69  */
70 slist_t	ptag_choices[] = {
71 	{ "unassigned",	"",	V_UNASSIGNED	},
72 	{ "boot",	"",	V_BOOT		},
73 	{ "root",	"",	V_ROOT		},
74 	{ "swap",	"",	V_SWAP		},
75 	{ "usr",	"",	V_USR		},
76 	{ "backup",	"",	V_BACKUP	},
77 	{ "stand",	"",	V_STAND		},
78 	{ "var",	"",	V_VAR		},
79 	{ "home",	"",	V_HOME		},
80 	{ "alternates",	"",	V_ALTSCTR	},
81 	{ "reserved",	"",	V_RESERVED	},
82 	{ NULL }
83 };
84 
85 
86 /*
87  * Choices for the p_flag vtoc field
88  */
89 slist_t	pflag_choices[] = {
90 	{ "wm",	"read-write, mountable",	0		},
91 	{ "wu",	"read-write, unmountable",	V_UNMNT		},
92 	{ "rm",	"read-only, mountable",		V_RONLY		},
93 	{ "ru",	"read-only, unmountable",	V_RONLY|V_UNMNT	},
94 	{ NULL }
95 };
96 
97 
98 /*
99  * This routine implements the 'disk' command.  It allows the user to
100  * select a disk to be current.  The list of choices is the list of
101  * disks that were found at startup time.
102  */
103 int
104 c_disk()
105 {
106 	struct disk_info	*disk;
107 	u_ioparam_t		ioparam;
108 	int			i;
109 	int			ndisks = 0;
110 	int			blind_select = 0;
111 	int			deflt;
112 	int			index;
113 	int			*defltptr = NULL;
114 	int			more = 0;
115 	int			more_quit = 0;
116 	int			one_line = 0;
117 	int			tty_lines;
118 
119 /*
120  * This buffer holds the check() prompt that verifies we've got the right
121  * disk when performing a blind selection.  The size should be sufficient
122  * to hold the prompt string, plus 256 characters for the disk name -
123  * way more than should ever be necessary.  See the #define in misc.h.
124  */
125 	char			chk_buf[BLIND_SELECT_VER_PROMPT];
126 
127 	if (istokenpresent()) {
128 		/*
129 		 * disk number to be selected is already in the
130 		 * input stream .
131 		 */
132 		TOKEN token, cleantoken;
133 
134 		/*
135 		 * Get the disk number the user has given.
136 		 */
137 		i = 0;
138 		for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
139 			i++;
140 		}
141 
142 		ioparam.io_bounds.lower = 0;
143 		ioparam.io_bounds.upper = i - 1;
144 		(void) gettoken(token);
145 		clean_token(cleantoken, token);
146 
147 		/*
148 		 * Convert the token into an integer.
149 		 */
150 		if (geti(cleantoken, (int *)&index, (int *)NULL))
151 			return (0);
152 
153 		/*
154 		 * Check to be sure it is within the legal bounds.
155 		 */
156 		if ((index < 0) || (index >= i)) {
157 			err_print("`%d' is out of range.\n", index);
158 			return (0);
159 		}
160 		goto checkdisk;
161 	}
162 
163 	fmt_print("\n\nAVAILABLE DISK SELECTIONS:\n");
164 
165 	i = 0;
166 	if ((option_f == (char *)NULL) && isatty(0) == 1 && isatty(1) == 1) {
167 		/*
168 		 * We have a real terminal for std input and output, enable
169 		 * more style of output for disk selection list.
170 		 */
171 		more = 1;
172 		tty_lines = get_tty_lines();
173 		enter_critical();
174 		echo_off();
175 		charmode_on();
176 		exit_critical();
177 	}
178 
179 	/*
180 	 * Loop through the list of found disks.
181 	 */
182 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
183 		/*
184 		 * If using more output, account 2 lines for each disk.
185 		 */
186 		if (more && !more_quit && i && (one_line ||
187 		    ((2 * i + 1) % (tty_lines - 2) <= 1))) {
188 			int	c;
189 
190 			/*
191 			 * Get the next character.
192 			 */
193 			fmt_print("- hit space for more or s to select - ");
194 			c = getchar();
195 			fmt_print("\015");
196 			one_line = 0;
197 			/*
198 			 * Handle display one line command
199 			 * (return key)
200 			 */
201 			if (c == '\012') {
202 				one_line++;
203 			}
204 			/* Handle Quit command */
205 			if (c == 'q') {
206 				fmt_print(
207 				"                       \015");
208 				more_quit++;
209 			}
210 			/* Handle ^D command */
211 			if (c == '\004')
212 				fullabort();
213 			/* or get on with the show */
214 			if (c == 's' || c == 'S') {
215 				fmt_print("%80s\n", " ");
216 				break;
217 			}
218 		}
219 		/*
220 		 * If this is the current disk, mark it as
221 		 * the default.
222 		 */
223 		if (cur_disk == disk) {
224 			deflt = i;
225 			defltptr = &deflt;
226 		}
227 		if (!more || !more_quit)
228 			pr_diskline(disk, i);
229 		i++;
230 	}
231 	if (more) {
232 		enter_critical();
233 		charmode_off();
234 		echo_on();
235 		exit_critical();
236 	}
237 
238 	/*
239 	 * Determine total number of disks, and ask the user which disk he
240 	 * would like to make current.
241 	 */
242 
243 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
244 		ndisks++;
245 	}
246 
247 	ioparam.io_bounds.lower = 0;
248 	ioparam.io_bounds.upper = ndisks - 1;
249 	index = input(FIO_INT, "Specify disk (enter its number)", ':',
250 	    &ioparam, defltptr, DATA_INPUT);
251 
252 	if (index >= i) {
253 		blind_select = 1;
254 	}
255 
256 	/*
257 	 * Find the disk chosen.  Search through controllers/disks
258 	 * in the same original order, so we match what the user
259 	 * chose.
260 	 */
261 checkdisk:
262 	i = 0;
263 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
264 		if (i == index)
265 			goto found;
266 		i++;
267 	}
268 	/*
269 	 * Should never happen.
270 	 */
271 	impossible("no disk found");
272 
273 found:
274 	if (blind_select) {
275 		(void) snprintf(chk_buf, sizeof (chk_buf),
276 "Disk %s selected - is this the desired disk? ", disk->disk_name);
277 		if (check(chk_buf)) {
278 			return (-1);
279 		}
280 	}
281 
282 	/*
283 	 * Update the state.  We lock out interrupts so the state can't
284 	 * get half-updated.
285 	 */
286 
287 	enter_critical();
288 	init_globals(disk);
289 	exit_critical();
290 
291 	/*
292 	 * If type unknown and interactive, ask user to specify type.
293 	 * Also, set partition table (best guess) too.
294 	 */
295 	if (!option_f && ncyl == 0 && nhead == 0 && nsect == 0 &&
296 		(disk->label_type != L_TYPE_EFI)) {
297 		    (void) c_type();
298 	}
299 
300 	/*
301 	 * Get the Solaris Fdisk Partition information
302 	 */
303 	if (nhead != 0 && nsect != 0)
304 		(void) copy_solaris_part(&cur_disk->fdisk_part);
305 
306 	if ((cur_disk->label_type == L_TYPE_EFI) &&
307 	    (cur_disk->disk_parts->etoc->efi_flags &
308 		EFI_GPT_PRIMARY_CORRUPT)) {
309 		    err_print("Reading the primary EFI GPT label ");
310 		    err_print("failed.  Using backup label.\n");
311 		    err_print("Use the 'backup' command to restore ");
312 		    err_print("the primary label.\n");
313 	}
314 	/*
315 	 * If the label of the disk is marked dirty,
316 	 * see if they'd like to label the disk now.
317 	 */
318 	if (cur_disk->disk_flags & DSK_LABEL_DIRTY) {
319 		if (check("Disk not labeled.  Label it now") == 0) {
320 			if (write_label()) {
321 				err_print("Write label failed\n");
322 			} else {
323 				cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
324 			}
325 		}
326 	}
327 	return (0);
328 }
329 
330 /*
331  * This routine implements the 'type' command.  It allows the user to
332  * specify the type of the current disk.  It should be necessary only
333  * if the disk was not labelled or was somehow labelled incorrectly.
334  * The list of legal types for the disk comes from information that was
335  * in the data file.
336  */
337 int
338 c_type()
339 {
340 	struct disk_type	*type, *tptr, *oldtype;
341 	u_ioparam_t		ioparam;
342 	int			i, index, deflt, *defltptr = NULL;
343 	struct disk_type	disk_type;
344 	struct disk_type	*d = &disk_type;
345 	int			first_disk;
346 	int			auto_conf_choice;
347 	int			other_choice;
348 	struct dk_label		label;
349 	struct efi_info		efi_info;
350 	uint64_t		maxLBA;
351 	char			volname[LEN_DKL_VVOL];
352 	int			volinit = 0;
353 
354 	/*
355 	 * There must be a current disk.
356 	 */
357 	if (cur_disk == NULL) {
358 		err_print("Current Disk is not set.\n");
359 		return (-1);
360 	}
361 	oldtype = cur_disk->disk_type;
362 	type = cur_ctype->ctype_dlist;
363 	/*
364 	 * Print out the list of choices.
365 	 */
366 	fmt_print("\n\nAVAILABLE DRIVE TYPES:\n");
367 	first_disk = 0;
368 	if (cur_ctype->ctype_ctype == DKC_SCSI_CCS) {
369 		auto_conf_choice = 0;
370 		fmt_print("        %d. Auto configure\n", first_disk++);
371 	} else {
372 		auto_conf_choice = -1;
373 	}
374 	i = first_disk;
375 	for (tptr = type; tptr != NULL; tptr = tptr->dtype_next) {
376 		/*
377 		 * If we pass the current type, mark it to be the default.
378 		 */
379 		if (cur_dtype == tptr) {
380 			deflt = i;
381 			defltptr = &deflt;
382 		}
383 		if (cur_disk->label_type == L_TYPE_EFI) {
384 			continue;
385 		}
386 		if (tptr->dtype_asciilabel)
387 		    fmt_print("        %d. %s\n", i++, tptr->dtype_asciilabel);
388 	}
389 	other_choice = i;
390 	fmt_print("        %d. other\n", i);
391 	ioparam.io_bounds.lower = 0;
392 	ioparam.io_bounds.upper = i;
393 	/*
394 	 * Ask the user which type the disk is.
395 	 */
396 	index = input(FIO_INT, "Specify disk type (enter its number)", ':',
397 	    &ioparam, defltptr, DATA_INPUT);
398 	/*
399 	 * Find the type s/he chose.
400 	 */
401 	if (index == auto_conf_choice) {
402 		float			scaled;
403 		long			nblks;
404 		int			nparts;
405 
406 		/*
407 		 * User chose "auto configure".
408 		 */
409 	    (void) strcpy(x86_devname, cur_disk->disk_name);
410 	    switch (cur_disk->label_type) {
411 	    case L_TYPE_SOLARIS:
412 		if ((tptr = auto_sense(cur_file, 1, &label)) == NULL) {
413 			err_print("Auto configure failed\n");
414 			return (-1);
415 		}
416 		fmt_print("%s: configured with capacity of ",
417 			cur_disk->disk_name);
418 		nblks = tptr->dtype_ncyl * tptr->dtype_nhead *
419 			tptr->dtype_nsect;
420 		scaled = bn2mb(nblks);
421 		if (scaled > 1024.0) {
422 			fmt_print("%1.2fGB\n", scaled/1024.0);
423 		} else {
424 			fmt_print("%1.2fMB\n", scaled);
425 		}
426 		fmt_print("<%s cyl %d alt %d hd %d sec %d>\n",
427 			tptr->dtype_asciilabel, tptr->dtype_ncyl,
428 			tptr->dtype_acyl, tptr->dtype_nhead,
429 			tptr->dtype_nsect);
430 		break;
431 	    case L_TYPE_EFI:
432 		if ((tptr = auto_efi_sense(cur_file, &efi_info)) == NULL) {
433 			err_print("Auto configure failed\n");
434 			return (-1);
435 		}
436 		fmt_print("%s: configured with capacity of ",
437 			cur_disk->disk_name);
438 		scaled = bn2mb(efi_info.capacity);
439 		if (scaled > 1024.0) {
440 			fmt_print("%1.2fGB\n", scaled/1024.0);
441 		} else {
442 			fmt_print("%1.2fMB\n", scaled);
443 		}
444 		print_efi_string(efi_info.vendor, efi_info.product,
445 		    efi_info.revision, efi_info.capacity);
446 		fmt_print("\n");
447 		for (nparts = 0; nparts < cur_parts->etoc->efi_nparts;
448 			nparts++) {
449 		    if (cur_parts->etoc->efi_parts[nparts].p_tag ==
450 			V_RESERVED) {
451 			if (cur_parts->etoc->efi_parts[nparts].p_name) {
452 			    (void) strcpy(volname,
453 				cur_parts->etoc->efi_parts[nparts].p_name);
454 			    volinit = 1;
455 			}
456 			break;
457 		    }
458 		}
459 		enter_critical();
460 		if (delete_disk_type(cur_disk->disk_type) != 0) {
461 			fmt_print("Autoconfiguration failed.\n");
462 			return (-1);
463 		}
464 		cur_disk->disk_type = tptr;
465 		cur_disk->disk_parts = tptr->dtype_plist;
466 		init_globals(cur_disk);
467 		exit_critical();
468 		if (volinit) {
469 		    for (nparts = 0; nparts < cur_parts->etoc->efi_nparts;
470 				nparts++) {
471 			if (cur_parts->etoc->efi_parts[nparts].p_tag ==
472 				V_RESERVED) {
473 			    (void) strcpy(
474 				cur_parts->etoc->efi_parts[nparts].p_name,
475 				    volname);
476 			    (void) strlcpy(cur_disk->v_volume, volname,
477 				    LEN_DKL_VVOL);
478 			    break;
479 			}
480 		    }
481 		}
482 		return (0);
483 		break;
484 	    default:
485 		/* Should never happen */
486 		return (-1);
487 	    }
488 	} else if ((index == other_choice) && (cur_label == L_TYPE_SOLARIS)) {
489 		/*
490 		 * User chose "other".
491 		 * Get the standard information on the new type.
492 		 * Put all information in a tmp structure, in
493 		 * case user aborts.
494 		 */
495 		bzero((char *)d, sizeof (struct disk_type));
496 
497 		d->dtype_ncyl = get_ncyl();
498 		d->dtype_acyl = get_acyl(d->dtype_ncyl);
499 		d->dtype_pcyl = get_pcyl(d->dtype_ncyl, d->dtype_acyl);
500 		d->dtype_nhead = get_nhead();
501 		d->dtype_phead = get_phead(d->dtype_nhead, &d->dtype_options);
502 		d->dtype_nsect = get_nsect();
503 		d->dtype_psect = get_psect(&d->dtype_options);
504 		d->dtype_bpt = get_bpt(d->dtype_nsect, &d->dtype_options);
505 		d->dtype_rpm = get_rpm();
506 		d->dtype_fmt_time = get_fmt_time(&d->dtype_options);
507 		d->dtype_cyl_skew = get_cyl_skew(&d->dtype_options);
508 		d->dtype_trk_skew = get_trk_skew(&d->dtype_options);
509 		d->dtype_trks_zone = get_trks_zone(&d->dtype_options);
510 		d->dtype_atrks = get_atrks(&d->dtype_options);
511 		d->dtype_asect = get_asect(&d->dtype_options);
512 		d->dtype_cache = get_cache(&d->dtype_options);
513 		d->dtype_threshold = get_threshold(&d->dtype_options);
514 		d->dtype_prefetch_min = get_min_prefetch(&d->dtype_options);
515 		d->dtype_prefetch_max = get_max_prefetch(d->dtype_prefetch_min,
516 			&d->dtype_options);
517 		d->dtype_bps = get_bps();
518 #if defined(sparc)
519 		d->dtype_dr_type = 0;
520 #endif /* defined(sparc) */
521 
522 		d->dtype_asciilabel = get_asciilabel();
523 
524 		/*
525 		 * Add the new type to the list of possible types for
526 		 * this controller.  We lock out interrupts so the lists
527 		 * can't get munged.  We put off actually allocating the
528 		 * structure till here in case the user wanted to
529 		 * interrupt while still inputting information.
530 		 */
531 		enter_critical();
532 		tptr = (struct disk_type *)zalloc(sizeof (struct disk_type));
533 		if (type == NULL)
534 			cur_ctype->ctype_dlist = tptr;
535 		else {
536 			while (type->dtype_next != NULL)
537 				type = type->dtype_next;
538 			type->dtype_next = tptr;
539 		}
540 		bcopy((char *)d, (char *)tptr, sizeof (disk_type));
541 		tptr->dtype_next = NULL;
542 		/*
543 		 * the new disk type does not have any defined
544 		 * partition table . Hence copy the current partition
545 		 * table if possible else create a default
546 		 * paritition table.
547 		 */
548 		new_partitiontable(tptr, oldtype);
549 	} else if ((index == other_choice) && (cur_label == L_TYPE_EFI)) {
550 		maxLBA = get_mlba();
551 		cur_parts->etoc->efi_last_lba = maxLBA;
552 		cur_parts->etoc->efi_last_u_lba = maxLBA - 34;
553 		for (i = 0; i < cur_parts->etoc->efi_nparts; i++) {
554 		    cur_parts->etoc->efi_parts[i].p_start = 0;
555 		    cur_parts->etoc->efi_parts[i].p_size = 0;
556 		    cur_parts->etoc->efi_parts[i].p_tag = V_UNASSIGNED;
557 		}
558 		cur_parts->etoc->efi_parts[8].p_start =
559 			maxLBA - 34 - (1024 * 16);
560 		cur_parts->etoc->efi_parts[8].p_size = (1024 * 16);
561 		cur_parts->etoc->efi_parts[8].p_tag = V_RESERVED;
562 		if (write_label()) {
563 		    err_print("Write label failed\n");
564 		} else {
565 		    cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
566 		}
567 		return (0);
568 	} else {
569 		/*
570 		 * User picked an existing disk type.
571 		 */
572 		i = first_disk;
573 		tptr = type;
574 		while (i < index) {
575 		    if (tptr->dtype_asciilabel) {
576 			i++;
577 		    }
578 		    tptr = tptr->dtype_next;
579 		}
580 		if ((tptr->dtype_asciilabel == NULL) &&
581 		    (tptr->dtype_next != NULL)) {
582 			while (tptr->dtype_asciilabel == NULL) {
583 				tptr = tptr->dtype_next;
584 			}
585 		}
586 	}
587 	/*
588 	 * Check for mounted file systems in the format zone.
589 	 * One potential problem with this would be that check()
590 	 * always returns 'yes' when running out of a file.  However,
591 	 * it is actually ok because we don't let the program get
592 	 * started if there are mounted file systems and we are
593 	 * running from a file.
594 	 */
595 	if ((tptr != oldtype) &&
596 			checkmount((daddr_t)-1, (daddr_t)-1)) {
597 		err_print(
598 		"Cannot set disk type while it has mounted partitions.\n\n");
599 		return (-1);
600 	}
601 	/*
602 	 * check for partitions being used for swapping in format zone
603 	 */
604 	if ((tptr != oldtype) &&
605 			checkswap((daddr_t)-1, (daddr_t)-1)) {
606 		err_print("Cannot set disk type while its partition are \
607 currently being used for swapping.\n");
608 		return (-1);
609 	}
610 
611 	/*
612 	 * Check for partitions being used in SVM, VxVM or LU devices
613 	 */
614 
615 	if ((tptr != oldtype) &&
616 		checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
617 		    (diskaddr_t)-1, 0, 0)) {
618 		err_print("Cannot set disk type while its "
619 		    "partitions are currently in use.\n");
620 				return (-1);
621 	}
622 	/*
623 	 * If the type selected is different from the previous type,
624 	 * mark the disk as not labelled and reload the current
625 	 * partition info.  This is not essential but probably the
626 	 * right thing to do, since the size of the disk has probably
627 	 * changed.
628 	 */
629 	enter_critical();
630 	if (tptr != oldtype) {
631 		cur_disk->disk_type = tptr;
632 		cur_disk->disk_parts = NULL;
633 		cur_disk->disk_flags &= ~DSK_LABEL;
634 	}
635 	/*
636 	 * Initialize the state of the current disk.
637 	 */
638 	init_globals(cur_disk);
639 	(void) get_partition();
640 	exit_critical();
641 
642 	/*
643 	 * If the label of the disk is marked dirty,
644 	 * see if they'd like to label the disk now.
645 	 */
646 	if (cur_disk->disk_flags & DSK_LABEL_DIRTY) {
647 		if (check("Disk not labeled.  Label it now") == 0) {
648 			if (write_label()) {
649 				err_print("Write label failed\n");
650 			} else {
651 				cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
652 			}
653 		}
654 	}
655 
656 	return (0);
657 }
658 
659 /*
660  * This routine implements the 'partition' command.  It simply runs
661  * the partition menu.
662  */
663 int
664 c_partition()
665 {
666 
667 	/*
668 	 * There must be a current disk type and a current disk
669 	 */
670 	if (cur_dtype == NULL) {
671 		err_print("Current Disk Type is not set.\n");
672 		return (-1);
673 	}
674 	/*
675 	 * Check for a valid fdisk table entry for Solaris
676 	 */
677 	if (!good_fdisk()) {
678 		return (-1);
679 	}
680 
681 	cur_menu++;
682 	last_menu = cur_menu;
683 
684 #ifdef	not
685 	/*
686 	 * If there is no current partition table, make one.  This is
687 	 * so the commands within the menu never have to check for
688 	 * a non-existent table.
689 	 */
690 	if (cur_parts == NULL)
691 		err_print("making partition.\n");
692 		make_partition();
693 #endif	/* not */
694 
695 	/*
696 	 * Run the menu.
697 	 */
698 	run_menu(menu_partition, "PARTITION", "partition", 0);
699 	cur_menu--;
700 	return (0);
701 }
702 
703 /*
704  * This routine implements the 'current' command.  It describes the
705  * current disk.
706  */
707 int
708 c_current()
709 {
710 
711 	/*
712 	 * If there is no current disk, say so.  Note that this is
713 	 * not an error since it is a legitimate response to the inquiry.
714 	 */
715 	if (cur_disk == NULL) {
716 		fmt_print("No Current Disk.\n");
717 		return (0);
718 	}
719 	/*
720 	 * Print out the info we have on the current disk.
721 	 */
722 	fmt_print("Current Disk = %s", cur_disk->disk_name);
723 	if (chk_volname(cur_disk)) {
724 		fmt_print(": ");
725 		print_volname(cur_disk);
726 	}
727 	fmt_print("\n");
728 	if (cur_disk->devfs_name != NULL) {
729 		if (cur_dtype == NULL) {
730 			fmt_print("<type unknown>\n");
731 		} else if (cur_label == L_TYPE_SOLARIS) {
732 			fmt_print("<%s cyl %d alt %d hd %d sec %d>\n",
733 				cur_dtype->dtype_asciilabel, ncyl,
734 				acyl, nhead, nsect);
735 		} else if (cur_label == L_TYPE_EFI) {
736 			print_efi_string(cur_dtype->vendor,
737 			    cur_dtype->product, cur_dtype->revision,
738 			    cur_dtype->capacity);
739 			fmt_print("\n");
740 		}
741 		fmt_print("%s\n", cur_disk->devfs_name);
742 	} else {
743 		fmt_print("%s%d: <", cur_ctlr->ctlr_dname,
744 			cur_disk->disk_dkinfo.dki_unit);
745 		if (cur_dtype == NULL) {
746 			fmt_print("type unknown");
747 		} else if (cur_label == L_TYPE_SOLARIS) {
748 			fmt_print("%s cyl %d alt %d hd %d sec %d",
749 				cur_dtype->dtype_asciilabel, ncyl,
750 				acyl, nhead, nsect);
751 		} else if (cur_label == L_TYPE_EFI) {
752 			print_efi_string(cur_dtype->vendor,
753 			    cur_dtype->product, cur_dtype->revision,
754 			    cur_dtype->capacity);
755 			fmt_print("\n");
756 		}
757 		fmt_print(">\n");
758 	}
759 	fmt_print("\n");
760 	return (0);
761 }
762 /*
763  * This routine implements the 'format' command.  It allows the user
764  * to format and verify any portion of the disk.
765  */
766 int
767 c_format()
768 {
769 	diskaddr_t		start, end;
770 	time_t			clock;
771 	int			format_time, format_tracks, format_cyls;
772 	int			format_interval;
773 	int			deflt, status;
774 	u_ioparam_t		ioparam;
775 
776 	/*
777 	 * There must be a current disk type and a current disk
778 	 */
779 	if (cur_dtype == NULL) {
780 		err_print("Current Disk Type is not set.\n");
781 		return (-1);
782 	}
783 
784 	/*
785 	 * There must be a format routine in cur_ops structure to have
786 	 *  this routine work.
787 	 */
788 	if (cur_ops->op_format == NULL) {
789 		err_print(
790 "Cannot format this drive. Please use your Manufacturer supplied formatting "
791 "utility.\n");
792 		return (-1);
793 	}
794 
795 	/*
796 	 * There must be a current defect list.  Except for
797 	 * unformatted SCSI disks.  For them the defect list
798 	 * can only be retrieved after formatting the disk.
799 	 */
800 	if ((cur_ctype->ctype_flags & CF_SCSI) && !EMBEDDED_SCSI &&
801 		(cur_ctype->ctype_flags & CF_DEFECTS) &&
802 			! (cur_flags & DISK_FORMATTED)) {
803 		cur_list.flags |= LIST_RELOAD;
804 
805 	} else if (cur_list.list == NULL && !EMBEDDED_SCSI) {
806 		err_print("Current Defect List must be initialized.\n");
807 		return (-1);
808 	}
809 	/*
810 	 * Ask for the bounds of the format.  We always use the whole
811 	 * disk as the default, since that is the most likely case.
812 	 * Note, for disks which must be formatted accross the whole disk,
813 	 * don't bother the user.
814 	 */
815 	ioparam.io_bounds.lower = start = 0;
816 	if (cur_label == L_TYPE_SOLARIS) {
817 	    if (cur_ctype->ctype_flags & CF_SCSI) {
818 		ioparam.io_bounds.upper = end = datasects() - 1;
819 	    } else {
820 		ioparam.io_bounds.upper = end = physsects() - 1;
821 	    }
822 	} else {
823 	    ioparam.io_bounds.upper = end = cur_parts->etoc->efi_last_lba;
824 	}
825 
826 	if (! (cur_ctlr->ctlr_flags & DKI_FMTVOL)) {
827 		deflt = ioparam.io_bounds.lower;
828 		start = input(FIO_BN,
829 			"Enter starting block number", ':',
830 			&ioparam, &deflt, DATA_INPUT);
831 		ioparam.io_bounds.lower = start;
832 		deflt = ioparam.io_bounds.upper;
833 		end = input(FIO_BN,
834 			"Enter ending block number", ':',
835 			&ioparam, &deflt, DATA_INPUT);
836 	}
837 	/*
838 	 * Some disks can format tracks.  Make sure the whole track is
839 	 * specified for them.
840 	 */
841 	if (cur_ctlr->ctlr_flags & DKI_FMTTRK) {
842 		if (bn2s(start) != 0 ||
843 				bn2s(end) != sectors(bn2h(end)) - 1) {
844 			err_print("Controller requires formatting of ");
845 			err_print("entire tracks.\n");
846 			return (-1);
847 		}
848 	}
849 	/*
850 	 * Check for mounted file systems in the format zone, and if we
851 	 * find any, make sure they are really serious.  One potential
852 	 * problem with this would be that check() always returns 'yes'
853 	 * when running out of a file.  However, it is actually ok
854 	 * because we don't let the program get started if there are
855 	 * mounted file systems and we are running from a file.
856 	 */
857 	if (checkmount(start, end)) {
858 		err_print(
859 		"Cannot format disk while it has mounted partitions.\n\n");
860 		return (-1);
861 	}
862 	/*
863 	 * check for partitions being used for swapping in format zone
864 	 */
865 	if (checkswap(start, end)) {
866 		err_print("Cannot format disk while its partition are \
867 currently being used for swapping.\n");
868 		return (-1);
869 	}
870 	/*
871 	 * Check for partitions being used in SVM, VxVM or LU devices
872 	 * in this format zone
873 	 */
874 	if (checkdevinuse(cur_disk->disk_name, start, end, 0, 0)) {
875 		err_print("Cannot format disk while its partitions "
876 			    "are currently in use.\n");
877 			return (-1);
878 	}
879 
880 	if (SCSI && (format_time = scsi_format_time()) > 0) {
881 		fmt_print(
882 		    "Ready to format.  Formatting cannot be interrupted\n"
883 		    "and takes %d minutes (estimated). ", format_time);
884 
885 	} else if (cur_dtype->dtype_options & SUP_FMTTIME) {
886 		/*
887 		 * Formatting time is (2 * time of 1 spin * number of
888 		 * tracks) + (step rate * number of cylinders) rounded
889 		 * up to the nearest minute.  Note, a 10% fudge factor
890 		 * is thrown in for insurance.
891 		 */
892 		if (cur_dtype->dtype_fmt_time == 0)
893 			cur_dtype->dtype_fmt_time = 2;
894 
895 		format_tracks = ((end-start) / cur_dtype->dtype_nsect) + 1;
896 		format_cyls = format_tracks / cur_dtype->dtype_nhead;
897 		format_tracks = format_tracks * cur_dtype->dtype_fmt_time;
898 
899 		/*
900 		 * ms.
901 		 */
902 		format_time = ((60000 / cur_dtype->dtype_rpm) +1) *
903 			format_tracks + format_cyls * 7;
904 		/*
905 		 * 20% done tick (sec)
906 		 */
907 		format_interval = format_time / 5000;
908 		/*
909 		 * min.
910 		 */
911 		format_time = (format_time + 59999) / 60000;
912 
913 		/*
914 		 * Check format time values and make adjustments
915 		 * to prevent sleeping too long (forever?) or
916 		 * too short.
917 		 */
918 		if (format_time <= 1) {
919 			/*
920 			 * Format time is less than 1 min..
921 			 */
922 			format_time = 1;
923 		}
924 
925 		if (format_interval < 11) {
926 			/* Format time is less than 1 minute. */
927 			if (format_interval < 2)
928 				format_interval = 2;	/* failsafe */
929 			format_interval = 10;
930 		} else {
931 			/* Format time is greater than 1 minute. */
932 			format_interval -= 10;
933 		}
934 
935 		fmt_print(
936 		    "Ready to format.  Formatting cannot be interrupted\n"
937 		    "and takes %d minutes (estimated). ", format_time);
938 	} else {
939 		fmt_print(
940 		    "Ready to format.  Formatting cannot be interrupted.\n");
941 	}
942 	if (check("Continue")) {
943 		return (-1);
944 	}
945 
946 	/*
947 	 * Print the time so that the user will know when format started.
948 	 * Lock out interrupts.  This could be a problem, since it could
949 	 * cause the user to sit for quite awhile with no control, but we
950 	 * don't have any other good way of keeping his gun from going off.
951 	 */
952 	clock = time((time_t *)0);
953 	fmt_print("Beginning format. The current time is %s\n",
954 		ctime(&clock));
955 	enter_critical();
956 	/*
957 	 * Mark the defect list dirty so it will be rewritten when we are
958 	 * done.  It is possible to qualify this so it doesn't always
959 	 * get rewritten, but it's not worth the trouble.
960 	 * Note: no defect lists for embedded scsi drives.
961 	 */
962 	if (!EMBEDDED_SCSI) {
963 		cur_list.flags |= LIST_DIRTY;
964 	}
965 	/*
966 	 * If we are formatting over any of the labels, mark the label
967 	 * dirty so it will be rewritten.
968 	 */
969 	if (cur_disk->label_type == L_TYPE_SOLARIS) {
970 	    if (start < totalsects() && end >= datasects()) {
971 		if (cur_disk->disk_flags & DSK_LABEL)
972 			cur_flags |= LABEL_DIRTY;
973 	    }
974 	} else if (cur_disk->label_type == L_TYPE_EFI) {
975 	    if (start < 34) {
976 		if (cur_disk->disk_flags & DSK_LABEL)
977 		    cur_flags |= LABEL_DIRTY;
978 	    }
979 	}
980 	if (start == 0) {
981 		cur_flags |= LABEL_DIRTY;
982 	}
983 	/*
984 	 * Do the format. bugid 1009138 removed the use of fork to
985 	 * background the format and print a tick.
986 	 */
987 
988 	status = (*cur_ops->op_format)(start, end, &cur_list);
989 	if (status) {
990 		exit_critical();
991 		err_print("failed\n");
992 		return (-1);
993 	}
994 	fmt_print("done\n");
995 	if (option_msg && diag_msg) {
996 		clock = time((time_t *)0);
997 		fmt_print("The current time is %s\n", ctime(&clock));
998 	}
999 	cur_flags |= DISK_FORMATTED;
1000 	/*
1001 	 * If the defect list or label is dirty, write them out again.
1002 	 * Note, for SCSI we have to wait til now to load defect list
1003 	 * since we can't access it until after formatting a virgin disk.
1004 	 */
1005 	/* enter_critical(); */
1006 	if (cur_list.flags & LIST_RELOAD) {
1007 		assert(!EMBEDDED_SCSI);
1008 		if (*cur_ops->op_ex_man == NULL ||
1009 		    (*cur_ops->op_ex_man)(&cur_list)) {
1010 			err_print("Warning: unable to reload defect list\n");
1011 			cur_list.flags &= ~LIST_DIRTY;
1012 			return (-1);
1013 		}
1014 		cur_list.flags |= LIST_DIRTY;
1015 	}
1016 
1017 	if (cur_list.flags & LIST_DIRTY) {
1018 		assert(!EMBEDDED_SCSI);
1019 		write_deflist(&cur_list);
1020 		cur_list.flags = 0;
1021 	}
1022 	if (cur_flags & LABEL_DIRTY) {
1023 		(void) write_label();
1024 		cur_flags &= ~LABEL_DIRTY;
1025 	}
1026 	/*
1027 	 * Come up for air, since the verify step does not need to
1028 	 * be atomic (it does it's own lockouts when necessary).
1029 	 */
1030 	exit_critical();
1031 	/*
1032 	 * If we are supposed to verify, we do the 'write' test over
1033 	 * the format zone.  The rest of the analysis parameters are
1034 	 * left the way they were.
1035 	 */
1036 	if (scan_auto) {
1037 		scan_entire = 0;
1038 		scan_lower = start;
1039 		scan_upper = end;
1040 		fmt_print("\nVerifying media...");
1041 		status = do_scan(SCAN_PATTERN, F_SILENT);
1042 	}
1043 	/*
1044 	 * If the defect list or label is dirty, write them out again.
1045 	 */
1046 	if (cur_list.flags & LIST_DIRTY) {
1047 		assert(!EMBEDDED_SCSI);
1048 		cur_list.flags = 0;
1049 		write_deflist(&cur_list);
1050 	}
1051 	if (cur_flags & LABEL_DIRTY) {
1052 		cur_flags &= ~LABEL_DIRTY;
1053 		(void) write_label();
1054 	}
1055 	return (status);
1056 }
1057 
1058 /*
1059  * This routine implements the 'repair' command.  It allows the user
1060  * to reallocate sectors on the disk that have gone bad.
1061  */
1062 int
1063 c_repair()
1064 {
1065 	diskaddr_t	bn;
1066 	int		status;
1067 	u_ioparam_t	ioparam;
1068 	char		buf[SECSIZE];
1069 	int		buf_is_good;
1070 	int		block_has_error;
1071 	int		i;
1072 
1073 	/*
1074 	 * There must be a current disk type (and therefore a current disk).
1075 	 */
1076 	if (cur_dtype == NULL) {
1077 		err_print("Current Disk Type is not set.\n");
1078 		return (-1);
1079 	}
1080 	/*
1081 	 * The current disk must be formatted for repair to work.
1082 	 */
1083 	if (!(cur_flags & DISK_FORMATTED)) {
1084 		err_print("Current Disk is unformatted.\n");
1085 		return (-1);
1086 	}
1087 	/*
1088 	 * Check for a valid fdisk table entry for Solaris
1089 	 */
1090 	if (!good_fdisk()) {
1091 		return (-1);
1092 	}
1093 	/*
1094 	 * Repair is an optional command for controllers, so it may
1095 	 * not be supported.
1096 	 */
1097 	if (cur_ops->op_repair == NULL) {
1098 		err_print("Controller does not support repairing.\n");
1099 		err_print("or disk supports automatic defect management.\n");
1100 		return (-1);
1101 	}
1102 	/*
1103 	 * There must be a defect list for non-embedded scsi devices,
1104 	 * since we will add to it.
1105 	 */
1106 	if (!EMBEDDED_SCSI && cur_list.list == NULL) {
1107 		err_print("Current Defect List must be initialized.\n");
1108 		return (-1);
1109 	}
1110 	/*
1111 	 * Ask the user which sector has gone bad.
1112 	 */
1113 	ioparam.io_bounds.lower = 0;
1114 	if (cur_disk->label_type == L_TYPE_SOLARIS) {
1115 	    ioparam.io_bounds.upper = physsects() - 1;
1116 	} else {
1117 	    ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba;
1118 	}
1119 	bn = input(FIO_BN,
1120 		"Enter absolute block number of defect", ':',
1121 		&ioparam, (int *)NULL, DATA_INPUT);
1122 	/*
1123 	 * Check to see if there is a mounted file system over the
1124 	 * specified sector.  If there is, make sure the user is
1125 	 * really serious.
1126 	 */
1127 	if (checkmount(bn, bn)) {
1128 		if (check("Repair is in a mounted partition, continue"))
1129 			return (-1);
1130 	}
1131 	/*
1132 	 * check for partitions being used for swapping in format zone
1133 	 */
1134 	if (checkswap(bn, bn)) {
1135 		if (check("Repair is in a partition which is currently \
1136 being used for swapping.\ncontinue"))
1137 		return (-1);
1138 	}
1139 
1140 	if (checkdevinuse(cur_disk->disk_name, bn, bn, 0, 0)) {
1141 		if (check("Repair is in a partition which is currently "
1142 		    "in use.\ncontinue"))
1143 			return (-1);
1144 	}
1145 
1146 	/*
1147 	 * Try to read the sector before repairing it.  If we can
1148 	 * get good data out of it, we can write that data back
1149 	 * after the repair.  If the sector looks ok, ask the
1150 	 * user to confirm the repair, since it doesn't appear
1151 	 * necessary.  Try reading the block several times to
1152 	 * see if we can read it consistently.
1153 	 *
1154 	 * First, let's see if the block appears to have problems...
1155 	 */
1156 	block_has_error = 1;
1157 	for (i = 0; i < 5; i++) {
1158 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
1159 				1, buf, (F_SILENT | F_ALLERRS), NULL);
1160 		if (status)
1161 			break;		/* one of the tries failed */
1162 	}
1163 	if (status == 0) {
1164 		block_has_error = 0;
1165 		if (check("\
1166 This block doesn't appear to be bad.  Repair it anyway")) {
1167 			return (0);
1168 		}
1169 	}
1170 	/*
1171 	 * Last chance...
1172 	 */
1173 	if (check("Ready to repair defect, continue")) {
1174 		return (-1);
1175 	}
1176 	/*
1177 	 * We're committed to repairing it.  Try to get any good
1178 	 * data out of the block if possible.  Note that we do
1179 	 * not set the F_ALLERRS flag.
1180 	 */
1181 	buf_is_good = 0;
1182 	for (i = 0; i < 5; i++) {
1183 		status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn,
1184 					1, buf, F_SILENT, NULL);
1185 		if (status == 0) {
1186 			buf_is_good = 1;
1187 			break;
1188 		}
1189 	}
1190 	/*
1191 	 * Lock out interrupts so the disk can't get out of sync with
1192 	 * the defect list.
1193 	 */
1194 	enter_critical();
1195 
1196 	fmt_print("Repairing ");
1197 	if (block_has_error) {
1198 		fmt_print("%s error on ", buf_is_good ? "soft" : "hard");
1199 	}
1200 	fmt_print("block %llu (", bn);
1201 	pr_dblock(fmt_print, bn);
1202 	fmt_print(")...");
1203 	/*
1204 	 * Do the repair.
1205 	 */
1206 	status = (*cur_ops->op_repair)(bn, F_NORMAL);
1207 	if (status) {
1208 		fmt_print("failed.\n\n");
1209 	} else {
1210 		/*
1211 		 * The repair worked.  Write the old data to the new
1212 		 * block if we were able to read it, otherwise
1213 		 * zero out the new block.  If it looks like the
1214 		 * new block is bad, let the user know that, too.
1215 		 * Should we attempt auto-repair in this case?
1216 		 */
1217 		fmt_print("ok.\n");
1218 		if (!buf_is_good) {
1219 			bzero(buf, SECSIZE);
1220 		}
1221 		status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn,
1222 					1, buf, (F_SILENT | F_ALLERRS), NULL);
1223 		if (status == 0) {
1224 			status = (*cur_ops->op_rdwr)(DIR_READ, cur_file,
1225 				bn, 1, buf, (F_SILENT | F_ALLERRS), NULL);
1226 		}
1227 		if (status) {
1228 			fmt_print("The new block %llu (", bn);
1229 			pr_dblock(fmt_print, bn);
1230 			fmt_print(") also appears defective.\n");
1231 		}
1232 		fmt_print("\n");
1233 		/*
1234 		 * Add the bad sector to the defect list, write out
1235 		 * the defect list, and kill off the working list so
1236 		 * it will get synced up with the current defect list
1237 		 * next time we need it.
1238 		 *
1239 		 * For embedded scsi, we don't require a defect list.
1240 		 * However, if we have one, add the defect if the
1241 		 * list includes the grown list.  If not, kill it
1242 		 * to force a resync if we need the list later.
1243 		 */
1244 		if (EMBEDDED_SCSI) {
1245 			if (cur_list.list != NULL) {
1246 				if (cur_list.flags & LIST_PGLIST) {
1247 					add_ldef(bn, &cur_list);
1248 				} else {
1249 					kill_deflist(&cur_list);
1250 				}
1251 			}
1252 		} else if (cur_ctype->ctype_flags & CF_WLIST) {
1253 			kill_deflist(&cur_list);
1254 			if (*cur_ops->op_ex_cur != NULL) {
1255 				(*cur_ops->op_ex_cur)(&cur_list);
1256 				fmt_print("Current list updated\n");
1257 			}
1258 		} else {
1259 			add_ldef(bn, &cur_list);
1260 			write_deflist(&cur_list);
1261 		}
1262 		kill_deflist(&work_list);
1263 	}
1264 	exit_critical();
1265 	/*
1266 	 * Return status.
1267 	 */
1268 	return (status);
1269 }
1270 
1271 /*
1272  * This routine implements the 'show' command.  It translates a disk
1273  * block given in any format into decimal, hexadecimal, and
1274  * cylinder/head/sector format.
1275  */
1276 int
1277 c_show()
1278 {
1279 	u_ioparam_t	ioparam;
1280 	daddr_t		bn;
1281 
1282 	/*
1283 	 * There must be a current disk type, so we will know the geometry.
1284 	 */
1285 	if (cur_dtype == NULL) {
1286 		err_print("Current Disk Type is not set.\n");
1287 		return (-1);
1288 	}
1289 	/*
1290 	 * Ask the user for a disk block.
1291 	 */
1292 	ioparam.io_bounds.lower = 0;
1293 	if (cur_disk->label_type == L_TYPE_SOLARIS) {
1294 	    ioparam.io_bounds.upper = physsects() - 1;
1295 	} else {
1296 	    ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba;
1297 	}
1298 	bn = (daddr_t)input(FIO_BN, "Enter a disk block", ':',
1299 	    &ioparam, (int *)NULL, DATA_INPUT);
1300 	/*
1301 	 * Echo it back.
1302 	 */
1303 	fmt_print("Disk block = %ld = 0x%lx = (", bn, bn);
1304 	pr_dblock(fmt_print, bn);
1305 	fmt_print(")\n\n");
1306 	return (0);
1307 }
1308 
1309 /*
1310  * This routine implements the 'label' command.  It writes the
1311  * primary and backup labels onto the current disk.
1312  */
1313 int
1314 c_label()
1315 {
1316 	int			status;
1317 	int			deflt, *defltptr = NULL;
1318 
1319 	/*
1320 	 * There must be a current disk type (and therefore a current disk).
1321 	 */
1322 	if (cur_dtype == NULL) {
1323 		err_print("Current Disk Type is not set.\n");
1324 		return (-1);
1325 	}
1326 	/*
1327 	 * The current disk must be formatted to label it.
1328 	 */
1329 	if (!(cur_flags & DISK_FORMATTED)) {
1330 		err_print("Current Disk is unformatted.\n");
1331 		return (-1);
1332 	}
1333 	/*
1334 	 * Check for a valid fdisk table entry for Solaris
1335 	 */
1336 	if (!good_fdisk()) {
1337 		return (-1);
1338 	}
1339 	/*
1340 	 * Check to see if there are any mounted file systems anywhere
1341 	 * on the current disk.  If so, refuse to label the disk, but
1342 	 * only if the partitions would change for the mounted partitions.
1343 	 *
1344 	 */
1345 	if (checkmount((daddr_t)-1, (daddr_t)-1)) {
1346 		/* Bleagh, too descriptive */
1347 		if (check_label_with_mount()) {
1348 			err_print("Cannot label disk while it has "
1349 				"mounted partitions.\n\n");
1350 			return (-1);
1351 		}
1352 	}
1353 
1354 	/*
1355 	 * check to see if there any partitions being used for swapping
1356 	 * on the current disk.  If so, refuse to label the disk, but
1357 	 * only if the partitions would change for the mounted partitions.
1358 	 */
1359 	if (checkswap((daddr_t)-1, (daddr_t)-1)) {
1360 		if (check_label_with_swap()) {
1361 			err_print("Cannot label disk while its "
1362 			    "partitions are currently being used for "
1363 			    "swapping.\n");
1364 			return (-1);
1365 		}
1366 	}
1367 
1368 	/*
1369 	 * Check to see if any partitions used for svm, vxvm or live upgrade
1370 	 * are on the disk. If so, refuse to label the disk, but only
1371 	 * if we are trying to shrink a partition in use.
1372 	 */
1373 	if (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
1374 	    (diskaddr_t)-1, 0, 1)) {
1375 		err_print("Cannot label disk when "
1376 		    "partitions are in use as described.\n");
1377 		return (-1);
1378 	}
1379 
1380 	/*
1381 	 * If there is not a current partition map, warn the user we
1382 	 * are going to use the default.  The default is the first
1383 	 * partition map we encountered in the data file.  If there is
1384 	 * no default we give up.
1385 	 */
1386 	if (cur_parts == NULL) {
1387 		fmt_print("Current Partition Table is not set, "
1388 			"using default.\n");
1389 		cur_disk->disk_parts = cur_parts = cur_dtype->dtype_plist;
1390 		if (cur_parts == NULL) {
1391 			err_print("No default available, cannot label.\n");
1392 			return (-1);
1393 		}
1394 	}
1395 	/*
1396 	 * If expert (-e) mode, then ask user if they wish
1397 	 * to change the current solaris label into an EFI one
1398 	 */
1399 	if (expert_mode) {
1400 #if defined(_SUNOS_VTOC_8)
1401 	    int 		i;
1402 #endif
1403 	    int 		choice;
1404 	    u_ioparam_t		ioparam;
1405 	    struct vtoc		vtoc;
1406 	    struct dk_label	label;
1407 	    struct dk_gpt	*vtoc64;
1408 	    struct efi_info	efinfo;
1409 	    struct disk_type	*dptr;
1410 
1411 		/* If capacity > 1TB then offer no choice */
1412 	    if (cur_label == L_TYPE_EFI) {
1413 		if (cur_dtype->capacity > INFINITY) {
1414 		    goto expert_end;
1415 		}
1416 	    }
1417 		/* Ask user what label to use */
1418 	    fmt_print("[0] SMI Label\n");
1419 	    fmt_print("[1] EFI Label\n");
1420 	    ioparam.io_bounds.lower = 0;
1421 	    ioparam.io_bounds.upper = 1;
1422 	    if (cur_label == L_TYPE_SOLARIS)
1423 		deflt = 0;
1424 	    else
1425 		deflt = 1;
1426 	    defltptr = &deflt;
1427 	    choice = input(FIO_INT, "Specify Label type", ':',
1428 		&ioparam, defltptr, DATA_INPUT);
1429 	    if ((choice == 0) && (cur_label == L_TYPE_SOLARIS)) {
1430 		goto expert_end;
1431 	    } else if ((choice == 1) && (cur_label == L_TYPE_EFI)) {
1432 		goto expert_end;
1433 	    }
1434 	    switch (choice) {
1435 	    case 0:
1436 		/*
1437 		 * EFI label to SMI label
1438 		 */
1439 		if (cur_dtype->capacity > INFINITY) {
1440 		    fmt_print("SMI Label not supported on this disk\n");
1441 		    return (-1);
1442 		}
1443 
1444 
1445 		fmt_print("Warning: This disk has an EFI label. Changing to "
1446 		    "SMI label will erase all\ncurrent partitions.\n");
1447 
1448 		if (check("Continue"))
1449 			return (-1);
1450 
1451 		(void) memset((char *)&label, 0, sizeof (struct dk_label));
1452 
1453 		if ((cur_disk->fdisk_part.systid == EFI_PMBR) ||
1454 		    (((cur_disk->fdisk_part.systid == SUNIXOS) ||
1455 		    (cur_disk->fdisk_part.systid == SUNIXOS2)) &&
1456 		    (cur_disk->fdisk_part.numsect == 0))) {
1457 			fmt_print("You must use fdisk to delete the current "
1458 			    "EFI partition and create a new\n"
1459 			    "Solaris partition before you can convert the "
1460 			    "label.\n");
1461 			return (-1);
1462 		}
1463 
1464 		(void) strcpy(x86_devname, cur_disk->disk_name);
1465 		if (cur_ctype->ctype_ctype == DKC_DIRECT)
1466 			dptr = auto_direct_get_geom_label(cur_file,  &label);
1467 		else
1468 			dptr = auto_sense(cur_file, 1, &label);
1469 		if (dptr == NULL) {
1470 			fmt_print("Autoconfiguration failed.\n");
1471 			return (-1);
1472 		}
1473 
1474 		pcyl = label.dkl_pcyl;
1475 		ncyl = label.dkl_ncyl;
1476 		acyl = label.dkl_acyl;
1477 		nhead = label.dkl_nhead;
1478 		nsect = label.dkl_nsect;
1479 
1480 		if (delete_disk_type(cur_disk->disk_type) == 0) {
1481 			cur_label = L_TYPE_SOLARIS;
1482 			cur_disk->label_type = L_TYPE_SOLARIS;
1483 			cur_disk->disk_type = dptr;
1484 			cur_disk->disk_parts = dptr->dtype_plist;
1485 			cur_dtype = dptr;
1486 			cur_parts = dptr->dtype_plist;
1487 
1488 			if (status = write_label())
1489 				err_print("Label failed.\n");
1490 			else
1491 				cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
1492 
1493 			return (status);
1494 		} else {
1495 			err_print("Label failed.\n");
1496 			return (-1);
1497 		}
1498 
1499 
1500 	    case 1:
1501 		/*
1502 		 * SMI label to EFI label
1503 		 */
1504 
1505 
1506 		fmt_print("Warning: This disk has an SMI label. Changing to "
1507 		    "EFI label will erase all\ncurrent partitions.\n");
1508 
1509 		if (check("Continue")) {
1510 			return (-1);
1511 		}
1512 
1513 		if (get_disk_info(cur_file, &efinfo) != 0) {
1514 		    return (-1);
1515 		}
1516 		(void) memset((char *)&label, 0, sizeof (struct dk_label));
1517 		label.dkl_pcyl = pcyl;
1518 		label.dkl_ncyl = ncyl;
1519 		label.dkl_acyl = acyl;
1520 #if defined(_SUNOS_VTOC_16)
1521 		label.dkl_bcyl = bcyl;
1522 #endif			/* defined(_SUNOC_VTOC_16) */
1523 		label.dkl_nhead = nhead;
1524 		label.dkl_nsect = nsect;
1525 #if defined(_SUNOS_VTOC_8)
1526 		for (i = 0; i < NDKMAP; i++) {
1527 		    label.dkl_map[i] = cur_parts->pinfo_map[i];
1528 		}
1529 #endif			/* defined(_SUNOS_VTOC_8) */
1530 		label.dkl_magic = DKL_MAGIC;
1531 		label.dkl_vtoc = cur_parts->vtoc;
1532 		if (label_to_vtoc(&vtoc, &label) == -1) {
1533 		    return (-1);
1534 		}
1535 		if (SMI_vtoc_to_EFI(cur_file, &vtoc64) == -1) {
1536 		    return (-1);
1537 		}
1538 		if (efi_write(cur_file, vtoc64) != 0) {
1539 		    err_check(vtoc64);
1540 		    err_print("Warning: error writing EFI.\n");
1541 		    return (-1);
1542 		} else {
1543 			cur_disk->disk_flags &= ~DSK_LABEL_DIRTY;
1544 		}
1545 		/*
1546 		 * copy over the EFI vtoc onto the SMI vtoc and return
1547 		 * okay.
1548 		 */
1549 		dptr = auto_efi_sense(cur_file, &efinfo);
1550 		if (dptr == NULL) {
1551 			fmt_print("Autoconfiguration failed.\n");
1552 			return (-1);
1553 		}
1554 
1555 		cur_label = L_TYPE_EFI;
1556 		cur_disk->label_type = L_TYPE_EFI;
1557 		cur_disk->disk_type = dptr;
1558 		cur_disk->disk_parts = dptr->dtype_plist;
1559 		cur_dtype = dptr;
1560 		cur_parts = dptr->dtype_plist;
1561 		cur_parts->etoc = vtoc64;
1562 
1563 		ncyl = pcyl = nsect = psect = acyl = phead = 0;
1564 
1565 		/*
1566 		 * Get the Solais Fdisk Partition information.
1567 		 */
1568 		(void) copy_solaris_part(&cur_disk->fdisk_part);
1569 
1570 		return (0);
1571 	    }
1572 	}
1573 
1574 expert_end:
1575 	/*
1576 	 * Make sure the user is serious.
1577 	 */
1578 	if (check("Ready to label disk, continue")) {
1579 		return (-1);
1580 	}
1581 	/*
1582 	 * Write the labels out (this will also notify unix) and
1583 	 * return status.
1584 	 */
1585 	fmt_print("\n");
1586 	if (status = write_label())
1587 		err_print("Label failed.\n");
1588 	return (status);
1589 }
1590 
1591 /*
1592  * This routine implements the 'analyze' command.  It simply runs
1593  * the analyze menu.
1594  */
1595 int
1596 c_analyze()
1597 {
1598 
1599 	/*
1600 	 * There must be a current disk type (and therefor a current disk).
1601 	 */
1602 	if (cur_dtype == NULL) {
1603 		err_print("Current Disk Type is not set.\n");
1604 		return (-1);
1605 	}
1606 	cur_menu++;
1607 	last_menu = cur_menu;
1608 
1609 	/*
1610 	 * Run the menu.
1611 	 */
1612 	run_menu(menu_analyze, "ANALYZE", "analyze", 0);
1613 	cur_menu--;
1614 	return (0);
1615 }
1616 
1617 /*
1618  * This routine implements the 'defect' command.  It simply runs
1619  * the defect menu.
1620  */
1621 int
1622 c_defect()
1623 {
1624 	int	i;
1625 
1626 	/*
1627 	 * There must be a current disk type (and therefor a current disk).
1628 	 */
1629 	if (cur_dtype == NULL) {
1630 		err_print("Current Disk Type is not set.\n");
1631 		return (-1);
1632 	}
1633 
1634 	/*
1635 	 * Check for the defect management and list management ops and
1636 	 * display appropriate message.
1637 	 */
1638 	if ((cur_ops->op_ex_man == NULL) && (cur_ops->op_ex_cur == NULL) &&
1639 		(cur_ops->op_create == NULL) && (cur_ops->op_wr_cur == NULL)) {
1640 		err_print("Controller does not support defect management\n");
1641 		err_print("or disk supports automatic defect management.\n");
1642 		return (-1);
1643 	}
1644 	cur_menu++;
1645 	last_menu = cur_menu;
1646 
1647 	/*
1648 	 * Lock out interrupt while we manipulate the defect lists.
1649 	 */
1650 	enter_critical();
1651 	/*
1652 	 * If the working list is null but there is a current list,
1653 	 * update the working list to be a copy of the current list.
1654 	 */
1655 	if ((work_list.list == NULL) && (cur_list.list != NULL)) {
1656 		work_list.header = cur_list.header;
1657 		work_list.list = (struct defect_entry *)zalloc(
1658 		    LISTSIZE(work_list.header.count) * SECSIZE);
1659 		for (i = 0; i < work_list.header.count; i++)
1660 			*(work_list.list + i) = *(cur_list.list + i);
1661 		work_list.flags = cur_list.flags & LIST_PGLIST;
1662 	}
1663 	exit_critical();
1664 	/*
1665 	 * Run the menu.
1666 	 */
1667 	run_menu(menu_defect, "DEFECT", "defect", 0);
1668 	cur_menu--;
1669 
1670 	/*
1671 	 * If the user has modified the working list but not committed
1672 	 * it, warn him that he is probably making a mistake.
1673 	 */
1674 	if (work_list.flags & LIST_DIRTY) {
1675 		if (!EMBEDDED_SCSI) {
1676 			err_print(
1677 		"Warning: working defect list modified; but not committed.\n");
1678 			if (!check(
1679 		"Do you wish to commit changes to current defect list"))
1680 			(void) do_commit();
1681 		}
1682 	}
1683 	return (0);
1684 }
1685 
1686 /*
1687  * This routine implements the 'backup' command.  It allows the user
1688  * to search for backup labels on the current disk.  This is useful
1689  * if the primary label was lost and the user wishes to recover the
1690  * partition information for the disk. The disk is relabeled and
1691  * the current defect list is written out if a backup label is found.
1692  */
1693 int
1694 c_backup()
1695 {
1696 	struct	dk_label label;
1697 	struct	disk_type *dtype;
1698 	struct	partition_info *parts, *plist;
1699 	daddr_t	bn;
1700 	int	sec, head, i;
1701 
1702 	/*
1703 	 * There must be a current disk type (and therefore a current disk).
1704 	 */
1705 	if (cur_dtype == NULL) {
1706 		err_print("Current Disk Type is not set.\n");
1707 		return (-1);
1708 	}
1709 	/*
1710 	 * The disk must be formatted to read backup labels.
1711 	 */
1712 	if (!(cur_flags & DISK_FORMATTED)) {
1713 		err_print("Current Disk is unformatted.\n");
1714 		return (-1);
1715 	}
1716 	/*
1717 	 * Check for a valid fdisk table entry for Solaris
1718 	 */
1719 	if (!good_fdisk()) {
1720 		return (-1);
1721 	}
1722 	/*
1723 	 * If we found a primary label on this disk, make sure
1724 	 * the user is serious.
1725 	 */
1726 	if (cur_disk->label_type == L_TYPE_EFI) {
1727 	    if (((cur_disk->disk_parts->etoc->efi_flags &
1728 		EFI_GPT_PRIMARY_CORRUPT) == 0) &&
1729 		check("Disk has a primary label, still continue"))
1730 		    return (-1);
1731 	    fmt_print("Restoring primary label.\n");
1732 	    if (write_label()) {
1733 		    err_print("Failed\n");
1734 		    return (-1);
1735 	    }
1736 	    return (0);
1737 	} else if (((cur_disk->disk_flags & (DSK_LABEL | DSK_LABEL_DIRTY)) ==
1738 		DSK_LABEL) &&
1739 	    (check("Disk has a primary label, still continue"))) {
1740 		return (-1);
1741 	}
1742 	fmt_print("Searching for backup labels...");
1743 	(void) fflush(stdout);
1744 	/*
1745 	 * Some disks have the backup labels in a strange place.
1746 	 */
1747 	if (cur_ctype->ctype_flags & CF_BLABEL)
1748 		head = 2;
1749 	else
1750 		head = nhead - 1;
1751 	/*
1752 	 * Loop through each copy of the backup label.
1753 	 */
1754 	for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect));
1755 	    sec += 2) {
1756 		bn = chs2bn(ncyl + acyl - 1, head, sec) + solaris_offset;
1757 		/*
1758 		 * Attempt to read it.
1759 		 */
1760 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, (diskaddr_t)bn,
1761 				1, (char *)&label, F_NORMAL, NULL)) {
1762 			continue;
1763 		}
1764 		/*
1765 		 * Verify that it is a reasonable label.
1766 		 */
1767 		if (!checklabel(&label))
1768 			continue;
1769 		if (trim_id(label.dkl_asciilabel))
1770 			continue;
1771 		/*
1772 		 * Lock out interrupts while we manipulate lists.
1773 		 */
1774 		enter_critical();
1775 		fmt_print("found.\n");
1776 		/*
1777 		 * Find out which disk type the backup label claims.
1778 		 */
1779 		for (dtype = cur_ctype->ctype_dlist; dtype != NULL;
1780 		    dtype = dtype->dtype_next)
1781 			if (dtype_match(&label, dtype))
1782 				break;
1783 		/*
1784 		 * If it disagrees with our current type, something
1785 		 * real bad is happening.
1786 		 */
1787 		if (dtype != cur_dtype) {
1788 			if (dtype == NULL) {
1789 				fmt_print("\
1790 Unknown disk type in backup label\n");
1791 				exit_critical();
1792 				return (-1);
1793 			}
1794 			fmt_print("Backup label claims different type:\n");
1795 			fmt_print("    <%s cyl %d alt %d hd %d sec %d>\n",
1796 				label.dkl_asciilabel, label.dkl_ncyl,
1797 				label.dkl_acyl, label.dkl_nhead,
1798 				label.dkl_nsect);
1799 			if (check("Continue")) {
1800 				exit_critical();
1801 				return (-1);
1802 			}
1803 			cur_dtype = dtype;
1804 		}
1805 		/*
1806 		 * Try to match the partition map with a known map.
1807 		 */
1808 		for (parts = dtype->dtype_plist; parts != NULL;
1809 		    parts = parts->pinfo_next)
1810 			if (parts_match(&label, parts))
1811 				break;
1812 		/*
1813 		 * If we couldn't match it, allocate space for a new one,
1814 		 * fill in the info, and add it to the list.  The name
1815 		 * for the new map is derived from the disk name.
1816 		 */
1817 		if (parts == NULL) {
1818 			parts = (struct partition_info *)
1819 				zalloc(sizeof (struct partition_info));
1820 			plist = dtype->dtype_plist;
1821 			if (plist == NULL)
1822 				dtype->dtype_plist = parts;
1823 			else {
1824 				while (plist->pinfo_next != NULL)
1825 					plist = plist->pinfo_next;
1826 				plist->pinfo_next = parts;
1827 			}
1828 			parts->pinfo_name = alloc_string("original");
1829 			for (i = 0; i < NDKMAP; i++)
1830 
1831 #if defined(_SUNOS_VTOC_8)
1832 				parts->pinfo_map[i] = label.dkl_map[i];
1833 
1834 #elif defined(_SUNOS_VTOC_16)
1835 				parts->pinfo_map[i].dkl_cylno  =
1836 				    label.dkl_vtoc.v_part[i].p_start / spc();
1837 				parts->pinfo_map[i].dkl_nblk =
1838 				    label.dkl_vtoc.v_part[i].p_size;
1839 #else
1840 #error No VTOC layout defined.
1841 #endif /* defined(_SUNOS_VTOC_8) */
1842 			parts->vtoc = label.dkl_vtoc;
1843 		}
1844 		/*
1845 		 * We now have a partition map.  Make it the current map.
1846 		 */
1847 		cur_disk->disk_parts = cur_parts = parts;
1848 		exit_critical();
1849 		/*
1850 		 * Rewrite the labels and defect lists, as appropriate.
1851 		 */
1852 		if (EMBEDDED_SCSI) {
1853 			fmt_print("Restoring primary label.\n");
1854 			if (write_label())
1855 				return (-1);
1856 		} else {
1857 			fmt_print("Restoring primary label and defect list.\n");
1858 			if (write_label())
1859 				return (-1);
1860 			if (cur_list.list != NULL)
1861 				write_deflist(&cur_list);
1862 		}
1863 		fmt_print("\n");
1864 		return (0);
1865 	}
1866 	/*
1867 	 * If we didn't find any backup labels, say so.
1868 	 */
1869 	fmt_print("not found.\n\n");
1870 	return (0);
1871 }
1872 
1873 /*
1874  * This routine is called by c_verify() for an EFI labeled disk
1875  */
1876 static int
1877 c_verify_efi()
1878 {
1879 	struct efi_info efi_info;
1880 	struct	partition_info	tmp_pinfo;
1881 	int status;
1882 
1883 	status = read_efi_label(cur_file, &efi_info);
1884 	if (status != 0) {
1885 	    err_print("Warning: Could not read label.\n");
1886 	    return (-1);
1887 	}
1888 	if (cur_parts->etoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
1889 		err_print("Reading the primary EFI GPT label ");
1890 		err_print("failed.  Using backup label.\n");
1891 		err_print("Use the 'backup' command to restore ");
1892 		err_print("the primary label.\n");
1893 	}
1894 	tmp_pinfo.etoc = efi_info.e_parts;
1895 	fmt_print("\n");
1896 	if (cur_parts->etoc->efi_parts[8].p_name) {
1897 	    fmt_print("Volume name = <%8s>\n",
1898 		cur_parts->etoc->efi_parts[8].p_name);
1899 	} else {
1900 	    fmt_print("Volume name = <        >\n");
1901 	}
1902 	fmt_print("ascii name  = ");
1903 	print_efi_string(efi_info.vendor, efi_info.product,
1904 	    efi_info.revision, efi_info.capacity);
1905 	fmt_print("\n");
1906 
1907 	fmt_print("bytes/sector	=  %d\n", DEV_BSIZE);
1908 	fmt_print("sectors = %llu\n", cur_parts->etoc->efi_last_lba);
1909 	fmt_print("accessible sectors = %llu\n",
1910 		cur_parts->etoc->efi_last_u_lba);
1911 
1912 	print_map(&tmp_pinfo);
1913 	return (0);
1914 }
1915 
1916 /*
1917  * This routine implements the 'verify' command.  It allows the user
1918  * to read the labels on the current disk.
1919  */
1920 int
1921 c_verify()
1922 {
1923 	struct	dk_label p_label, b_label, *label;
1924 	struct	partition_info tmp_pinfo;
1925 	daddr_t	bn;
1926 	int	sec, head, i, status;
1927 	int	p_label_bad = 0;
1928 	int	b_label_bad = 0;
1929 	int	p_label_found = 0;
1930 	int	b_label_found = 0;
1931 	char	id_str[128];
1932 
1933 	/*
1934 	 * There must be a current disk type (and therefore a current disk).
1935 	 */
1936 	if (cur_dtype == NULL) {
1937 		err_print("Current Disk Type is not set.\n");
1938 		return (-1);
1939 	}
1940 	/*
1941 	 * The disk must be formatted to read labels.
1942 	 */
1943 	if (!(cur_flags & DISK_FORMATTED)) {
1944 		err_print("Current Disk is unformatted.\n");
1945 		return (-1);
1946 	}
1947 	/*
1948 	 * Check for a valid fdisk table entry for Solaris
1949 	 */
1950 	if (!good_fdisk()) {
1951 		return (-1);
1952 	}
1953 	/*
1954 	 * Branch off here if the disk is EFI labelled.
1955 	 */
1956 	if (cur_label == L_TYPE_EFI) {
1957 	    return (c_verify_efi());
1958 	}
1959 	/*
1960 	 * Attempt to read the primary label.
1961 	 */
1962 	status = read_label(cur_file, &p_label);
1963 	if (status == -1) {
1964 		err_print("Warning: Could not read primary label.\n");
1965 		p_label_bad = 1;
1966 	} else {
1967 		/*
1968 		 * Verify that it is a reasonable label.
1969 		 */
1970 		/*
1971 		 * Save complete ascii string for printing later.
1972 		 */
1973 		(void) strncpy(id_str, p_label.dkl_asciilabel, 128);
1974 
1975 		if ((!checklabel((struct dk_label *)&p_label)) ||
1976 			(trim_id(p_label.dkl_asciilabel))) {
1977 			err_print("\
1978 Warning: Primary label appears to be corrupt.\n");
1979 			p_label_bad = 1;
1980 		} else {
1981 			p_label_found = 1;
1982 			/*
1983 			 * Make sure it matches current label
1984 			 */
1985 			if ((!dtype_match(&p_label, cur_dtype)) ||
1986 				(!parts_match(&p_label, cur_parts))) {
1987 				err_print("\
1988 Warning: Primary label on disk appears to be different from\ncurrent label.\n");
1989 			p_label_bad = 1;
1990 			}
1991 		}
1992 	}
1993 
1994 	/*
1995 	 * Read backup labels.
1996 	 * Some disks have the backup labels in a strange place.
1997 	 */
1998 	if (cur_ctype->ctype_flags & CF_BLABEL)
1999 		head = 2;
2000 	else
2001 		head = nhead - 1;
2002 	/*
2003 	 * Loop through each copy of the backup label.
2004 	 */
2005 	for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect));
2006 	    sec += 2) {
2007 		bn = chs2bn(ncyl + acyl - 1, head, sec) + solaris_offset;
2008 		/*
2009 		 * Attempt to read it.
2010 		 */
2011 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, (diskaddr_t)bn,
2012 				1, (char *)&b_label, F_NORMAL, NULL))
2013 			continue;
2014 		/*
2015 		 * Verify that it is a reasonable label.
2016 		 */
2017 		if (!checklabel(&b_label))
2018 			continue;
2019 
2020 		/*
2021 		 * Save complete label only if no primary label exists
2022 		 */
2023 		if (!p_label_found)
2024 			(void) strncpy(id_str, b_label.dkl_asciilabel, 128);
2025 
2026 		if (trim_id(b_label.dkl_asciilabel))
2027 			continue;
2028 		b_label_found = 1;
2029 		/*
2030 		 * Compare against primary label
2031 		 */
2032 		if (p_label_found) {
2033 			if ((strcmp(b_label.dkl_asciilabel,
2034 				p_label.dkl_asciilabel) != 0) ||
2035 				(b_label.dkl_ncyl != p_label.dkl_ncyl) ||
2036 				(b_label.dkl_acyl != p_label.dkl_acyl) ||
2037 				(b_label.dkl_nhead != p_label.dkl_nhead) ||
2038 				(b_label.dkl_nsect != p_label.dkl_nsect)) {
2039 				b_label_bad = 1;
2040 			} else {
2041 				for (i = 0; i < NDKMAP; i++) {
2042 #if defined(_SUNOS_VTOC_8)
2043 					if ((b_label.dkl_map[i].dkl_cylno !=
2044 					    p_label.dkl_map[i].dkl_cylno) ||
2045 					    (b_label.dkl_map[i].dkl_nblk !=
2046 					    p_label.dkl_map[i].dkl_nblk)) {
2047 						b_label_bad = 1;
2048 						break;
2049 					}
2050 
2051 #elif defined(_SUNOS_VTOC_16)
2052 					if ((b_label.dkl_vtoc.v_part[i].p_tag !=
2053 					p_label.dkl_vtoc.v_part[i].p_tag) ||
2054 					(b_label.dkl_vtoc.v_part[i].p_flag !=
2055 					p_label.dkl_vtoc.v_part[i].p_flag) ||
2056 					(b_label.dkl_vtoc.v_part[i].p_start !=
2057 					p_label.dkl_vtoc.v_part[i].p_start) ||
2058 					(b_label.dkl_vtoc.v_part[i].p_size !=
2059 					p_label.dkl_vtoc.v_part[i].p_size)) {
2060 						b_label_bad = 1;
2061 						break;
2062 					}
2063 #else
2064 #error No VTOC layout defined.
2065 #endif /* defined(_SUNOS_VTOC_8) */
2066 				}
2067 			}
2068 		}
2069 		if (b_label_bad)
2070 			err_print(
2071 "Warning: Primary and backup labels do not match.\n");
2072 		break;
2073 	}
2074 	/*
2075 	 * If we didn't find any backup labels, say so.
2076 	 */
2077 	if (!b_label_found)
2078 		err_print("Warning: Could not read backup labels.\n");
2079 
2080 	if ((!b_label_found) || (p_label_bad) || (b_label_bad))
2081 		err_print("\n\
2082 Warning: Check the current partitioning and 'label' the disk or use the\n\
2083 \t 'backup' command.\n");
2084 
2085 	/*
2086 	 * Print label information.
2087 	 */
2088 	if (p_label_found) {
2089 		fmt_print("\nPrimary label contents:\n");
2090 		label = &p_label;
2091 	} else if (b_label_found) {
2092 		fmt_print("\nBackup label contents:\n");
2093 		label = &b_label;
2094 	} else {
2095 		return (0);
2096 	}
2097 
2098 	/*
2099 	 * Must put info into partition_info struct for
2100 	 * for print routine.
2101 	 */
2102 	bzero(&tmp_pinfo, sizeof (struct partition_info));
2103 	for (i = 0; i < NDKMAP; i++) {
2104 
2105 #if defined(_SUNOS_VTOC_8)
2106 		tmp_pinfo.pinfo_map[i] = label->dkl_map[i];
2107 
2108 #elif defined(_SUNOS_VTOC_16)
2109 		tmp_pinfo.pinfo_map[i].dkl_cylno =
2110 			label->dkl_vtoc.v_part[i].p_start / spc();
2111 		tmp_pinfo.pinfo_map[i].dkl_nblk =
2112 			label->dkl_vtoc.v_part[i].p_size;
2113 #else
2114 #error No VTOC layout defined.
2115 #endif /* defined(_SUNOS_VTOC_8) */
2116 	}
2117 	tmp_pinfo.vtoc = label->dkl_vtoc;
2118 
2119 	fmt_print("\n");
2120 	fmt_print("Volume name = <%8s>\n", label->dkl_vtoc.v_volume);
2121 	fmt_print("ascii name  = <%s>\n", id_str);
2122 	fmt_print("pcyl        = %4d\n", label->dkl_pcyl);
2123 	fmt_print("ncyl        = %4d\n", label->dkl_ncyl);
2124 	fmt_print("acyl        = %4d\n", label->dkl_acyl);
2125 
2126 #if defined(_SUNOS_VTOC_16)
2127 	fmt_print("bcyl        = %4d\n", label->dkl_bcyl);
2128 #endif /* defined(_SUNOS_VTOC_16) */
2129 
2130 	fmt_print("nhead       = %4d\n", label->dkl_nhead);
2131 	fmt_print("nsect       = %4d\n", label->dkl_nsect);
2132 
2133 	print_map(&tmp_pinfo);
2134 	return (0);
2135 }
2136 
2137 
2138 /*
2139  * This command implements the inquiry command, for embedded SCSI
2140  * disks only, which issues a SCSI inquiry command, and
2141  * displays the resulting vendor, product id and revision level.
2142  */
2143 int
2144 c_inquiry()
2145 {
2146 	char			inqbuf[255];
2147 	struct scsi_inquiry	*inq;
2148 
2149 	assert(SCSI);
2150 
2151 	inq = (struct scsi_inquiry *)inqbuf;
2152 
2153 	if (uscsi_inquiry(cur_file, inqbuf, sizeof (inqbuf))) {
2154 		err_print("Failed\n");
2155 		return (-1);
2156 	} else {
2157 		fmt_print("Vendor:   ");
2158 		print_buf(inq->inq_vid, sizeof (inq->inq_vid));
2159 		fmt_print("\nProduct:  ");
2160 		print_buf(inq->inq_pid, sizeof (inq->inq_pid));
2161 		fmt_print("\nRevision: ");
2162 		print_buf(inq->inq_revision, sizeof (inq->inq_revision));
2163 		fmt_print("\n");
2164 	}
2165 
2166 	return (0);
2167 }
2168 
2169 
2170 /*
2171  * This routine allows the user to set the 8-character
2172  * volume name in the vtoc.  It then writes both the
2173  * primary and backup labels onto the current disk.
2174  */
2175 int
2176 c_volname()
2177 {
2178 	int	 status;
2179 	char	*prompt;
2180 	union {
2181 		int	xfoo;
2182 		char	defvolname[LEN_DKL_VVOL+1];
2183 	} x;
2184 	char    s1[MAXPATHLEN], nclean[MAXPATHLEN];
2185 	char	*volname;
2186 
2187 
2188 	/*
2189 	 * There must be a current disk type (and therefore a current disk).
2190 	 */
2191 	if (cur_dtype == NULL) {
2192 		err_print("Current Disk Type is not set.\n");
2193 		return (-1);
2194 	}
2195 	/*
2196 	 * The current disk must be formatted to label it.
2197 	 */
2198 	if (!(cur_flags & DISK_FORMATTED)) {
2199 		err_print("Current Disk is unformatted.\n");
2200 		return (-1);
2201 	}
2202 	/*
2203 	 * Check for a valid fdisk table entry for Solaris
2204 	 */
2205 	if (!good_fdisk()) {
2206 		return (-1);
2207 	}
2208 	/*
2209 	 * The current disk must be formatted to label it.
2210 	 */
2211 	if (cur_parts == NULL) {
2212 	err_print(
2213 "Please select a partition map for the disk first.\n");
2214 	return (-1);
2215 	}
2216 
2217 	/*
2218 	 * Check to see if there are any mounted file systems anywhere
2219 	 * on the current disk.  If so, refuse to label the disk, but
2220 	 * only if the partitions would change for the mounted partitions.
2221 	 *
2222 	 */
2223 	if (checkmount((daddr_t)-1, (daddr_t)-1)) {
2224 		/* Bleagh, too descriptive */
2225 		if (check_label_with_mount()) {
2226 			err_print(
2227 "Cannot label disk while it has mounted partitions.\n\n");
2228 			return (-1);
2229 		}
2230 	}
2231 
2232 	/*
2233 	 * Check to see if there are partitions being used for swapping
2234 	 * on the current disk.  If so, refuse to label the disk, but
2235 	 * only if the partitions would change for the swap partitions.
2236 	 *
2237 	 */
2238 	if (checkswap((daddr_t)-1, (daddr_t)-1)) {
2239 		/* Bleagh, too descriptive */
2240 		if (check_label_with_swap()) {
2241 			err_print(
2242 "Cannot label disk while its partitions are currently \
2243 being used for swapping.\n\n");
2244 			return (-1);
2245 		}
2246 	}
2247 
2248 	/*
2249 	 * Check to see if any partitions used for svm, vxvm, ZFS zpool
2250 	 * or live upgrade are on the disk. If so, refuse to label the
2251 	 * disk, but only if we are trying to shrink a partition in
2252 	 * use.
2253 	 */
2254 	if (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
2255 	    (diskaddr_t)-1, 0, 1)) {
2256 		err_print("Cannot label disk while its partitions "
2257 		    "are in use as described.\n");
2258 		return (-1);
2259 	}
2260 
2261 	/*
2262 	 * Prompt for the disk volume name.
2263 	 */
2264 	prompt = "Enter 8-character volume name (remember quotes)";
2265 	bzero(x.defvolname, LEN_DKL_VVOL+1);
2266 	bcopy(cur_disk->v_volume, x.defvolname, LEN_DKL_VVOL);
2267 	/*
2268 	 *  Get the input using "get_inputline" since
2269 	 *  input would never return null string.
2270 	 */
2271 	fmt_print("%s[\"%s\"]:", prompt, x.defvolname);
2272 
2273 	/*
2274 	 * Get input from the user.
2275 	 */
2276 	get_inputline(nclean, MAXPATHLEN);
2277 	clean_token(s1, nclean);
2278 	/*
2279 	 * check for return.
2280 	 */
2281 	if (s1[0] == 0) {
2282 		volname = x.defvolname;
2283 	} else {
2284 		/*
2285 		 * remove the " mark from volname.
2286 		 */
2287 		if (s1[0] == '"') {
2288 			int i = 1;
2289 			volname = &s1[1];
2290 			while (s1[i] != '"' && s1[i] != '\0')
2291 				i++;
2292 			s1[i] = '\0';
2293 			clean_token(nclean, volname);
2294 			volname = nclean;
2295 		} else {
2296 			(void) sscanf(&s1[0], "%1024s", nclean);
2297 			volname = nclean;
2298 		};
2299 	}
2300 	/*
2301 	 * Make sure the user is serious.
2302 	 */
2303 	if (check("Ready to label disk, continue")) {
2304 		fmt_print("\n");
2305 		return (-1);
2306 	}
2307 	/*
2308 	 * Use the volume name chosen above
2309 	 */
2310 	bzero(cur_disk->v_volume, LEN_DKL_VVOL);
2311 	bcopy(volname, cur_disk->v_volume, min((int)strlen(volname),
2312 	    LEN_DKL_VVOL));
2313 	if (cur_label == L_TYPE_EFI) {
2314 	    bzero(cur_parts->etoc->efi_parts[8].p_name, LEN_DKL_VVOL);
2315 	    bcopy(volname, cur_parts->etoc->efi_parts[8].p_name,
2316 		LEN_DKL_VVOL);
2317 	}
2318 	/*
2319 	 * Write the labels out (this will also notify unix) and
2320 	 * return status.
2321 	 */
2322 	fmt_print("\n");
2323 	if (status = write_label())
2324 		err_print("Label failed.\n");
2325 	return (status);
2326 }
2327