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