xref: /titanic_51/usr/src/cmd/format/main.c (revision 853389e344087efe23a5cc688693e5301a1cb90d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 /*
28  * This file contains the main entry point of the program and other
29  * routines relating to the general flow.
30  */
31 #include "global.h"
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <signal.h>
36 #include <memory.h>
37 #include <string.h>
38 #include <errno.h>
39 
40 #ifdef sparc
41 #include <sys/hdio.h>
42 #include <sys/dkbad.h>
43 #endif
44 
45 #include <sys/time.h>
46 #include "main.h"
47 #include "analyze.h"
48 #include "menu.h"
49 #include "param.h"
50 #include "misc.h"
51 #include "startup.h"
52 #include "menu_command.h"
53 #include "menu_partition.h"
54 #include "prompts.h"
55 #include "checkdev.h"
56 #include "label.h"
57 
58 extern	struct menu_item menu_command[];
59 
60 #ifdef	__STDC__
61 
62 /*
63  *	Local prototypes for ANSI C compilers
64  */
65 static void	get_disk_characteristics(void);
66 
67 
68 #else	/* __STDC__ */
69 
70 /*
71  *	Local prototypes for non-ANSI C compilers
72  */
73 static void	get_disk_characteristics();
74 
75 #endif	/* __STDC__ */
76 
77 /*
78  * This is the main entry point.
79  */
80 int
81 main(int argc, char *argv[])
82 {
83 	int	i;
84 	int	ret_code = 1;
85 	char	**arglist;
86 	struct	disk_info *disk = NULL;
87 	struct	disk_type *type, *oldtype;
88 	struct	partition_info *parts;
89 	struct	sigaction act;
90 
91 	solaris_offset = 0;
92 	/*
93 	 * Initialize cur_ctype to avoid null pointer dereference
94 	 * in auto_efi_sense().
95 	 */
96 	cur_ctype = (struct ctlr_type *)NULL;
97 	/*
98 	 * Decode the command line options.
99 	 */
100 	i = do_options(argc, argv);
101 	/*
102 	 * If we are to run from a command file, open it up.
103 	 */
104 	if (option_f) {
105 		if (freopen(option_f, "r", stdin) == NULL) {
106 			err_print("Unable to open command file '%s'.\n",
107 			    option_f);
108 			fullabort();
109 		}
110 	}
111 	/*
112 	 * If we are logging, open the log file.
113 	 */
114 	if (option_l) {
115 		if ((log_file = fopen(option_l, "w")) == NULL) {
116 			err_print("Unable to open log file '%s'.\n",
117 			    option_l);
118 			fullabort();
119 		}
120 	}
121 	/*
122 	 * Read in the data file and initialize the hardware structs.
123 	 */
124 	sup_init();
125 	/*
126 	 * If there are no disks on the command line, search the
127 	 * appropriate device directory for character devices that
128 	 * look like disks.
129 	 */
130 	if (i < 0) {
131 		arglist = (char **)NULL;
132 	/*
133 	 * There were disks on the command line.  They comprise the
134 	 * search list.
135 	 */
136 	} else {
137 		arglist = &argv[i];
138 	}
139 	/*
140 	 * Perform the search for disks.
141 	 */
142 	do_search(arglist);
143 	/*
144 	 * Catch ctrl-C and ctrl-Z so critical sections can be
145 	 * implemented.  We use sigaction, as this sets up the
146 	 * signal handler permanently, and also automatically
147 	 * restarts any interrupted system call.
148 	 */
149 	act.sa_handler = cmdabort;
150 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
151 	act.sa_flags = SA_RESTART | SA_NODEFER;
152 	if (sigaction(SIGINT, &act, (struct sigaction *)NULL) == -1) {
153 		err_print("sigaction(SIGINT) failed - %s\n",
154 		    strerror(errno));
155 		fullabort();
156 	}
157 
158 	act.sa_handler = onsusp;
159 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
160 	act.sa_flags = SA_RESTART | SA_NODEFER;
161 	if (sigaction(SIGTSTP, &act, (struct sigaction *)NULL) == -1) {
162 		err_print("sigaction(SIGTSTP) failed - %s\n",
163 		    strerror(errno));
164 		fullabort();
165 	}
166 
167 	act.sa_handler = onalarm;
168 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
169 	act.sa_flags = SA_RESTART;
170 	if (sigaction(SIGALRM, &act, (struct sigaction *)NULL) == -1) {
171 		err_print("sigaction(SIGALRM) failed - %s\n",
172 		    strerror(errno));
173 		fullabort();
174 	}
175 
176 	/*
177 	 * If there was only 1 disk on the command line, mark it
178 	 * to be the current disk.  If it wasn't found, it's an error.
179 	 */
180 	if (i == argc - 1) {
181 		disk = disk_list;
182 		if (disk == NULL) {
183 			err_print("Unable to find specified disk '%s'.\n",
184 			    argv[i]);
185 			fullabort();
186 		}
187 	}
188 	/*
189 	 * A disk was forced on the command line.
190 	 */
191 	if (option_d) {
192 		/*
193 		 * Find it in the list of found disks and mark it to
194 		 * be the current disk.
195 		 */
196 		for (disk = disk_list; disk != NULL; disk = disk->disk_next)
197 			if (diskname_match(option_d, disk))
198 				break;
199 		/*
200 		 * If it wasn't found, it's an error.
201 		 */
202 		if (disk == NULL) {
203 			err_print("Unable to find specified disk '%s'.\n",
204 			    option_d);
205 			fullabort();
206 		}
207 	}
208 	/*
209 	 * A disk type was forced on the command line.
210 	 */
211 	if (option_t != NULL) {
212 		/*
213 		 * Only legal if a disk was also forced.
214 		 */
215 		if (disk == NULL) {
216 			err_print("Must specify disk as well as type.\n");
217 			fullabort();
218 		}
219 		oldtype = disk->disk_type;
220 		/*
221 		 * Find the specified type in the list of legal types
222 		 * for the disk.
223 		 */
224 		for (type = disk->disk_ctlr->ctlr_ctype->ctype_dlist;
225 		    type != NULL; type = type->dtype_next)
226 			if (strcmp(option_t, type->dtype_asciilabel) == 0)
227 				break;
228 		/*
229 		 * If it wasn't found, it's an error.
230 		 */
231 		if (type == NULL) {
232 			err_print(
233 "Specified type '%s' is not a known type.\n", option_t);
234 			fullabort();
235 		}
236 		/*
237 		 * If the specified type is not the same as the type
238 		 * in the disk label, update the type and nullify the
239 		 * partition map.
240 		 */
241 		if (type != oldtype) {
242 			disk->disk_type = type;
243 			disk->disk_parts = NULL;
244 		}
245 	}
246 	/*
247 	 * A partition map was forced on the command line.
248 	 */
249 	if (option_p) {
250 		/*
251 		 * Only legal if both disk and type were also forced.
252 		 */
253 		if (disk == NULL || disk->disk_type == NULL) {
254 			err_print("Must specify disk and type as well ");
255 			err_print("as partitiion.\n");
256 			fullabort();
257 		}
258 		/*
259 		 * Find the specified map in the list of legal maps
260 		 * for the type.
261 		 */
262 		for (parts = disk->disk_type->dtype_plist; parts != NULL;
263 		    parts = parts->pinfo_next)
264 			if (strcmp(option_p, parts->pinfo_name) == 0)
265 				break;
266 		/*
267 		 * If it wasn't found, it's an error.
268 		 */
269 		if (parts == NULL) {
270 			err_print(
271 "Specified table '%s' is not a known table.\n", option_p);
272 			fullabort();
273 		}
274 		/*
275 		 * Update the map.
276 		 */
277 		disk->disk_parts = parts;
278 	}
279 	/*
280 	 * If a disk was marked to become current, initialize the state
281 	 * to make it current.  If not, ask user to pick one.
282 	 */
283 	if (disk != NULL) {
284 		init_globals(disk);
285 	} else if (option_f == 0 && option_d == 0) {
286 		while (ret_code) {
287 			ret_code = c_disk();
288 		}
289 	}
290 
291 #ifdef	BUG1134748
292 	/*
293 	 * if -f command-file is specified, check for disk and disktype
294 	 * input also. For SCSI disks, the type input may not be needed
295 	 * since format would have figured that using inquiry information.
296 	 */
297 	if (option_f) {
298 		if (cur_disk == NULL) {
299 			err_print("Must specify a disk using -d option.\n");
300 			fullabort();
301 		}
302 		if (cur_dtype == NULL) {
303 			err_print("Must specify disk as well as type.\n");
304 			fullabort();
305 		}
306 	}
307 #endif	/* BUG1134748 */
308 
309 	/*
310 	 * Run the command menu.
311 	 */
312 	cur_menu = last_menu = 0;
313 	run_menu(menu_command, "FORMAT", "format", 1);
314 
315 	/*
316 	 * normal ending. Explicitly return(0);
317 	 */
318 	return (0);
319 }
320 
321 /*
322  * This routine initializes the internal state to ready it for a new
323  * current disk.  There are a zillion state variables that store
324  * information on the current disk, and they must all be updated.
325  * We also tell SunOS about the disk, since it may not know if the
326  * disk wasn't labeled at boot time.
327  */
328 void
329 init_globals(disk)
330 	struct	disk_info *disk;
331 {
332 	int		status;
333 	int		found_mount;
334 	int		found_inuse;
335 #ifdef sparc
336 	int		i;
337 	caddr_t		bad_ptr = (caddr_t)&badmap;
338 #endif
339 
340 	/*
341 	 * If there was an old current disk, close the file for it.
342 	 */
343 	if (cur_disk != NULL)
344 		(void) close(cur_file);
345 	/*
346 	 * Kill off any defect lists still lying around.
347 	 */
348 	kill_deflist(&cur_list);
349 	kill_deflist(&work_list);
350 	/*
351 	 * If there were any buffers, free them up.
352 	 */
353 	if ((char *)cur_buf != NULL) {
354 		destroy_data((char *)cur_buf);
355 		cur_buf = NULL;
356 	}
357 	if ((char *)pattern_buf != NULL) {
358 		destroy_data((char *)pattern_buf);
359 		pattern_buf = NULL;
360 	}
361 	/*
362 	 * Fill in the hardware struct pointers for the new disk.
363 	 */
364 	cur_disk = disk;
365 	cur_dtype = cur_disk->disk_type;
366 	cur_label = cur_disk->label_type;
367 	cur_ctlr = cur_disk->disk_ctlr;
368 	cur_parts = cur_disk->disk_parts;
369 	cur_ctype = cur_ctlr->ctlr_ctype;
370 	cur_ops = cur_ctype->ctype_ops;
371 	cur_flags = 0;
372 	/*
373 	 * Open a file for the new disk.
374 	 */
375 	if ((cur_file = open_disk(cur_disk->disk_path,
376 					O_RDWR | O_NDELAY)) < 0) {
377 		err_print(
378 "Error: can't open selected disk '%s'.\n", cur_disk->disk_name);
379 		fullabort();
380 	}
381 #ifdef sparc
382 	/*
383 	 * If the new disk uses bad-144, initialize the bad block table.
384 	 */
385 	if (cur_ctlr->ctlr_flags & DKI_BAD144) {
386 		badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0;
387 		for (i = 0; i < NDKBAD; i++) {
388 			badmap.bt_bad[i].bt_cyl = -1;
389 			badmap.bt_bad[i].bt_trksec = -1;
390 		}
391 	}
392 #endif
393 	/*
394 	 * If the type of the new disk is known...
395 	 */
396 	if (cur_dtype != NULL) {
397 		/*
398 		 * Initialize the physical characteristics.
399 		 * If need disk specs, prompt for undefined disk
400 		 * characteristics.  If running from a file,
401 		 * use defaults.
402 		 */
403 		if (cur_dtype->dtype_flags & DT_NEED_SPEFS) {
404 			get_disk_characteristics();
405 			cur_dtype->dtype_flags &= ~DT_NEED_SPEFS;
406 		}
407 
408 		ncyl = cur_dtype->dtype_ncyl;
409 		acyl = cur_dtype->dtype_acyl;
410 		pcyl = cur_dtype->dtype_pcyl;
411 		nhead = cur_dtype->dtype_nhead;
412 		nsect = cur_dtype->dtype_nsect;
413 		phead = cur_dtype->dtype_phead;
414 		psect = cur_dtype->dtype_psect;
415 		/*
416 		 * Alternates per cylinder are forced to 0 or 1,
417 		 * independent of what the label says.  This works
418 		 * because we know which ctlr we are dealing with.
419 		 */
420 		if (cur_ctype->ctype_flags & CF_APC)
421 			apc = 1;
422 		else
423 			apc = 0;
424 		/*
425 		 * Initialize the surface analysis info.  We always start
426 		 * out with scan set for the whole disk.  Note,
427 		 * for SCSI disks, we can only scan the data area.
428 		 */
429 		scan_lower = 0;
430 		scan_size = BUF_SECTS;
431 		if ((cur_ctype->ctype_flags & CF_SCSI) &&
432 		    (cur_disk->label_type == L_TYPE_SOLARIS)) {
433 			scan_upper = datasects() - 1;
434 		} else if (cur_disk->label_type == L_TYPE_SOLARIS) {
435 			scan_upper = physsects() - 1;
436 		} else if (cur_disk->label_type == L_TYPE_EFI) {
437 			scan_upper = cur_parts->etoc->efi_last_lba;
438 		}
439 
440 		/*
441 		 * Allocate the buffers.
442 		 */
443 		cur_buf = (void *) zalloc(BUF_SECTS * SECSIZE);
444 		pattern_buf = (void *) zalloc(BUF_SECTS * SECSIZE);
445 
446 		/*
447 		 * Tell the user which disk (s)he selected.
448 		 */
449 		if (chk_volname(cur_disk)) {
450 			fmt_print("selecting %s: ", cur_disk->disk_name);
451 			print_volname(cur_disk);
452 			fmt_print("\n");
453 		} else {
454 			fmt_print("selecting %s\n", cur_disk->disk_name);
455 		}
456 
457 		/*
458 		 * If the drive is formatted...
459 		 */
460 		if ((*cur_ops->op_ck_format)()) {
461 			/*
462 			 * Mark it formatted.
463 			 */
464 			cur_flags |= DISK_FORMATTED;
465 			/*
466 			 * Read the defect list, if we have one.
467 			 */
468 			if (!EMBEDDED_SCSI) {
469 				read_list(&cur_list);
470 			}
471 #ifdef sparc
472 			/*
473 			 * If the disk does BAD-144, we do an ioctl to
474 			 * tell SunOS about the bad block table.
475 			 */
476 			if (cur_ctlr->ctlr_flags & DKI_BAD144) {
477 				if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr)) {
478 					err_print(
479 "Warning: error telling SunOS bad block map table.\n");
480 				}
481 			}
482 #endif
483 			fmt_print("[disk formatted");
484 			if (!EMBEDDED_SCSI) {
485 				if (cur_list.list != NULL) {
486 					fmt_print(", defect list found");
487 				} else {
488 					fmt_print(", no defect list found");
489 				}
490 			}
491 			fmt_print("]");
492 		/*
493 		 * Drive wasn't formatted.  Tell the user in case he
494 		 * disagrees.
495 		 */
496 		} else if (EMBEDDED_SCSI) {
497 			fmt_print("[disk unformatted]");
498 		} else {
499 			/*
500 			 * Make sure the user is serious.  Note, for
501 			 * SCSI disks since this is instantaneous, we
502 			 * will just do it and not ask for confirmation.
503 			 */
504 			status = 0;
505 			if (!(cur_ctype->ctype_flags & CF_CONFIRM)) {
506 				if (check("\n\
507 Ready to get manufacturer's defect list from unformatted drive.\n\
508 This cannot be interrupted and takes a long while.\n\
509 Continue"))
510 					status = 1;
511 				else
512 					fmt_print(
513 				"Extracting manufacturer's defect list...");
514 			}
515 			/*
516 			 * Extract manufacturer's defect list.
517 			 */
518 			if ((status == 0) && (cur_ops->op_ex_man != NULL)) {
519 				status = (*cur_ops->op_ex_man)(&cur_list);
520 			} else {
521 				status = 1;
522 			}
523 			fmt_print("[disk unformatted");
524 			if (status != 0) {
525 				fmt_print(", no defect list found]");
526 			} else {
527 				fmt_print(", defect list found]");
528 			}
529 		}
530 	} else {
531 		/*
532 		 * Disk type is not known.
533 		 * Initialize physical characteristics to 0 and tell the
534 		 * user we don't know what type the disk is.
535 		 */
536 		ncyl = acyl = nhead = nsect = psect = 0;
537 	}
538 
539 	fmt_print("\n");
540 
541 	/*
542 	 * Check to see if there are any mounted file systems on the
543 	 * disk.  If there are, print a warning.
544 	 */
545 	if ((found_mount = checkmount((daddr_t)-1, (daddr_t)-1)) != 0)
546 		err_print("Warning: Current Disk has mounted partitions.\n");
547 
548 	/*
549 	 * If any part of this device is also part of an SVM, VxVM or
550 	 * Live Upgrade device, print a warning.
551 	 */
552 	found_inuse =  checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1,
553 	    (diskaddr_t)-1, 1, 0);
554 
555 	/*
556 	 * Get the Solaris Fdisk Partition information
557 	 */
558 	(void) copy_solaris_part(&cur_disk->fdisk_part);
559 
560 	if (!found_mount && !found_inuse &&
561 	    cur_disk->label_type == L_TYPE_EFI) {
562 
563 		/*
564 		 * If alter_lba is 1, we are using the backup label.
565 		 * Since we can locate the backup label by disk capacity,
566 		 * there must be no space expanded after backup label.
567 		 */
568 		if ((cur_parts->etoc->efi_altern_lba != 1) &&
569 		    (cur_parts->etoc->efi_altern_lba <
570 		    cur_parts->etoc->efi_last_lba)) {
571 
572 			/*
573 			 * Lun expansion detected. Prompt user now and actually
574 			 * adjust the label in <partition> command.
575 			 */
576 			fmt_print(
577 "Note: capacity in disk label is smaller than the real disk capacity.\n\
578 Select <partition> <expand> to adjust the label capacity. \n");
579 		}
580 	}
581 }
582 
583 
584 /*
585  * Prompt for some undefined disk characteristics.
586  * Used when there is no disk definition, but the
587  * disk has a valid label, so basically we're
588  * prompting for everything that isn't in the label.
589  */
590 static void
591 get_disk_characteristics()
592 {
593 	/*
594 	 * The need_spefs flag is used to tell us that this disk
595 	 * is not a known type and the ctlr specific info must
596 	 * be prompted for.  We only prompt for the info that applies
597 	 * to this ctlr.
598 	 */
599 	assert(cur_dtype->dtype_flags & DT_NEED_SPEFS);
600 
601 	/*
602 	 * If we're running with input from a file, use
603 	 * reasonable defaults, since prompting for the
604 	 * information will probably mess things up.
605 	 */
606 	if (option_f) {
607 		cur_dtype->dtype_pcyl = ncyl + acyl;
608 		cur_dtype->dtype_rpm = AVG_RPM;
609 		cur_dtype->dtype_bpt = INFINITY;
610 		cur_dtype->dtype_phead = 0;
611 		cur_dtype->dtype_psect = 0;
612 		cur_dtype->dtype_cyl_skew = 0;
613 		cur_dtype->dtype_trk_skew = 0;
614 		cur_dtype->dtype_trks_zone = 0;
615 		cur_dtype->dtype_atrks = 0;
616 		cur_dtype->dtype_asect = 0;
617 		cur_dtype->dtype_cache = 0;
618 		cur_dtype->dtype_threshold = 0;
619 		cur_dtype->dtype_prefetch_min = 0;
620 		cur_dtype->dtype_prefetch_max = 0;
621 
622 		if (cur_ctype->ctype_flags & CF_SMD_DEFS) {
623 			cur_dtype->dtype_bps = AVG_BPS;
624 		}
625 	} else {
626 
627 		cur_dtype->dtype_pcyl = get_pcyl(ncyl, cur_dtype->dtype_acyl);
628 		cur_dtype->dtype_bpt = get_bpt(cur_dtype->dtype_nsect,
629 		    &cur_dtype->dtype_options);
630 		cur_dtype->dtype_rpm = get_rpm();
631 		cur_dtype->dtype_fmt_time =
632 		    get_fmt_time(&cur_dtype->dtype_options);
633 		cur_dtype->dtype_cyl_skew =
634 		    get_cyl_skew(&cur_dtype->dtype_options);
635 		cur_dtype->dtype_trk_skew =
636 		    get_trk_skew(&cur_dtype->dtype_options);
637 		cur_dtype->dtype_trks_zone =
638 		    get_trks_zone(&cur_dtype->dtype_options);
639 		cur_dtype->dtype_atrks = get_atrks(&cur_dtype->dtype_options);
640 		cur_dtype->dtype_asect = get_asect(&cur_dtype->dtype_options);
641 		cur_dtype->dtype_cache = get_cache(&cur_dtype->dtype_options);
642 		cur_dtype->dtype_threshold =
643 		    get_threshold(&cur_dtype->dtype_options);
644 		cur_dtype->dtype_prefetch_min =
645 		    get_min_prefetch(&cur_dtype->dtype_options);
646 		cur_dtype->dtype_prefetch_max =
647 		    get_max_prefetch(cur_dtype->dtype_prefetch_min,
648 		    &cur_dtype->dtype_options);
649 		cur_dtype->dtype_phead =
650 		    get_phead(nhead, &cur_dtype->dtype_options);
651 		cur_dtype->dtype_psect = get_psect(&cur_dtype->dtype_options);
652 		cur_dtype->dtype_bps = get_bps();
653 #ifdef sparc
654 		cur_dtype->dtype_dr_type = 0;
655 #endif
656 	}
657 }
658