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