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