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