xref: /illumos-gate/usr/src/cmd/format/startup.c (revision 582547a924999b3501d957157ec7c98f039597f2)
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 2015 Nexenta Systems, Inc.  All rights reserved.
23  *
24  * Copyright (c) 2011 Gary Mills
25  *
26  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 
29 /*
30  * This file contains the code to perform program startup.  This
31  * includes reading the data file and the search for disks.
32  */
33 #include "global.h"
34 
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <memory.h>
43 #include <dirent.h>
44 #include <sys/fcntl.h>
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #include "startup.h"
49 #include "param.h"
50 #include "label.h"
51 #include "misc.h"
52 #include "menu_command.h"
53 #include "partition.h"
54 #include "ctlr_scsi.h"
55 
56 #include "auto_sense.h"
57 
58 extern	struct	ctlr_type ctlr_types[];
59 extern	int	nctypes;
60 extern	struct	ctlr_ops	genericops;
61 extern	long	strtol();
62 
63 extern	int	errno;
64 
65 char	*file_name;
66 char	*option_d;
67 char	*option_f;
68 char	*option_l;
69 char	*option_p;
70 char	option_s;
71 char	*option_t;
72 char	*option_x;
73 char	diag_msg;
74 char	option_msg;
75 int	need_newline;
76 int	dev_expert;
77 int	expert_mode;
78 uint_t	cur_blksz;
79 struct ctlr_info	*ctlr_list;
80 struct disk_info	*disk_list;
81 struct mctlr_list	*controlp;
82 char	x86_devname[MAXNAMELEN];
83 FILE	*data_file;
84 
85 #ifdef __STDC__
86 
87 /* Function prototypes for ANSI C Compilers */
88 static void	usage(void);
89 static int	sup_prxfile(void);
90 static void	sup_setpath(void);
91 static void	sup_setdtype(void);
92 static int	sup_change_spec(struct disk_type *, char *);
93 static void	sup_setpart(void);
94 static void	search_for_logical_dev(char *devname);
95 static void	add_device_to_disklist(char *devname, char *devpath);
96 static int	disk_is_known(struct dk_cinfo *dkinfo);
97 static void	datafile_error(char *errmsg, char *token);
98 static void	search_duplicate_dtypes(void);
99 static void	search_duplicate_pinfo(void);
100 static void	check_dtypes_for_inconsistency(struct disk_type *dp1,
101 		struct disk_type *dp2);
102 static void	check_pinfo_for_inconsistency(struct partition_info *pp1,
103 		struct partition_info *pp2);
104 static uint_t	str2blks(char *str);
105 static int	str2cyls(char *str);
106 static struct	chg_list *new_chg_list(struct disk_type *);
107 static char	*get_physical_name(char *);
108 static void	sort_disk_list(void);
109 static int	disk_name_compare(const void *, const void *);
110 static void	make_controller_list(void);
111 static void	check_for_duplicate_disknames(char *arglist[]);
112 
113 #else	/* __STDC__ */
114 
115 /* Function prototypes for non-ANSI C Compilers */
116 static void	usage();
117 static int	sup_prxfile();
118 static void	sup_setpath();
119 static void	sup_setdtype();
120 static int	sup_change_spec();
121 static void	sup_setpart();
122 static void	search_for_logical_dev();
123 static void	add_device_to_disklist();
124 static int	disk_is_known();
125 static void	datafile_error();
126 static void	search_duplicate_dtypes();
127 static void	search_duplicate_pinfo();
128 static void	check_dtypes_for_inconsistency();
129 static void	check_pinfo_for_inconsistency();
130 static uint_t	str2blks();
131 static int	str2cyls();
132 static struct	chg_list *new_chg_list();
133 static char	*get_physical_name();
134 static void	sort_disk_list();
135 static int	disk_name_compare();
136 static void	make_controller_list();
137 static void	check_for_duplicate_disknames();
138 
139 #endif	/* __STDC__ */
140 
141 #if defined(sparc)
142 static char *other_ctlrs[] = {
143 	"ata"
144 	};
145 #define	OTHER_CTLRS 1
146 
147 #elif defined(i386)
148 static char *other_ctlrs[] = {
149 	"ISP-80"
150 	};
151 #define	OTHER_CTLRS 2
152 
153 #else
154 #error No Platform defined.
155 #endif
156 
157 
158 /*
159  * This global is used to store the current line # in the data file.
160  * It must be global because the I/O routines are allowed to side
161  * effect it to keep track of backslashed newlines.
162  */
163 int	data_lineno;			/* current line # in data file */
164 
165 /*
166  * Search path as defined in the format.dat files
167  */
168 static char	**search_path = NULL;
169 
170 
171 static int name_represents_wholedisk(char *name);
172 
173 static void get_disk_name(int fd, char *disk_name, struct disk_info *disk_info);
174 
175 /*
176  * This routine digests the options on the command line.  It returns
177  * the index into argv of the first string that is not an option.  If
178  * there are none, it returns -1.
179  */
180 int
do_options(int argc,char * argv[])181 do_options(int argc, char *argv[])
182 {
183 	char	*ptr;
184 	int	i;
185 	int	next;
186 
187 	/*
188 	 * Default is no extended messages.  Can be enabled manually.
189 	 */
190 	option_msg = 0;
191 	diag_msg = 0;
192 	expert_mode = 0;
193 	need_newline = 0;
194 	dev_expert = 0;
195 
196 	/*
197 	 * Loop through the argument list, incrementing each time by
198 	 * an amount determined by the options found.
199 	 */
200 	for (i = 1; i < argc; i = next) {
201 		/*
202 		 * Start out assuming an increment of 1.
203 		 */
204 		next = i + 1;
205 		/*
206 		 * As soon as we hit a non-option, we're done.
207 		 */
208 		if (*argv[i] != '-')
209 			return (i);
210 		/*
211 		 * Loop through all the characters in this option string.
212 		 */
213 		for (ptr = argv[i] + 1; *ptr != '\0'; ptr++) {
214 			/*
215 			 * Determine each option represented.  For options
216 			 * that use a second string, increase the increment
217 			 * of the main loop so they aren't re-interpreted.
218 			 */
219 			switch (*ptr) {
220 			case 's':
221 			case 'S':
222 				option_s = 1;
223 				break;
224 			case 'f':
225 			case 'F':
226 				option_f = argv[next++];
227 				if (next > argc)
228 					goto badopt;
229 				break;
230 			case 'l':
231 			case 'L':
232 				option_l = argv[next++];
233 				if (next > argc)
234 					goto badopt;
235 				break;
236 			case 'x':
237 			case 'X':
238 				option_x = argv[next++];
239 				if (next > argc)
240 					goto badopt;
241 				break;
242 			case 'd':
243 			case 'D':
244 				option_d = argv[next++];
245 				if (next > argc)
246 					goto badopt;
247 				break;
248 			case 't':
249 			case 'T':
250 				option_t = argv[next++];
251 				if (next > argc)
252 					goto badopt;
253 				break;
254 			case 'p':
255 			case 'P':
256 				option_p = argv[next++];
257 				if (next > argc)
258 					goto badopt;
259 				break;
260 			case 'm':
261 				option_msg = 1;
262 				break;
263 			case 'M':
264 				option_msg = 1;
265 				diag_msg = 1;
266 				break;
267 			case 'e':
268 				expert_mode = 1;
269 				break;
270 #ifdef DEBUG
271 			case 'z':
272 				dev_expert = 1;
273 				break;
274 #endif
275 			default:
276 badopt:
277 				usage();
278 				break;
279 			}
280 		}
281 	}
282 	/*
283 	 * All the command line strings were options.  Return that fact.
284 	 */
285 	return (-1);
286 }
287 
288 
289 static void
usage(void)290 usage(void)
291 {
292 	err_print("Usage:  format [-s][-d disk_name]");
293 	err_print("[-t disk_type][-p partition_name]\n");
294 	err_print("\t[-f cmd_file][-l log_file]");
295 	err_print("[-x data_file] [-m] [-M] [-e] disk_list\n");
296 	fullabort();
297 }
298 
299 
300 /*
301  * This routine reads in and digests the data file.  The data file contains
302  * definitions for the search path, known disk types, and known partition
303  * maps.
304  *
305  * Note: for each file being processed, file_name is a pointer to that
306  * file's name.  We are careful to make sure that file_name points to
307  * globally-accessible data, not data on the stack, because each
308  * disk/partition/controller definition now keeps a pointer to the
309  * filename in which it was defined.  In the case of duplicate,
310  * conflicting definitions, we can thus tell the user exactly where
311  * the problem is occurring.
312  */
313 void
sup_init(void)314 sup_init(void)
315 {
316 	int		nopened_files = 0;
317 	char		fname[MAXPATHLEN];
318 	char		*path;
319 	char		*p;
320 	struct stat	stbuf;
321 
322 
323 	/*
324 	 * Create a singly-linked list of controller types so that we may
325 	 * dynamically add unknown controllers to this for 3'rd
326 	 * party disk support.
327 	 */
328 
329 	make_controller_list();
330 
331 	/*
332 	 * If a data file was specified on the command line, use it first
333 	 * If the file cannot be opened, fail.  We want to guarantee
334 	 * that, if the user explicitly names a file, they can
335 	 * access it.
336 	 *
337 	 * option_x is already global, no need to dup it on the heap.
338 	 */
339 	if (option_x) {
340 		file_name = option_x;
341 		if (sup_prxfile()) {
342 			nopened_files++;
343 		} else {
344 			err_print("Unable to open data file '%s' - %s.\n",
345 			    file_name, strerror(errno));
346 			fullabort();
347 		}
348 	}
349 
350 	/*
351 	 * Now look for an environment variable FORMAT_PATH.
352 	 * If found, we use it as a colon-separated list
353 	 * of directories.  If no such environment variable
354 	 * is defined, use a default path of "/etc".
355 	 */
356 	path = getenv("FORMAT_PATH");
357 	if (path == NULL) {
358 		path = "/etc";
359 	}
360 	/*
361 	 * Traverse the path one file at a time.  Pick off
362 	 * the file name, and append the name "format.dat"
363 	 * at the end of the pathname.
364 	 * Whatever string we construct, duplicate it on the
365 	 * heap, so that file_name is globally accessible.
366 	 */
367 	while (*path != 0) {
368 		p = fname;
369 		while (*path != 0 && *path != ':')
370 			*p++ = *path++;
371 		if (p == fname)
372 			continue;
373 		*p = 0;
374 		if (*path == ':')
375 			path++;
376 		/*
377 		 * If the path we have so far is a directory,
378 		 * look for a format.dat file in that directory,
379 		 * otherwise try using the path name specified.
380 		 * This permits arbitrary file names in the
381 		 * path specification, if this proves useful.
382 		 */
383 		if (stat(fname, &stbuf) == -1) {
384 			err_print("Unable to access '%s' - %s.\n",
385 			    fname, strerror(errno));
386 		} else {
387 			if (S_ISDIR(stbuf.st_mode)) {
388 				if (*(p-1) != '/')
389 					*p++ = '/';
390 				(void) strcpy(p, "format.dat");
391 			}
392 			file_name = alloc_string(fname);
393 			if (sup_prxfile()) {
394 				nopened_files++;
395 			}
396 		}
397 	}
398 
399 	/*
400 	 * Check for duplicate disk or partitions definitions
401 	 * that are inconsistent - this would be very confusing.
402 	 */
403 	search_duplicate_dtypes();
404 	search_duplicate_pinfo();
405 }
406 
407 
408 /*
409  * Open and process a format data file.  Unfortunately, we use
410  * globals: file_name for the file name, and data_file
411  * for the descriptor.  Return true if able to open the file.
412  */
413 static int
sup_prxfile(void)414 sup_prxfile(void)
415 {
416 	int	status;
417 	TOKEN	token;
418 	TOKEN	cleaned;
419 
420 	/*
421 	 * Open the data file.  Return 0 if unable to do so.
422 	 */
423 	data_file = fopen(file_name, "r");
424 	if (data_file == NULL) {
425 		return (0);
426 	}
427 	/*
428 	 * Step through the data file a meta-line at a time.  There are
429 	 * typically several backslashed newlines in each meta-line,
430 	 * so data_lineno will be getting side effected along the way.
431 	 */
432 	data_lineno = 0;
433 	for (;;) {
434 		data_lineno++;
435 		/*
436 		 * Get the keyword.
437 		 */
438 		status = sup_gettoken(token);
439 		/*
440 		 * If we hit the end of the data file, we're done.
441 		 */
442 		if (status == SUP_EOF)
443 			break;
444 		/*
445 		 * If the line is blank, skip it.
446 		 */
447 		if (status == SUP_EOL)
448 			continue;
449 		/*
450 		 * If the line starts with some key character, it's an error.
451 		 */
452 		if (status != SUP_STRING) {
453 			datafile_error("Expecting keyword, found '%s'", token);
454 			continue;
455 		}
456 		/*
457 		 * Clean up the token and see which keyword it is.  Call
458 		 * the appropriate routine to process the rest of the line.
459 		 */
460 		clean_token(cleaned, token);
461 		if (strcmp(cleaned, "search_path") == 0)
462 			sup_setpath();
463 		else if (strcmp(cleaned, "disk_type") == 0)
464 			sup_setdtype();
465 		else if (strcmp(cleaned, "partition") == 0)
466 			sup_setpart();
467 		else {
468 			datafile_error("Unknown keyword '%s'", cleaned);
469 		}
470 	}
471 	/*
472 	 * Close the data file.
473 	 */
474 	(void) fclose(data_file);
475 
476 	return (1);
477 }
478 
479 /*
480  * This routine processes a 'search_path' line in the data file.  The
481  * search path is a list of disk names that will be searched for by the
482  * program.
483  *
484  * The static path_size and path_alloc are used to build up the
485  * list of files comprising the search path.  The static definitions
486  * enable supporting multiple search path definitions.
487  */
488 static void
sup_setpath(void)489 sup_setpath(void)
490 {
491 	TOKEN		token;
492 	TOKEN		cleaned;
493 	int		status;
494 	static int	path_size;
495 	static int	path_alloc;
496 
497 	/*
498 	 * Pull in some grammar.
499 	 */
500 	status = sup_gettoken(token);
501 	if (status != SUP_EQL) {
502 		datafile_error("Expecting '=', found '%s'", token);
503 		return;
504 	}
505 	/*
506 	 * Loop through the entries.
507 	 */
508 	for (;;) {
509 		/*
510 		 * Pull in the disk name.
511 		 */
512 		status = sup_gettoken(token);
513 		/*
514 		 * If we hit end of line, we're done.
515 		 */
516 		if (status == SUP_EOL)
517 			break;
518 		/*
519 		 * If we hit some key character, it's an error.
520 		 */
521 		if (status != SUP_STRING) {
522 			datafile_error("Expecting value, found '%s'", token);
523 			break;
524 		}
525 		clean_token(cleaned, token);
526 		/*
527 		 * Build the string into an argvlist.  This array
528 		 * is dynamically sized, as necessary, and terminated
529 		 * with a null.  Each name is alloc'ed on the heap,
530 		 * so no dangling references.
531 		 */
532 		search_path = build_argvlist(search_path, &path_size,
533 		    &path_alloc, cleaned);
534 		/*
535 		 * Pull in some grammar.
536 		 */
537 		status = sup_gettoken(token);
538 		if (status == SUP_EOL)
539 			break;
540 		if (status != SUP_COMMA) {
541 			datafile_error("Expecting ', ', found '%s'", token);
542 			break;
543 		}
544 	}
545 }
546 
547 /*
548  * This routine processes a 'disk_type' line in the data file.  It defines
549  * the physical attributes of a brand of disk when connected to a specific
550  * controller type.
551  */
552 static void
sup_setdtype(void)553 sup_setdtype(void)
554 {
555 	TOKEN	token, cleaned, ident;
556 	int	val, status, i;
557 	ulong_t	flags = 0;
558 	struct	disk_type *dtype, *type;
559 	struct	ctlr_type *ctype = NULL;
560 	char	*dtype_name, *ptr;
561 	struct	mctlr_list	*mlp;
562 
563 	/*
564 	 * Pull in some grammar.
565 	 */
566 	status = sup_gettoken(token);
567 	if (status != SUP_EQL) {
568 		datafile_error("Expecting '=', found '%s'", token);
569 		return;
570 	}
571 	/*
572 	 * Pull in the name of the disk type.
573 	 */
574 	status = sup_gettoken(token);
575 	if (status != SUP_STRING) {
576 		datafile_error("Expecting value, found '%s'", token);
577 		return;
578 	}
579 	clean_token(cleaned, token);
580 	/*
581 	 * Allocate space for the disk type and copy in the name.
582 	 */
583 	dtype_name = (char *)zalloc(strlen(cleaned) + 1);
584 	(void) strcpy(dtype_name, cleaned);
585 	dtype = (struct disk_type *)zalloc(sizeof (struct disk_type));
586 	dtype->dtype_asciilabel = dtype_name;
587 	/*
588 	 * Save the filename/linenumber where this disk was defined
589 	 */
590 	dtype->dtype_filename = file_name;
591 	dtype->dtype_lineno = data_lineno;
592 	/*
593 	 * Loop for each attribute.
594 	 */
595 	for (;;) {
596 		/*
597 		 * Pull in some grammar.
598 		 */
599 		status = sup_gettoken(token);
600 		/*
601 		 * If we hit end of line, we're done.
602 		 */
603 		if (status == SUP_EOL)
604 			break;
605 		if (status != SUP_COLON) {
606 			datafile_error("Expecting ':', found '%s'", token);
607 			return;
608 		}
609 		/*
610 		 * Pull in the attribute.
611 		 */
612 		status = sup_gettoken(token);
613 		/*
614 		 * If we hit end of line, we're done.
615 		 */
616 		if (status == SUP_EOL)
617 			break;
618 		/*
619 		 * If we hit a key character, it's an error.
620 		 */
621 		if (status != SUP_STRING) {
622 			datafile_error("Expecting keyword, found '%s'", token);
623 			return;
624 		}
625 		clean_token(ident, token);
626 		/*
627 		 * Check to see if we've got a change specification
628 		 * If so, this routine will parse the entire
629 		 * specification, so just restart at top of loop
630 		 */
631 		if (sup_change_spec(dtype, ident)) {
632 			continue;
633 		}
634 		/*
635 		 * Pull in more grammar.
636 		 */
637 		status = sup_gettoken(token);
638 		if (status != SUP_EQL) {
639 			datafile_error("Expecting '=', found '%s'", token);
640 			return;
641 		}
642 		/*
643 		 * Pull in the value of the attribute.
644 		 */
645 		status = sup_gettoken(token);
646 		if (status != SUP_STRING) {
647 			datafile_error("Expecting value, found '%s'", token);
648 			return;
649 		}
650 		clean_token(cleaned, token);
651 		/*
652 		 * If the attribute defined the ctlr...
653 		 */
654 		if (strcmp(ident, "ctlr") == 0) {
655 			/*
656 			 * Match the value with a ctlr type.
657 			 */
658 			mlp = controlp;
659 
660 			while (mlp != NULL) {
661 				if (strcmp(mlp->ctlr_type->ctype_name,
662 				    cleaned) == 0)
663 					break;
664 				mlp = mlp->next;
665 			}
666 			/*
667 			 * If we couldn't match it, it's an error.
668 			 */
669 			if (mlp == NULL) {
670 				for (i = 0; i < OTHER_CTLRS; i++) {
671 					if (strcmp(other_ctlrs[i], cleaned)
672 					    == 0) {
673 						datafile_error(NULL, NULL);
674 						return;
675 					}
676 				}
677 				if (i == OTHER_CTLRS) {
678 					datafile_error(
679 					    "Unknown controller '%s'",
680 					    cleaned);
681 					return;
682 				}
683 			}
684 			/*
685 			 * Found a match.  Add this disk type to the list
686 			 * for the ctlr type if we can complete the
687 			 * disk specification correctly.
688 			 */
689 			ctype = mlp->ctlr_type;
690 			flags |= SUP_CTLR;
691 			continue;
692 		}
693 		/*
694 		 * All other attributes require a numeric value.  Convert
695 		 * the value to a number.
696 		 */
697 		val = (int)strtol(cleaned, &ptr, 0);
698 		if (*ptr != '\0') {
699 			datafile_error("Expecting an integer, found '%s'",
700 			    cleaned);
701 			return;
702 		}
703 		/*
704 		 * Figure out which attribute it was and fill in the
705 		 * appropriate value.  Also note that the attribute
706 		 * has been defined.
707 		 */
708 		if (strcmp(ident, "ncyl") == 0) {
709 			dtype->dtype_ncyl = val;
710 			flags |= SUP_NCYL;
711 		} else if (strcmp(ident, "acyl") == 0) {
712 			dtype->dtype_acyl = val;
713 			flags |= SUP_ACYL;
714 		} else if (strcmp(ident, "pcyl") == 0) {
715 			dtype->dtype_pcyl = val;
716 			flags |= SUP_PCYL;
717 		} else if (strcmp(ident, "nhead") == 0) {
718 			dtype->dtype_nhead = val;
719 			flags |= SUP_NHEAD;
720 		} else if (strcmp(ident, "nsect") == 0) {
721 			dtype->dtype_nsect = val;
722 			flags |= SUP_NSECT;
723 		} else if (strcmp(ident, "rpm") == 0) {
724 			dtype->dtype_rpm = val;
725 			flags |= SUP_RPM;
726 		} else if (strcmp(ident, "bpt") == 0) {
727 			dtype->dtype_bpt = val;
728 			flags |= SUP_BPT;
729 		} else if (strcmp(ident, "bps") == 0) {
730 			dtype->dtype_bps = val;
731 			flags |= SUP_BPS;
732 		} else if (strcmp(ident, "drive_type") == 0) {
733 			dtype->dtype_dr_type = val;
734 			flags |= SUP_DRTYPE;
735 		} else if (strcmp(ident, "cache") == 0) {
736 			dtype->dtype_cache = val;
737 			flags |= SUP_CACHE;
738 		} else if (strcmp(ident, "prefetch") == 0) {
739 			dtype->dtype_threshold = val;
740 			flags |= SUP_PREFETCH;
741 		} else if (strcmp(ident, "read_retries") == 0) {
742 			dtype->dtype_read_retries = val;
743 			flags |= SUP_READ_RETRIES;
744 		} else if (strcmp(ident, "write_retries") == 0) {
745 			dtype->dtype_write_retries = val;
746 			flags |= SUP_WRITE_RETRIES;
747 		} else if (strcmp(ident, "min_prefetch") == 0) {
748 			dtype->dtype_prefetch_min = val;
749 			flags |= SUP_CACHE_MIN;
750 		} else if (strcmp(ident, "max_prefetch") == 0) {
751 			dtype->dtype_prefetch_max = val;
752 			flags |= SUP_CACHE_MAX;
753 		} else if (strcmp(ident, "trks_zone") == 0) {
754 			dtype->dtype_trks_zone = val;
755 			flags |= SUP_TRKS_ZONE;
756 		} else if (strcmp(ident, "atrks") == 0) {
757 			dtype->dtype_atrks = val;
758 			flags |= SUP_ATRKS;
759 		} else if (strcmp(ident, "asect") == 0) {
760 			dtype->dtype_asect = val;
761 			flags |= SUP_ASECT;
762 		} else if (strcmp(ident, "psect") == 0) {
763 			dtype->dtype_psect = val;
764 			flags |= SUP_PSECT;
765 		} else if (strcmp(ident, "phead") == 0) {
766 			dtype->dtype_phead = val;
767 			flags |= SUP_PHEAD;
768 		} else if (strcmp(ident, "fmt_time") == 0) {
769 			dtype->dtype_fmt_time = val;
770 			flags |= SUP_FMTTIME;
771 		} else if (strcmp(ident, "cyl_skew") == 0) {
772 			dtype->dtype_cyl_skew = val;
773 			flags |= SUP_CYLSKEW;
774 		} else if (strcmp(ident, "trk_skew") == 0) {
775 			dtype->dtype_trk_skew = val;
776 			flags |= SUP_TRKSKEW;
777 		} else {
778 			datafile_error("Unknown keyword '%s'", ident);
779 		}
780 	}
781 
782 	/*
783 	 * Check to be sure all the necessary attributes have been defined.
784 	 * If any are missing, it's an error.  Also, log options for later
785 	 * use by specific driver.
786 	 */
787 	dtype->dtype_options = flags;
788 	if ((flags & SUP_MIN_DRIVE) != SUP_MIN_DRIVE) {
789 		datafile_error("Incomplete specification", "");
790 		return;
791 	}
792 	if ((!(ctype->ctype_flags & CF_SCSI)) && (!(flags & SUP_BPT)) &&
793 	    (!(ctype->ctype_flags & CF_NOFORMAT))) {
794 		datafile_error("Incomplete specification", "");
795 		return;
796 	}
797 	if ((ctype->ctype_flags & CF_SMD_DEFS) && (!(flags & SUP_BPS))) {
798 		datafile_error("Incomplete specification", "");
799 		return;
800 	}
801 	/*
802 	 * Add this disk type to the list for the ctlr type
803 	 */
804 	assert(flags & SUP_CTLR);
805 	type = ctype->ctype_dlist;
806 	if (type == NULL) {
807 		ctype->ctype_dlist = dtype;
808 	} else {
809 		while (type->dtype_next != NULL)
810 			type = type->dtype_next;
811 		type->dtype_next = dtype;
812 	}
813 }
814 
815 
816 /*
817  * Parse a SCSI mode page change specification.
818  *
819  * Return:
820  *		0:  not change specification, continue parsing
821  *		1:  was change specification, it was ok,
822  *		    or we already handled the error.
823  */
824 static int
sup_change_spec(struct disk_type * disk,char * id)825 sup_change_spec(struct disk_type *disk, char *id)
826 {
827 	char		*p;
828 	char		*p2;
829 	int		pageno;
830 	int		byteno;
831 	int		mode;
832 	int		value;
833 	TOKEN		token;
834 	TOKEN		ident;
835 	struct chg_list	*cp;
836 	int		tilde;
837 	int		i;
838 
839 	/*
840 	 * Syntax: p[<nn>|0x<xx>]
841 	 */
842 	if (*id != 'p') {
843 		return (0);
844 	}
845 	pageno = (int)strtol(id+1, &p2, 0);
846 	if (*p2 != 0) {
847 		return (0);
848 	}
849 	/*
850 	 * Once we get this far, we know we have the
851 	 * beginnings of a change specification.
852 	 * If there's a problem now, report the problem,
853 	 * and return 1, so that the caller can restart
854 	 * parsing at the next expression.
855 	 */
856 	if (!scsi_supported_page(pageno)) {
857 		datafile_error("Unsupported mode page '%s'", id);
858 		return (1);
859 	}
860 	/*
861 	 * Next token should be the byte offset
862 	 */
863 	if (sup_gettoken(token) != SUP_STRING) {
864 		datafile_error("Unexpected value '%s'", token);
865 		return (1);
866 	}
867 	clean_token(ident, token);
868 
869 	/*
870 	 * Syntax: b[<nn>|0x<xx>]
871 	 */
872 	p = ident;
873 	if (*p++ != 'b') {
874 		datafile_error("Unknown keyword '%s'", ident);
875 		return (1);
876 	}
877 	byteno = (int)strtol(p, &p2, 10);
878 	if (*p2 != 0) {
879 		datafile_error("Unknown keyword '%s'", ident);
880 		return (1);
881 	}
882 	if (byteno == 0 || byteno == 1) {
883 		datafile_error("Unsupported byte offset '%s'", ident);
884 		return (1);
885 	}
886 
887 	/*
888 	 * Get the operator for this expression
889 	 */
890 	mode = CHG_MODE_UNDEFINED;
891 	switch (sup_gettoken(token)) {
892 	case SUP_EQL:
893 		mode = CHG_MODE_ABS;
894 		break;
895 	case SUP_OR:
896 		if (sup_gettoken(token) == SUP_EQL)
897 			mode = CHG_MODE_SET;
898 		break;
899 	case SUP_AND:
900 		if (sup_gettoken(token) == SUP_EQL)
901 			mode = CHG_MODE_CLR;
902 		break;
903 	}
904 	if (mode == CHG_MODE_UNDEFINED) {
905 		datafile_error("Unexpected operator: '%s'", token);
906 		return (1);
907 	}
908 
909 	/*
910 	 * Get right-hand of expression - accept optional tilde
911 	 */
912 	tilde = 0;
913 	if ((i = sup_gettoken(token)) == SUP_TILDE) {
914 		tilde = 1;
915 		i = sup_gettoken(token);
916 	}
917 	if (i != SUP_STRING) {
918 		datafile_error("Expecting value, found '%s'", token);
919 		return (1);
920 	}
921 	clean_token(ident, token);
922 	value = (int)strtol(ident, &p, 0);
923 	if (*p != 0) {
924 		datafile_error("Expecting value, found '%s'", token);
925 		return (1);
926 	}
927 
928 	/*
929 	 * Apply the tilde operator, if found.
930 	 * Constrain to a byte value.
931 	 */
932 	if (tilde) {
933 		value = ~value;
934 	}
935 	value &= 0xff;
936 
937 	/*
938 	 * We parsed a successful change specification expression.
939 	 * Add it to the list for this disk type.
940 	 */
941 	cp = new_chg_list(disk);
942 	cp->pageno = pageno;
943 	cp->byteno = byteno;
944 	cp->mode = mode;
945 	cp->value = value;
946 	return (1);
947 }
948 
949 
950 /*
951  * This routine processes a 'partition' line in the data file.  It defines
952  * a known partition map for a particular disk type on a particular
953  * controller type.
954  */
955 static void
sup_setpart(void)956 sup_setpart(void)
957 {
958 	TOKEN	token, cleaned, disk, ctlr, ident;
959 	struct	disk_type *dtype = NULL;
960 	struct	ctlr_type *ctype = NULL;
961 	struct	partition_info *pinfo, *parts;
962 	char	*pinfo_name;
963 	int	i, index, status, flags = 0;
964 	uint_t	val1, val2;
965 	ushort_t	vtoc_tag;
966 	ushort_t	vtoc_flag;
967 	struct	mctlr_list	*mlp;
968 
969 	/*
970 	 * Pull in some grammar.
971 	 */
972 	status = sup_gettoken(token);
973 	if (status != SUP_EQL) {
974 		datafile_error("Expecting '=', found '%s'", token);
975 		return;
976 	}
977 	/*
978 	 * Pull in the name of the map.
979 	 */
980 	status = sup_gettoken(token);
981 	if (status != SUP_STRING) {
982 		datafile_error("Expecting value, found '%s'", token);
983 		return;
984 	}
985 	clean_token(cleaned, token);
986 	/*
987 	 * Allocate space for the partition map and fill in the name.
988 	 */
989 	pinfo_name = (char *)zalloc(strlen(cleaned) + 1);
990 	(void) strcpy(pinfo_name, cleaned);
991 	pinfo = (struct partition_info *)zalloc(sizeof (struct partition_info));
992 	pinfo->pinfo_name = pinfo_name;
993 	/*
994 	 * Save the filename/linenumber where this partition was defined
995 	 */
996 	pinfo->pinfo_filename = file_name;
997 	pinfo->pinfo_lineno = data_lineno;
998 
999 	/*
1000 	 * Install default vtoc information into the new partition table
1001 	 */
1002 	set_vtoc_defaults(pinfo);
1003 
1004 	/*
1005 	 * Loop for each attribute in the line.
1006 	 */
1007 	for (;;) {
1008 		/*
1009 		 * Pull in some grammar.
1010 		 */
1011 		status = sup_gettoken(token);
1012 		/*
1013 		 * If we hit end of line, we're done.
1014 		 */
1015 		if (status == SUP_EOL)
1016 			break;
1017 		if (status != SUP_COLON) {
1018 			datafile_error("Expecting ':', found '%s'", token);
1019 			return;
1020 		}
1021 		/*
1022 		 * Pull in the attribute.
1023 		 */
1024 		status = sup_gettoken(token);
1025 		/*
1026 		 * If we hit end of line, we're done.
1027 		 */
1028 		if (status == SUP_EOL)
1029 			break;
1030 		if (status != SUP_STRING) {
1031 			datafile_error("Expecting keyword, found '%s'", token);
1032 			return;
1033 		}
1034 		clean_token(ident, token);
1035 		/*
1036 		 * Pull in more grammar.
1037 		 */
1038 		status = sup_gettoken(token);
1039 		if (status != SUP_EQL) {
1040 			datafile_error("Expecting '=', found '%s'", token);
1041 			return;
1042 		}
1043 		/*
1044 		 * Pull in the value of the attribute.
1045 		 */
1046 		status = sup_gettoken(token);
1047 		/*
1048 		 * If we hit a key character, it's an error.
1049 		 */
1050 		if (status != SUP_STRING) {
1051 			datafile_error("Expecting value, found '%s'", token);
1052 			return;
1053 		}
1054 		clean_token(cleaned, token);
1055 		/*
1056 		 * If the attribute is the ctlr, save the ctlr name and
1057 		 * mark it defined.
1058 		 */
1059 		if (strcmp(ident, "ctlr") == 0) {
1060 			(void) strcpy(ctlr, cleaned);
1061 			flags |= SUP_CTLR;
1062 			continue;
1063 		/*
1064 		 * If the attribute is the disk, save the disk name and
1065 		 * mark it defined.
1066 		 */
1067 		} else if (strcmp(ident, "disk") == 0) {
1068 			(void) strcpy(disk, cleaned);
1069 			flags |= SUP_DISK;
1070 			continue;
1071 		}
1072 		/*
1073 		 * If we now know both the controller name and the
1074 		 * disk name, let's see if we can find the controller
1075 		 * and disk type.  This will give us the geometry,
1076 		 * which can permit us to accept partitions specs
1077 		 * in cylinders or blocks.
1078 		 */
1079 		if (((flags & (SUP_DISK|SUP_CTLR)) == (SUP_DISK|SUP_CTLR)) &&
1080 		    dtype == NULL && ctype == NULL) {
1081 			/*
1082 			 * Attempt to match the specified ctlr to a known type.
1083 			 */
1084 			mlp = controlp;
1085 
1086 			while (mlp != NULL) {
1087 				if (strcmp(mlp->ctlr_type->ctype_name,
1088 				    ctlr) == 0)
1089 					break;
1090 				mlp = mlp->next;
1091 			}
1092 			/*
1093 			 * If no match is found, it's an error.
1094 			 */
1095 			if (mlp == NULL) {
1096 				for (i = 0; i < OTHER_CTLRS; i++) {
1097 					if (strcmp(other_ctlrs[i], ctlr) == 0) {
1098 						datafile_error(NULL, NULL);
1099 						return;
1100 					}
1101 				}
1102 				if (i == OTHER_CTLRS) {
1103 					datafile_error(
1104 					    "Unknown controller '%s'", ctlr);
1105 					return;
1106 				}
1107 			}
1108 			ctype = mlp->ctlr_type;
1109 			/*
1110 			 * Attempt to match the specified disk to a known type.
1111 			 */
1112 			for (dtype = ctype->ctype_dlist; dtype != NULL;
1113 			    dtype = dtype->dtype_next) {
1114 				if (strcmp(dtype->dtype_asciilabel, disk) == 0)
1115 					break;
1116 			}
1117 			/*
1118 			 * If no match is found, it's an error.
1119 			 */
1120 			if (dtype == NULL) {
1121 				datafile_error("Unknown disk '%s'", disk);
1122 				return;
1123 			}
1124 			/*
1125 			 * Now that we know the disk type, set up the
1126 			 * globals that let that magic macro "spc()"
1127 			 * do it's thing.  Sorry that this is glued
1128 			 * together so poorly...
1129 			 */
1130 			nhead = dtype->dtype_nhead;
1131 			nsect = dtype->dtype_nsect;
1132 			acyl = dtype->dtype_acyl;
1133 			ncyl = dtype->dtype_ncyl;
1134 		}
1135 		/*
1136 		 * By now, the disk and controller type must be defined
1137 		 */
1138 		if (dtype == NULL || ctype == NULL) {
1139 			datafile_error("Incomplete specification", "");
1140 			return;
1141 		}
1142 		/*
1143 		 * The rest of the attributes are all single letters.
1144 		 * Make sure the specified attribute is a single letter.
1145 		 */
1146 		if (strlen(ident) != 1) {
1147 			datafile_error("Unknown keyword '%s'", ident);
1148 			return;
1149 		}
1150 		/*
1151 		 * Also make sure it is within the legal range of letters.
1152 		 */
1153 		if (ident[0] < PARTITION_BASE || ident[0] > PARTITION_BASE+9) {
1154 			datafile_error("Unknown keyword '%s'", ident);
1155 			return;
1156 		}
1157 		/*
1158 		 * Here's the index of the partition we're dealing with
1159 		 */
1160 		index = ident[0] - PARTITION_BASE;
1161 		/*
1162 		 * For SunOS 5.0, we support the additional syntax:
1163 		 *	[<tag>, ] [<flag>, ] <start>, <end>
1164 		 * instead of:
1165 		 *	<start>, <end>
1166 		 *
1167 		 * <tag> may be one of: boot, root, swap, etc.
1168 		 * <flag> consists of two characters:
1169 		 *	W (writable) or R (read-only)
1170 		 *	M (mountable) or U (unmountable)
1171 		 *
1172 		 * Start with the defaults assigned above:
1173 		 */
1174 		vtoc_tag = pinfo->vtoc.v_part[index].p_tag;
1175 		vtoc_flag = pinfo->vtoc.v_part[index].p_flag;
1176 
1177 		/*
1178 		 * First try to match token against possible tag values
1179 		 */
1180 		if (find_value(ptag_choices, cleaned, &i) == 1) {
1181 			/*
1182 			 * Found valid tag. Use it and advance parser
1183 			 */
1184 			vtoc_tag = (ushort_t)i;
1185 			status = sup_gettoken(token);
1186 			if (status != SUP_COMMA) {
1187 				datafile_error(
1188 				    "Expecting ', ', found '%s'", token);
1189 				return;
1190 			}
1191 			status = sup_gettoken(token);
1192 			if (status != SUP_STRING) {
1193 				datafile_error("Expecting value, found '%s'",
1194 				    token);
1195 				return;
1196 			}
1197 			clean_token(cleaned, token);
1198 		}
1199 
1200 		/*
1201 		 * Try to match token against possible flag values
1202 		 */
1203 		if (find_value(pflag_choices, cleaned, &i) == 1) {
1204 			/*
1205 			 * Found valid flag. Use it and advance parser
1206 			 */
1207 			vtoc_flag = (ushort_t)i;
1208 			status = sup_gettoken(token);
1209 			if (status != SUP_COMMA) {
1210 				datafile_error("Expecting ', ', found '%s'",
1211 				    token);
1212 				return;
1213 			}
1214 			status = sup_gettoken(token);
1215 			if (status != SUP_STRING) {
1216 				datafile_error("Expecting value, found '%s'",
1217 				    token);
1218 				return;
1219 			}
1220 			clean_token(cleaned, token);
1221 		}
1222 		/*
1223 		 * All other attributes have a pair of numeric values.
1224 		 * Convert the first value to a number.  This value
1225 		 * is the starting cylinder number of the partition.
1226 		 */
1227 		val1 = str2cyls(cleaned);
1228 		if (val1 == (uint_t)(-1)) {
1229 			datafile_error("Expecting an integer, found '%s'",
1230 			    cleaned);
1231 			return;
1232 		}
1233 		/*
1234 		 * Pull in some grammar.
1235 		 */
1236 		status = sup_gettoken(token);
1237 		if (status != SUP_COMMA) {
1238 			datafile_error("Expecting ', ', found '%s'", token);
1239 			return;
1240 		}
1241 		/*
1242 		 * Pull in the second value.
1243 		 */
1244 		status = sup_gettoken(token);
1245 		if (status != SUP_STRING) {
1246 			datafile_error("Expecting value, found '%s'", token);
1247 			return;
1248 		}
1249 		clean_token(cleaned, token);
1250 		/*
1251 		 * Convert the second value to a number.  This value
1252 		 * is the number of blocks composing the partition.
1253 		 * If the token is terminated with a 'c', the units
1254 		 * are cylinders, not blocks.  Also accept a 'b', if
1255 		 * they choose to be so specific.
1256 		 */
1257 		val2 = str2blks(cleaned);
1258 		if (val2 == (uint_t)(-1)) {
1259 			datafile_error("Expecting an integer, found '%s'",
1260 			    cleaned);
1261 			return;
1262 		}
1263 		/*
1264 		 * Fill in the appropriate map entry with the values.
1265 		 */
1266 		pinfo->pinfo_map[index].dkl_cylno = val1;
1267 		pinfo->pinfo_map[index].dkl_nblk = val2;
1268 		pinfo->vtoc.v_part[index].p_tag = vtoc_tag;
1269 		pinfo->vtoc.v_part[index].p_flag = vtoc_flag;
1270 
1271 #if defined(_SUNOS_VTOC_16)
1272 		pinfo->vtoc.v_part[index].p_start = val1 * (nhead * nsect);
1273 		pinfo->vtoc.v_part[index].p_size = val2;
1274 
1275 		if (val2 == 0) {
1276 			pinfo->vtoc.v_part[index].p_tag = 0;
1277 			pinfo->vtoc.v_part[index].p_flag = 0;
1278 			pinfo->vtoc.v_part[index].p_start = 0;
1279 			pinfo->pinfo_map[index].dkl_cylno = 0;
1280 		}
1281 #endif /* defined(_SUNOS_VTOC_16) */
1282 
1283 	}
1284 	/*
1285 	 * Check to be sure that all necessary attributes were defined.
1286 	 */
1287 	if ((flags & SUP_MIN_PART) != SUP_MIN_PART) {
1288 		datafile_error("Incomplete specification", "");
1289 		return;
1290 	}
1291 	/*
1292 	 * Add this partition map to the list of known maps for the
1293 	 * specified disk/ctlr.
1294 	 */
1295 	parts = dtype->dtype_plist;
1296 	if (parts == NULL)
1297 		dtype->dtype_plist = pinfo;
1298 	else {
1299 		while (parts->pinfo_next != NULL)
1300 			parts = parts->pinfo_next;
1301 		parts->pinfo_next = pinfo;
1302 	}
1303 }
1304 
1305 /*
1306  * Open the disk device - just a wrapper for open.
1307  */
1308 int
open_disk(char * diskname,int flags)1309 open_disk(char *diskname, int flags)
1310 {
1311 	return (open(diskname, flags));
1312 }
1313 
1314 /*
1315  * This routine performs the disk search during startup.  It looks for
1316  * all the disks in the search path, and creates a list of those that
1317  * are found.
1318  */
1319 void
do_search(char * arglist[])1320 do_search(char *arglist[])
1321 {
1322 	char			**sp;
1323 	DIR			*dir;
1324 	struct dirent		*dp;
1325 	char			s[MAXPATHLEN];
1326 	char			path[MAXPATHLEN];
1327 	char			curdir[MAXPATHLEN];
1328 	char			*directory = "/dev/rdsk";
1329 	struct disk_info	*disk;
1330 	int			i;
1331 
1332 	/*
1333 	 * Change directory to the device directory.  This
1334 	 * gives us the most efficient access to that directory.
1335 	 * Remember where we were, and return there when finished.
1336 	 */
1337 	if (getcwd(curdir, sizeof (curdir)) == NULL) {
1338 		err_print("Cannot get current directory - %s\n",
1339 		    strerror(errno));
1340 		fullabort();
1341 	}
1342 	if (chdir(directory) == -1) {
1343 		err_print("Cannot set directory to %s - %s\n",
1344 		    directory, strerror(errno));
1345 		fullabort();
1346 	}
1347 
1348 	/*
1349 	 * If there were disks specified on the command line,
1350 	 * use those disks, and nothing but those disks.
1351 	 */
1352 	if (arglist != NULL) {
1353 		check_for_duplicate_disknames(arglist);
1354 		for (; *arglist != NULL; arglist++) {
1355 			search_for_logical_dev(*arglist);
1356 		}
1357 	} else {
1358 		/*
1359 		 * If there were no disks specified on the command line,
1360 		 * search for all disks attached to the system.
1361 		 */
1362 		fmt_print("Searching for disks...");
1363 		(void) fflush(stdout);
1364 		need_newline = 1;
1365 
1366 		/*
1367 		 * Find all disks specified in search_path definitions
1368 		 * in whatever format.dat files were processed.
1369 		 */
1370 		sp = search_path;
1371 		if (sp != NULL) {
1372 			while (*sp != NULL) {
1373 				search_for_logical_dev(*sp++);
1374 			}
1375 		}
1376 
1377 		/*
1378 		 * Open the device directory
1379 		 */
1380 		if ((dir = opendir(".")) == NULL) {
1381 			err_print("Cannot open %s - %s\n",
1382 			    directory, strerror(errno));
1383 			fullabort();
1384 		}
1385 
1386 		/*
1387 		 * Now find all usable nodes in /dev/rdsk (or /dev, if 4.x)
1388 		 * First find all nodes which do not conform to
1389 		 * standard disk naming conventions.  This permits
1390 		 * all user-defined names to override the default names.
1391 		 */
1392 		while ((dp = readdir(dir)) != NULL) {
1393 			if (strcmp(dp->d_name, ".") == 0 ||
1394 			    strcmp(dp->d_name, "..") == 0)
1395 				continue;
1396 			if (!conventional_name(dp->d_name)) {
1397 				if (!fdisk_physical_name(dp->d_name)) {
1398 					/*
1399 					 * If non-conventional name represents
1400 					 * a link to non-s2 slice , ignore it.
1401 					 */
1402 					if (!name_represents_wholedisk
1403 					    (dp->d_name)) {
1404 						(void) strcpy(path, directory);
1405 						(void) strcat(path, "/");
1406 						(void) strcat(path, dp->d_name);
1407 						add_device_to_disklist(
1408 						    dp->d_name, path);
1409 					}
1410 				}
1411 			}
1412 		}
1413 		rewinddir(dir);
1414 
1415 
1416 		/*
1417 		 * Now find all nodes corresponding to the standard
1418 		 * device naming conventions.
1419 		 */
1420 		while ((dp = readdir(dir)) != NULL) {
1421 			if (strcmp(dp->d_name, ".") == 0 ||
1422 			    strcmp(dp->d_name, "..") == 0)
1423 				continue;
1424 			if (whole_disk_name(dp->d_name)) {
1425 				(void) strcpy(path, directory);
1426 				(void) strcat(path, "/");
1427 				(void) strcat(path, dp->d_name);
1428 				canonicalize_name(s, dp->d_name);
1429 				add_device_to_disklist(s, path);
1430 			}
1431 		}
1432 		/*
1433 		 * Close the directory
1434 		 */
1435 		if (closedir(dir) == -1) {
1436 			err_print("Cannot close directory %s - %s\n",
1437 			    directory, strerror(errno));
1438 			fullabort();
1439 		}
1440 
1441 		need_newline = 0;
1442 		fmt_print("done\n");
1443 	}
1444 
1445 	/*
1446 	 * Return to whence we came
1447 	 */
1448 	if (chdir(curdir) == -1) {
1449 		err_print("Cannot set directory to %s - %s\n",
1450 		    curdir, strerror(errno));
1451 		fullabort();
1452 	}
1453 
1454 	/*
1455 	 * If we didn't find any disks, give up.
1456 	 */
1457 	if (disk_list == NULL) {
1458 		if (geteuid() == 0) {
1459 			err_print("No disks found!\n");
1460 		} else {
1461 			err_print("No permission (or no disks found)!\n");
1462 		}
1463 		(void) fflush(stdout);
1464 		fullabort();
1465 	}
1466 
1467 	sort_disk_list();
1468 
1469 	/*
1470 	 * Tell user the results of the auto-configure process
1471 	 */
1472 	i = 0;
1473 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
1474 		float			scaled;
1475 		diskaddr_t		nblks;
1476 		struct disk_type	*type;
1477 		if (disk->disk_flags & DSK_AUTO_CONFIG) {
1478 			if (i++ == 0) {
1479 				fmt_print("\n");
1480 			}
1481 			fmt_print("%s: ", disk->disk_name);
1482 			if (disk->disk_flags & DSK_LABEL_DIRTY) {
1483 				fmt_print("configured ");
1484 			} else {
1485 				fmt_print("configured and labeled ");
1486 			}
1487 			type = disk->disk_type;
1488 			nblks = type->dtype_ncyl * type->dtype_nhead *
1489 			    type->dtype_nsect;
1490 			if (disk->label_type == L_TYPE_SOLARIS)
1491 				scaled = bn2mb(nblks);
1492 			else
1493 				scaled = bn2mb(type->capacity);
1494 			fmt_print("with capacity of ");
1495 			if (scaled > 1024.0) {
1496 				fmt_print("%1.2fGB\n", scaled/1024.0);
1497 			} else {
1498 				fmt_print("%1.2fMB\n", scaled);
1499 			}
1500 		}
1501 	}
1502 }
1503 
1504 
1505 /*
1506  * For a given "logical" disk name as specified in a format.dat
1507  * search path, try to find the device it actually refers to.
1508  * Since we are trying to maintain 4.x naming convention
1509  * compatibility in 5.0, this involves a little bit of work.
1510  * We also want to be able to function under 4.x, if needed.
1511  *
1512  * canonical:	standard name reference.  append a partition
1513  *	reference, and open that file in the device directory.
1514  *	examples:	SVR4:	c0t0d0
1515  *			4.x:	sd0
1516  *
1517  * absolute:	begins with a '/', and is assumed to be an
1518  *	absolute pathname to some node.
1519  *
1520  * relative:	non-canonical, doesn't begin with a '/'.
1521  *	assumed to be the name of a file in the appropriate
1522  *	device directory.
1523  */
1524 static void
search_for_logical_dev(char * devname)1525 search_for_logical_dev(char *devname)
1526 {
1527 	char		path[MAXPATHLEN];
1528 	char		*directory = "/dev/rdsk/";
1529 	char		*partition = "s2";
1530 
1531 	/*
1532 	 * If the name is an absolute path name, accept it as is
1533 	 */
1534 	if (*devname == '/') {
1535 		(void) strcpy(path, devname);
1536 	} else if (canonical_name(devname)) {
1537 		/*
1538 		 * If canonical name, construct a standard path name.
1539 		 */
1540 		(void) strcpy(path, directory);
1541 		(void) strcat(path, devname);
1542 		(void) strcat(path, partition);
1543 	} else if (canonical4x_name(devname)) {
1544 		/*
1545 		 * Check to see if it's a 4.x file name in the /dev
1546 		 * directory on 5.0.  Here, we only accept the
1547 		 * canonicalized form: sd0.
1548 		 */
1549 		(void) strcpy(path, "/dev/r");
1550 		(void) strcat(path, devname);
1551 		(void) strcat(path, "c");
1552 	} else {
1553 		/*
1554 		 * If it's not a canonical name, then it may be a
1555 		 * reference to an actual file name in the device
1556 		 * directory itself.
1557 		 */
1558 		(void) strcpy(path, directory);
1559 		(void) strcat(path, devname);
1560 	}
1561 
1562 	/* now add the device */
1563 	add_device_to_disklist(devname, path);
1564 }
1565 
1566 /*
1567  * Get the disk name from the inquiry data
1568  */
1569 static void
get_disk_name(int fd,char * disk_name,struct disk_info * disk_info)1570 get_disk_name(int fd, char *disk_name, struct disk_info *disk_info)
1571 {
1572 	struct scsi_inquiry	inquiry;
1573 	char			*vid, *pid, *rid;
1574 
1575 	if (get_disk_inquiry_prop(disk_info->devfs_name, &vid, &pid, &rid)
1576 	    == 0) {
1577 		(void) snprintf(disk_name, MAXNAMELEN - 1, "%s-%s-%s",
1578 		    vid, pid, rid);
1579 		free(vid);
1580 		free(pid);
1581 		free(rid);
1582 
1583 		return;
1584 	}
1585 
1586 	if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
1587 		if (option_msg)
1588 			err_print("\nInquiry failed - %s\n", strerror(errno));
1589 		(void) strcpy(disk_name, "Unknown-Unknown-0001");
1590 		return;
1591 	}
1592 
1593 	(void) get_generic_disk_name(disk_name, &inquiry);
1594 }
1595 
1596 /*
1597  * Add a device to the disk list, if it appears to be a disk,
1598  * and we haven't already found it under some other name.
1599  */
1600 static void
add_device_to_disklist(char * devname,char * devpath)1601 add_device_to_disklist(char *devname, char *devpath)
1602 {
1603 	struct disk_info	*search_disk;
1604 	struct ctlr_info	*search_ctlr;
1605 	struct disk_type	*search_dtype, *efi_disk;
1606 	struct partition_info	*search_parts;
1607 	struct disk_info	*dptr;
1608 	struct ctlr_info	*cptr;
1609 	struct disk_type	*type;
1610 	struct partition_info	*parts;
1611 	struct dk_label		search_label;
1612 	struct dk_cinfo		dkinfo;
1613 	struct stat		stbuf;
1614 	struct ctlr_type	*ctlr, *tctlr;
1615 	struct	mctlr_list	*mlp;
1616 	struct	efi_info	efi_info;
1617 	struct dk_minfo		mediainfo;
1618 	int			search_file;
1619 	int			status;
1620 	int			i;
1621 	int			access_flags = 0;
1622 	char			disk_name[MAXNAMELEN];
1623 
1624 	/*
1625 	 * Attempt to open the disk.  If it fails, skip it.
1626 	 */
1627 	if ((search_file = open_disk(devpath, O_RDWR | O_NDELAY)) < 0) {
1628 		return;
1629 	}
1630 	/*
1631 	 * Must be a character device
1632 	 */
1633 	if (fstat(search_file, &stbuf) == -1 || !S_ISCHR(stbuf.st_mode)) {
1634 		(void) close(search_file);
1635 		return;
1636 	}
1637 	/*
1638 	 * Attempt to read the configuration info on the disk.
1639 	 * Again, if it fails, we assume the disk's not there.
1640 	 * Note we must close the file for the disk before we
1641 	 * continue.
1642 	 */
1643 	if (ioctl(search_file, DKIOCINFO, &dkinfo) < 0) {
1644 		(void) close(search_file);
1645 		return;
1646 	}
1647 
1648 	/* If it is a removable media, skip it. */
1649 
1650 	if (!expert_mode) {
1651 		int isremovable, ret;
1652 		ret = ioctl(search_file, DKIOCREMOVABLE, &isremovable);
1653 		if ((ret >= 0) && (isremovable != 0)) {
1654 			(void) close(search_file);
1655 			return;
1656 		}
1657 	}
1658 
1659 	if (ioctl(search_file, DKIOCGMEDIAINFO, &mediainfo) == -1) {
1660 		cur_blksz = DEV_BSIZE;
1661 	} else {
1662 		cur_blksz = mediainfo.dki_lbsize;
1663 	}
1664 
1665 	/*
1666 	 * If the type of disk is one we don't know about,
1667 	 * add it to the list.
1668 	 */
1669 	mlp = controlp;
1670 
1671 	while (mlp != NULL) {
1672 		if (mlp->ctlr_type->ctype_ctype == dkinfo.dki_ctype) {
1673 			break;
1674 		}
1675 		mlp = mlp->next;
1676 	}
1677 
1678 	if (mlp == NULL) {
1679 		if (dkinfo.dki_ctype == DKC_CDROM) {
1680 			if (ioctl(search_file, DKIOCGMEDIAINFO,
1681 			    &mediainfo) < 0) {
1682 				mediainfo.dki_media_type = DK_UNKNOWN;
1683 			}
1684 		}
1685 		/*
1686 		 * Skip CDROM devices, they are read only.
1687 		 * But not devices like Iomega Rev Drive which
1688 		 * identifies itself as a CDROM, but has a removable
1689 		 * disk.
1690 		 */
1691 		if ((dkinfo.dki_ctype == DKC_CDROM) &&
1692 		    (mediainfo.dki_media_type != DK_REMOVABLE_DISK)) {
1693 			(void) close(search_file);
1694 			return;
1695 		}
1696 		/*
1697 		 * create the new ctlr_type structure and fill it in.
1698 		 */
1699 		tctlr = zalloc(sizeof (struct ctlr_type));
1700 		tctlr->ctype_ctype = dkinfo.dki_ctype;
1701 		tctlr->ctype_name = zalloc(DK_DEVLEN);
1702 		if (strlcpy(tctlr->ctype_name, dkinfo.dki_cname,
1703 		    DK_DEVLEN) > DK_DEVLEN) {
1704 			/*
1705 			 * DKIOCINFO returned a controller name longer
1706 			 * than DK_DEVLEN bytes, which means more of the
1707 			 * dk_cinfo structure may be corrupt.  We don't
1708 			 * allow the user to perform any operations on
1709 			 * the device in this case
1710 			 */
1711 			err_print("\nError: Device %s: controller "
1712 			    "name (%s)\nis invalid.  Device will not "
1713 			    "be displayed.\n", devname, dkinfo.dki_cname);
1714 			(void) close(search_file);
1715 			destroy_data(tctlr->ctype_name);
1716 			destroy_data((char *)tctlr);
1717 			return;
1718 		} else {
1719 			tctlr->ctype_ops = zalloc(sizeof (struct ctlr_ops));
1720 
1721 			/*
1722 			 * copy the generic disk ops structure into local copy.
1723 			 */
1724 			*(tctlr->ctype_ops) = genericops;
1725 
1726 			tctlr->ctype_flags = CF_WLIST;
1727 
1728 			mlp = controlp;
1729 
1730 			while (mlp->next != NULL) {
1731 				mlp = mlp->next;
1732 			}
1733 
1734 			mlp->next = zalloc(sizeof (struct mctlr_list));
1735 			mlp->next->ctlr_type = tctlr;
1736 		}
1737 	}
1738 
1739 	/*
1740 	 * Search through all disks known at this time, to
1741 	 * determine if we're already identified this disk.
1742 	 * If so, then there's no need to include it a
1743 	 * second time.  This permits the user-defined names
1744 	 * to supercede the standard conventional names.
1745 	 */
1746 	if (disk_is_known(&dkinfo)) {
1747 		(void) close(search_file);
1748 		return;
1749 	}
1750 #if defined(sparc)
1751 	/*
1752 	 * Because opening id with FNDELAY always succeeds,
1753 	 * read the label early on to see whether the device
1754 	 * really exists.  A result of DSK_RESERVED
1755 	 * means the disk may be reserved.
1756 	 * In the future, it will be good
1757 	 * to move these into controller specific files and have a common
1758 	 * generic check for reserved disks here, including intel disks.
1759 	 */
1760 	if (dkinfo.dki_ctype == DKC_SCSI_CCS) {
1761 		char	*first_sector;
1762 
1763 		first_sector = zalloc(cur_blksz);
1764 		i = scsi_rdwr(DIR_READ, search_file, (diskaddr_t)0,
1765 		    1, first_sector, F_SILENT, NULL);
1766 		switch (i) {
1767 		case DSK_RESERVED:
1768 			access_flags |= DSK_RESERVED;
1769 			break;
1770 		case DSK_UNAVAILABLE:
1771 			access_flags |= DSK_UNAVAILABLE;
1772 			break;
1773 		default:
1774 			break;
1775 		}
1776 		free(first_sector);
1777 	}
1778 #endif /* defined(sparc) */
1779 
1780 	/*
1781 	 * The disk appears to be present.  Allocate space for the
1782 	 * disk structure and add it to the list of found disks.
1783 	 */
1784 	search_disk = (struct disk_info *)zalloc(sizeof (struct disk_info));
1785 	if (disk_list == NULL)
1786 		disk_list = search_disk;
1787 	else {
1788 		for (dptr = disk_list; dptr->disk_next != NULL;
1789 		    dptr = dptr->disk_next)
1790 			;
1791 		dptr->disk_next = search_disk;
1792 	}
1793 	/*
1794 	 * Fill in some info from the ioctls.
1795 	 */
1796 	search_disk->disk_dkinfo = dkinfo;
1797 	if (is_efi_type(search_file)) {
1798 		search_disk->label_type = L_TYPE_EFI;
1799 	} else {
1800 		search_disk->label_type = L_TYPE_SOLARIS;
1801 	}
1802 	/*
1803 	 * Remember the names of the disk
1804 	 */
1805 	search_disk->disk_name = alloc_string(devname);
1806 	search_disk->disk_path = alloc_string(devpath);
1807 
1808 	/*
1809 	 * Remember the lba size of the disk
1810 	 */
1811 	search_disk->disk_lbasize = cur_blksz;
1812 
1813 	(void) strcpy(x86_devname, devname);
1814 
1815 	/*
1816 	 * Determine if this device is linked to a physical name.
1817 	 */
1818 	search_disk->devfs_name = get_physical_name(devpath);
1819 
1820 	/*
1821 	 * Try to match the ctlr for this disk with a ctlr we
1822 	 * have already found.  A match is assumed if the ctlrs
1823 	 * are at the same address && ctypes agree
1824 	 */
1825 	for (search_ctlr = ctlr_list; search_ctlr != NULL;
1826 	    search_ctlr = search_ctlr->ctlr_next)
1827 		if (search_ctlr->ctlr_addr == dkinfo.dki_addr &&
1828 		    search_ctlr->ctlr_space == dkinfo.dki_space &&
1829 		    search_ctlr->ctlr_ctype->ctype_ctype ==
1830 		    dkinfo.dki_ctype)
1831 			break;
1832 	/*
1833 	 * If no match was found, we need to identify this ctlr.
1834 	 */
1835 	if (search_ctlr == NULL) {
1836 		/*
1837 		 * Match the type of the ctlr to a known type.
1838 		 */
1839 		mlp = controlp;
1840 
1841 		while (mlp != NULL) {
1842 			if (mlp->ctlr_type->ctype_ctype == dkinfo.dki_ctype)
1843 				break;
1844 			mlp = mlp->next;
1845 		}
1846 		/*
1847 		 * If no match was found, it's an error.
1848 		 * Close the disk and report the error.
1849 		 */
1850 		if (mlp == NULL) {
1851 			err_print("\nError: found disk attached to ");
1852 			err_print("unsupported controller type '%d'.\n",
1853 			    dkinfo.dki_ctype);
1854 			(void) close(search_file);
1855 			return;
1856 		}
1857 		/*
1858 		 * Allocate space for the ctlr structure and add it
1859 		 * to the list of found ctlrs.
1860 		 */
1861 		search_ctlr = (struct ctlr_info *)
1862 		    zalloc(sizeof (struct ctlr_info));
1863 		search_ctlr->ctlr_ctype = mlp->ctlr_type;
1864 		if (ctlr_list == NULL)
1865 			ctlr_list = search_ctlr;
1866 		else {
1867 			for (cptr = ctlr_list; cptr->ctlr_next != NULL;
1868 			    cptr = cptr->ctlr_next)
1869 				;
1870 			cptr->ctlr_next = search_ctlr;
1871 		}
1872 		/*
1873 		 * Fill in info from the ioctl.
1874 		 */
1875 		for (i = 0; i < DK_DEVLEN; i++) {
1876 			search_ctlr->ctlr_cname[i] = dkinfo.dki_cname[i];
1877 			search_ctlr->ctlr_dname[i] = dkinfo.dki_dname[i];
1878 		}
1879 		/*
1880 		 * Make sure these can be used as simple strings
1881 		 */
1882 		search_ctlr->ctlr_cname[i] = 0;
1883 		search_ctlr->ctlr_dname[i] = 0;
1884 
1885 		search_ctlr->ctlr_flags = dkinfo.dki_flags;
1886 		search_ctlr->ctlr_num = dkinfo.dki_cnum;
1887 		search_ctlr->ctlr_addr = dkinfo.dki_addr;
1888 		search_ctlr->ctlr_space = dkinfo.dki_space;
1889 		search_ctlr->ctlr_prio = dkinfo.dki_prio;
1890 		search_ctlr->ctlr_vec = dkinfo.dki_vec;
1891 	}
1892 	/*
1893 	 * By this point, we have a known ctlr.  Link the disk
1894 	 * to the ctlr.
1895 	 */
1896 	search_disk->disk_ctlr = search_ctlr;
1897 	if (access_flags & (DSK_RESERVED | DSK_UNAVAILABLE)) {
1898 		if (access_flags & DSK_RESERVED)
1899 			search_disk->disk_flags |= DSK_RESERVED;
1900 		else
1901 			search_disk->disk_flags |= DSK_UNAVAILABLE;
1902 		(void) close(search_file);
1903 		return;
1904 	} else {
1905 		search_disk->disk_flags &= ~(DSK_RESERVED | DSK_UNAVAILABLE);
1906 	}
1907 
1908 	/*
1909 	 * Attempt to read the primary label.
1910 	 * (Note that this is really through the DKIOCGVTOC
1911 	 * ioctl, then converted from vtoc to label.)
1912 	 */
1913 	if (search_disk->label_type == L_TYPE_SOLARIS) {
1914 		status = read_label(search_file, &search_label);
1915 	} else {
1916 		status = read_efi_label(search_file, &efi_info, search_disk);
1917 	}
1918 	/*
1919 	 * If reading the label failed, and this is a SCSI
1920 	 * disk, we can attempt to auto-sense the disk
1921 	 * Configuration.
1922 	 */
1923 	ctlr = search_ctlr->ctlr_ctype;
1924 	if ((status == -1) &&
1925 	    (ctlr->ctype_ctype == DKC_SCSI_CCS ||
1926 	    ctlr->ctype_ctype == DKC_BLKDEV)) {
1927 		if (option_msg && diag_msg) {
1928 			err_print("%s: attempting auto configuration\n",
1929 			    search_disk->disk_name);
1930 		}
1931 
1932 		switch (search_disk->label_type) {
1933 		case (L_TYPE_SOLARIS):
1934 			if (auto_sense(search_file, 0, &search_label) != NULL) {
1935 			/*
1936 			 * Auto config worked, so we now have
1937 			 * a valid label for the disk.  Mark
1938 			 * the disk as needing the label flushed.
1939 			 */
1940 				status = 0;
1941 				search_disk->disk_flags |=
1942 				    (DSK_LABEL_DIRTY | DSK_AUTO_CONFIG);
1943 				break;
1944 			}
1945 			/* With SOLARIS label type failed, try EFI. */
1946 			/* FALLTHROUGH */
1947 		case (L_TYPE_EFI):
1948 			efi_disk = auto_efi_sense(search_file, &efi_info);
1949 			if (efi_disk != NULL) {
1950 				/*
1951 				 * Auto config worked, so we now have
1952 				 * a valid label for the disk.
1953 				 */
1954 				search_disk->label_type = L_TYPE_EFI;
1955 				status = 0;
1956 				search_disk->disk_flags |=
1957 				    (DSK_LABEL_DIRTY | DSK_AUTO_CONFIG);
1958 			}
1959 			break;
1960 		default:
1961 			/* Should never happen */
1962 			break;
1963 		}
1964 	}
1965 
1966 	/*
1967 	 * If we didn't successfully read the label, or the label
1968 	 * appears corrupt, just leave the disk as an unknown type.
1969 	 */
1970 	if (status == -1) {
1971 		(void) close(search_file);
1972 		return;
1973 	}
1974 
1975 	if (search_disk->label_type == L_TYPE_SOLARIS) {
1976 		if (!checklabel(&search_label)) {
1977 			(void) close(search_file);
1978 			return;
1979 		}
1980 		if (trim_id(search_label.dkl_asciilabel)) {
1981 			(void) close(search_file);
1982 			return;
1983 		}
1984 	}
1985 	/*
1986 	 * The label looks ok.  Mark the disk as labeled.
1987 	 */
1988 	search_disk->disk_flags |= DSK_LABEL;
1989 
1990 	if (search_disk->label_type == L_TYPE_EFI) {
1991 		search_dtype = (struct disk_type *)
1992 		    zalloc(sizeof (struct disk_type));
1993 		type = search_ctlr->ctlr_ctype->ctype_dlist;
1994 		if (type == NULL) {
1995 			search_ctlr->ctlr_ctype->ctype_dlist =
1996 			    search_dtype;
1997 		} else {
1998 			while (type->dtype_next != NULL) {
1999 				type = type->dtype_next;
2000 			}
2001 			type->dtype_next = search_dtype;
2002 		}
2003 		search_dtype->dtype_next = NULL;
2004 
2005 		search_dtype->vendor = strdup(efi_info.vendor);
2006 		search_dtype->product = strdup(efi_info.product);
2007 		search_dtype->revision = strdup(efi_info.revision);
2008 
2009 		if (search_dtype->vendor == NULL ||
2010 		    search_dtype->product == NULL ||
2011 		    search_dtype->revision == NULL) {
2012 			free(search_dtype->vendor);
2013 			free(search_dtype->product);
2014 			free(search_dtype->revision);
2015 			free(search_dtype);
2016 			goto out;
2017 		}
2018 
2019 		search_dtype->capacity = efi_info.capacity;
2020 		search_disk->disk_type = search_dtype;
2021 
2022 		search_parts = (struct partition_info *)
2023 		    zalloc(sizeof (struct partition_info));
2024 		search_dtype->dtype_plist = search_parts;
2025 
2026 		search_parts->pinfo_name = alloc_string("original");
2027 		search_parts->pinfo_next = NULL;
2028 		search_parts->etoc = efi_info.e_parts;
2029 		search_disk->disk_parts = search_parts;
2030 
2031 		/*
2032 		 * Copy the volume name, if present
2033 		 */
2034 		for (i = 0; i < search_parts->etoc->efi_nparts; i++) {
2035 			if (search_parts->etoc->efi_parts[i].p_tag ==
2036 			    V_RESERVED) {
2037 				bcopy(search_parts->etoc->efi_parts[i].p_name,
2038 				    search_disk->v_volume, LEN_DKL_VVOL);
2039 				break;
2040 			}
2041 		}
2042 	out:
2043 		(void) close(search_file);
2044 
2045 		free(efi_info.vendor);
2046 		free(efi_info.product);
2047 		free(efi_info.revision);
2048 		return;
2049 	}
2050 
2051 	/*
2052 	 * Attempt to match the disk type in the label with a
2053 	 * known disk type.
2054 	 */
2055 	for (search_dtype = search_ctlr->ctlr_ctype->ctype_dlist;
2056 	    search_dtype != NULL;
2057 	    search_dtype = search_dtype->dtype_next)
2058 		if (dtype_match(&search_label, search_dtype))
2059 			break;
2060 	/*
2061 	 * If no match was found, we need to create a disk type
2062 	 * for this disk.
2063 	 */
2064 	if (search_dtype == NULL) {
2065 		/*
2066 		 * Allocate space for the disk type and add it
2067 		 * to the list of disk types for this ctlr type.
2068 		 */
2069 		search_dtype = (struct disk_type *)
2070 		    zalloc(sizeof (struct disk_type));
2071 		type = search_ctlr->ctlr_ctype->ctype_dlist;
2072 		if (type == NULL)
2073 			search_ctlr->ctlr_ctype->ctype_dlist =
2074 			    search_dtype;
2075 		else {
2076 			while (type->dtype_next != NULL)
2077 				type = type->dtype_next;
2078 			type->dtype_next = search_dtype;
2079 		}
2080 		/*
2081 		 * Fill in the drive info from the disk label.
2082 		 */
2083 		search_dtype->dtype_next = NULL;
2084 		if (strncmp(search_label.dkl_asciilabel, "DEFAULT",
2085 		    strlen("DEFAULT")) == 0) {
2086 			(void) get_disk_name(search_file, disk_name,
2087 			    search_disk);
2088 			search_dtype->dtype_asciilabel = (char *)
2089 			    zalloc(strlen(disk_name) + 1);
2090 			(void) strcpy(search_dtype->dtype_asciilabel,
2091 			    disk_name);
2092 		} else {
2093 			search_dtype->dtype_asciilabel = (char *)
2094 			    zalloc(strlen(search_label.dkl_asciilabel) + 1);
2095 			(void) strcpy(search_dtype->dtype_asciilabel,
2096 			    search_label.dkl_asciilabel);
2097 		}
2098 		search_dtype->dtype_pcyl = search_label.dkl_pcyl;
2099 		search_dtype->dtype_ncyl = search_label.dkl_ncyl;
2100 		search_dtype->dtype_acyl = search_label.dkl_acyl;
2101 		search_dtype->dtype_nhead = search_label.dkl_nhead;
2102 		search_dtype->dtype_nsect = search_label.dkl_nsect;
2103 		search_dtype->dtype_rpm = search_label.dkl_rpm;
2104 		/*
2105 		 * Mark the disk as needing specification of
2106 		 * ctlr specific attributes.  This is necessary
2107 		 * because the label doesn't contain these attributes,
2108 		 * and they aren't known at this point.  They will
2109 		 * be asked for if this disk is ever selected by
2110 		 * the user.
2111 		 * Note: for SCSI, we believe the label.
2112 		 */
2113 		if ((search_ctlr->ctlr_ctype->ctype_ctype != DKC_SCSI_CCS) &&
2114 		    (search_ctlr->ctlr_ctype->ctype_ctype != DKC_DIRECT) &&
2115 		    (search_ctlr->ctlr_ctype->ctype_ctype != DKC_VBD) &&
2116 		    (search_ctlr->ctlr_ctype->ctype_ctype != DKC_PCMCIA_ATA) &&
2117 		    (search_ctlr->ctlr_ctype->ctype_ctype != DKC_BLKDEV)) {
2118 			search_dtype->dtype_flags |= DT_NEED_SPEFS;
2119 		}
2120 	}
2121 	/*
2122 	 * By this time we have a known disk type.  Link the disk
2123 	 * to the disk type.
2124 	 */
2125 	search_disk->disk_type = search_dtype;
2126 
2127 	/*
2128 	 * Close the file for this disk
2129 	 */
2130 	(void) close(search_file);
2131 
2132 	/*
2133 	 * Attempt to match the partition map in the label with
2134 	 * a known partition map for this disk type.
2135 	 */
2136 	for (search_parts = search_dtype->dtype_plist;
2137 	    search_parts != NULL;
2138 	    search_parts = search_parts->pinfo_next)
2139 		if (parts_match(&search_label, search_parts)) {
2140 			break;
2141 		}
2142 	/*
2143 	 * If no match was made, we need to create a partition
2144 	 * map for this disk.
2145 	 */
2146 	if (search_parts == NULL) {
2147 		/*
2148 		 * Allocate space for the partition map and add
2149 		 * it to the list of maps for this disk type.
2150 		 */
2151 		search_parts = (struct partition_info *)
2152 		    zalloc(sizeof (struct partition_info));
2153 		parts = search_dtype->dtype_plist;
2154 		if (parts == NULL)
2155 			search_dtype->dtype_plist = search_parts;
2156 		else {
2157 			while (parts->pinfo_next != NULL)
2158 				parts = parts->pinfo_next;
2159 			parts->pinfo_next = search_parts;
2160 		}
2161 		search_parts->pinfo_next = NULL;
2162 		/*
2163 		 * Fill in the name of the map with a name derived
2164 		 * from the name of this disk.  This is necessary
2165 		 * because the label contains no name for the
2166 		 * partition map.
2167 		 */
2168 		search_parts->pinfo_name = alloc_string("original");
2169 		/*
2170 		 * Fill in the partition info from the disk label.
2171 		 */
2172 		for (i = 0; i < NDKMAP; i++) {
2173 
2174 #if defined(_SUNOS_VTOC_8)
2175 			search_parts->pinfo_map[i] =
2176 			    search_label.dkl_map[i];
2177 
2178 #elif defined(_SUNOS_VTOC_16)
2179 			search_parts->pinfo_map[i].dkl_cylno =
2180 			    search_label.dkl_vtoc.v_part[i].p_start /
2181 			    ((blkaddr32_t)(search_label.dkl_nhead *
2182 			    search_label.dkl_nsect));
2183 			search_parts->pinfo_map[i].dkl_nblk =
2184 			    search_label.dkl_vtoc.v_part[i].p_size;
2185 
2186 #else
2187 #error No VTOC format defined.
2188 #endif
2189 		}
2190 	}
2191 	/*
2192 	 * If the vtoc looks valid, copy the volume name and vtoc
2193 	 * info from the label.  Otherwise, install a default vtoc.
2194 	 * This permits vtoc info to automatically appear in the sun
2195 	 * label, without requiring an upgrade procedure.
2196 	 */
2197 	if (search_label.dkl_vtoc.v_version == V_VERSION) {
2198 		bcopy(search_label.dkl_vtoc.v_volume,
2199 		    search_disk->v_volume, LEN_DKL_VVOL);
2200 		search_parts->vtoc = search_label.dkl_vtoc;
2201 	} else {
2202 		bzero(search_disk->v_volume, LEN_DKL_VVOL);
2203 		set_vtoc_defaults(search_parts);
2204 	}
2205 	/*
2206 	 * By this time we have a known partitition map.  Link the
2207 	 * disk to the partition map.
2208 	 */
2209 	search_disk->disk_parts = search_parts;
2210 }
2211 
2212 
2213 /*
2214  * Search the disk list for a disk with the identical configuration.
2215  * Return true if one is found.
2216  */
2217 static int
disk_is_known(struct dk_cinfo * dkinfo)2218 disk_is_known(struct dk_cinfo *dkinfo)
2219 {
2220 	struct disk_info	*dp;
2221 
2222 	dp = disk_list;
2223 	while (dp != NULL) {
2224 		if (dp->disk_dkinfo.dki_ctype == dkinfo->dki_ctype &&
2225 		    dp->disk_dkinfo.dki_cnum == dkinfo->dki_cnum &&
2226 		    dp->disk_dkinfo.dki_unit == dkinfo->dki_unit &&
2227 		    strcmp(dp->disk_dkinfo.dki_dname, dkinfo->dki_dname) == 0) {
2228 			return (1);
2229 		}
2230 		dp = dp->disk_next;
2231 	}
2232 	return (0);
2233 }
2234 
2235 
2236 /*
2237  * This routine checks to see if a given disk type matches the type
2238  * in the disk label.
2239  */
2240 int
dtype_match(struct dk_label * label,struct disk_type * dtype)2241 dtype_match(struct dk_label *label, struct disk_type *dtype)
2242 {
2243 
2244 	if (dtype->dtype_asciilabel == NULL) {
2245 		return (0);
2246 	}
2247 
2248 	/*
2249 	 * If the any of the physical characteristics are different, or
2250 	 * the name is different, it doesn't match.
2251 	 */
2252 	if ((strcmp(label->dkl_asciilabel, dtype->dtype_asciilabel) != 0) ||
2253 	    (label->dkl_ncyl != dtype->dtype_ncyl) ||
2254 	    (label->dkl_acyl != dtype->dtype_acyl) ||
2255 	    (label->dkl_nhead != dtype->dtype_nhead) ||
2256 	    (label->dkl_nsect != dtype->dtype_nsect)) {
2257 		return (0);
2258 	}
2259 	/*
2260 	 * If those are all identical, assume it's a match.
2261 	 */
2262 	return (1);
2263 }
2264 
2265 /*
2266  * This routine checks to see if a given partition map matches the map
2267  * in the disk label.
2268  */
2269 int
parts_match(struct dk_label * label,struct partition_info * pinfo)2270 parts_match(struct dk_label *label, struct partition_info *pinfo)
2271 {
2272 	int i;
2273 
2274 	/*
2275 	 * If any of the partition entries is different, it doesn't match.
2276 	 */
2277 	for (i = 0; i < NDKMAP; i++)
2278 
2279 #if defined(_SUNOS_VTOC_8)
2280 		if ((label->dkl_map[i].dkl_cylno !=
2281 		    pinfo->pinfo_map[i].dkl_cylno) ||
2282 		    (label->dkl_map[i].dkl_nblk !=
2283 		    pinfo->pinfo_map[i].dkl_nblk))
2284 
2285 #elif defined(_SUNOS_VTOC_16)
2286 		if ((pinfo->pinfo_map[i].dkl_cylno !=
2287 		    label->dkl_vtoc.v_part[i].p_start /
2288 		    (label->dkl_nhead * label->dkl_nsect)) ||
2289 		    (pinfo->pinfo_map[i].dkl_nblk !=
2290 		    label->dkl_vtoc.v_part[i].p_size))
2291 #else
2292 #error No VTOC format defined.
2293 #endif
2294 			return (0);
2295 	/*
2296 	 * Compare the vtoc information for a match
2297 	 * Do not require the volume name to be equal, for a match!
2298 	 */
2299 	if (label->dkl_vtoc.v_version != pinfo->vtoc.v_version)
2300 		return (0);
2301 	if (label->dkl_vtoc.v_nparts != pinfo->vtoc.v_nparts)
2302 		return (0);
2303 	for (i = 0; i < NDKMAP; i++) {
2304 		if (label->dkl_vtoc.v_part[i].p_tag !=
2305 		    pinfo->vtoc.v_part[i].p_tag)
2306 			return (0);
2307 		if (label->dkl_vtoc.v_part[i].p_flag !=
2308 		    pinfo->vtoc.v_part[i].p_flag)
2309 			return (0);
2310 	}
2311 	/*
2312 	 * If they are all identical, it's a match.
2313 	 */
2314 	return (1);
2315 }
2316 
2317 /*
2318  * This routine checks to see if the given disk name refers to the disk
2319  * in the given disk structure.
2320  */
2321 int
diskname_match(char * name,struct disk_info * disk)2322 diskname_match(char *name, struct disk_info *disk)
2323 {
2324 	struct dk_cinfo		dkinfo;
2325 	char			s[MAXPATHLEN];
2326 	int			fd;
2327 
2328 	/*
2329 	 * Match the name of the disk in the disk_info structure
2330 	 */
2331 	if (strcmp(name, disk->disk_name) == 0) {
2332 		return (1);
2333 	}
2334 
2335 	/*
2336 	 * Check to see if it's a 4.x file name in the /dev
2337 	 * directory on 5.0.  Here, we only accept the
2338 	 * canonicalized form: sd0.
2339 	 */
2340 	if (canonical4x_name(name) == 0) {
2341 		return (0);
2342 	}
2343 
2344 	(void) strcpy(s, "/dev/r");
2345 	(void) strcat(s, name);
2346 	(void) strcat(s, "c");
2347 
2348 	if ((fd = open_disk(s, O_RDWR | O_NDELAY)) < 0) {
2349 		return (0);
2350 	}
2351 
2352 	if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
2353 		(void) close(fd);
2354 		return (0);
2355 	}
2356 	(void) close(fd);
2357 
2358 	if (disk->disk_dkinfo.dki_ctype == dkinfo.dki_ctype &&
2359 	    disk->disk_dkinfo.dki_cnum == dkinfo.dki_cnum &&
2360 	    disk->disk_dkinfo.dki_unit == dkinfo.dki_unit &&
2361 	    strcmp(disk->disk_dkinfo.dki_dname, dkinfo.dki_dname) == 0) {
2362 		return (1);
2363 	}
2364 	return (0);
2365 }
2366 
2367 
2368 static void
datafile_error(char * errmsg,char * token)2369 datafile_error(char *errmsg, char *token)
2370 {
2371 	int	token_type;
2372 	TOKEN	token_buf;
2373 
2374 	/*
2375 	 * Allow us to get by controllers that the other platforms don't
2376 	 * know about.
2377 	 */
2378 	if (errmsg != NULL) {
2379 		err_print(errmsg, token);
2380 		err_print(" - %s (%d)\n", file_name, data_lineno);
2381 	}
2382 
2383 	/*
2384 	 * Re-sync the parsing at the beginning of the next line
2385 	 * unless of course we're already there.
2386 	 */
2387 	if (last_token_type != SUP_EOF && last_token_type != SUP_EOL) {
2388 		do {
2389 			token_type = sup_gettoken(token_buf);
2390 		} while (token_type != SUP_EOF && token_type != SUP_EOL);
2391 
2392 		if (token_type == SUP_EOF) {
2393 			sup_pushtoken(token_buf, token_type);
2394 		}
2395 	}
2396 }
2397 
2398 
2399 /*
2400  * Search through all defined disk types for duplicate entries
2401  * that are inconsistent with each other.  Disks with different
2402  * characteristics should be named differently.
2403  * Note that this function only checks for duplicate disks
2404  * for the same controller.  It's possible to have two disks with
2405  * the same name, but defined for different controllers.
2406  * That may or may not be a problem...
2407  */
2408 static void
search_duplicate_dtypes(void)2409 search_duplicate_dtypes(void)
2410 {
2411 	struct disk_type	*dp1;
2412 	struct disk_type	*dp2;
2413 	struct mctlr_list	*mlp;
2414 
2415 	mlp = controlp;
2416 
2417 	while (mlp != NULL) {
2418 		dp1 = mlp->ctlr_type->ctype_dlist;
2419 		while (dp1 != NULL) {
2420 			dp2 = dp1->dtype_next;
2421 			while (dp2 != NULL) {
2422 				check_dtypes_for_inconsistency(dp1, dp2);
2423 				dp2 = dp2->dtype_next;
2424 			}
2425 			dp1 = dp1->dtype_next;
2426 		}
2427 	mlp = mlp->next;
2428 	}
2429 }
2430 
2431 
2432 /*
2433  * Search through all defined partition types for duplicate entries
2434  * that are inconsistent with each other.  Partitions with different
2435  * characteristics should be named differently.
2436  * Note that this function only checks for duplicate partitions
2437  * for the same disk.  It's possible to have two partitions with
2438  * the same name, but defined for different disks.
2439  * That may or may not be a problem...
2440  */
2441 static void
search_duplicate_pinfo(void)2442 search_duplicate_pinfo(void)
2443 {
2444 	struct disk_type	*dp;
2445 	struct partition_info	*pp1;
2446 	struct partition_info	*pp2;
2447 	struct mctlr_list	*mlp;
2448 
2449 	mlp = controlp;
2450 
2451 	while (mlp != NULL) {
2452 		dp = mlp->ctlr_type->ctype_dlist;
2453 		while (dp != NULL) {
2454 			pp1 = dp->dtype_plist;
2455 			while (pp1 != NULL) {
2456 				pp2 = pp1->pinfo_next;
2457 				while (pp2 != NULL) {
2458 					check_pinfo_for_inconsistency(pp1, pp2);
2459 					pp2 = pp2->pinfo_next;
2460 				}
2461 				pp1 = pp1->pinfo_next;
2462 			}
2463 			dp = dp->dtype_next;
2464 		}
2465 	mlp = mlp->next;
2466 	}
2467 }
2468 
2469 
2470 /*
2471  * Determine if two particular disk definitions are inconsistent.
2472  * Ie:  same name, but different characteristics.
2473  * If so, print an error message and abort.
2474  */
2475 static void
check_dtypes_for_inconsistency(struct disk_type * dp1,struct disk_type * dp2)2476 check_dtypes_for_inconsistency(struct disk_type	*dp1, struct disk_type *dp2)
2477 {
2478 	int		i;
2479 	int		result;
2480 	struct chg_list	*cp1;
2481 	struct chg_list	*cp2;
2482 
2483 
2484 	/*
2485 	 * If the name's different, we're ok
2486 	 */
2487 	if (strcmp(dp1->dtype_asciilabel, dp2->dtype_asciilabel) != 0) {
2488 		return;
2489 	}
2490 
2491 	/*
2492 	 * Compare all the disks' characteristics
2493 	 */
2494 	result = 0;
2495 	result |= (dp1->dtype_flags != dp2->dtype_flags);
2496 	result |= (dp1->dtype_options != dp2->dtype_options);
2497 	result |= (dp1->dtype_fmt_time != dp2->dtype_fmt_time);
2498 	result |= (dp1->dtype_bpt != dp2->dtype_bpt);
2499 	result |= (dp1->dtype_ncyl != dp2->dtype_ncyl);
2500 	result |= (dp1->dtype_acyl != dp2->dtype_acyl);
2501 	result |= (dp1->dtype_pcyl != dp2->dtype_pcyl);
2502 	result |= (dp1->dtype_nhead != dp2->dtype_nhead);
2503 	result |= (dp1->dtype_nsect != dp2->dtype_nsect);
2504 	result |= (dp1->dtype_rpm != dp2->dtype_rpm);
2505 	result |= (dp1->dtype_cyl_skew != dp2->dtype_cyl_skew);
2506 	result |= (dp1->dtype_trk_skew != dp2->dtype_trk_skew);
2507 	result |= (dp1->dtype_trks_zone != dp2->dtype_trks_zone);
2508 	result |= (dp1->dtype_atrks != dp2->dtype_atrks);
2509 	result |= (dp1->dtype_asect != dp2->dtype_asect);
2510 	result |= (dp1->dtype_cache != dp2->dtype_cache);
2511 	result |= (dp1->dtype_threshold != dp2->dtype_threshold);
2512 	result |= (dp1->dtype_read_retries != dp2->dtype_read_retries);
2513 	result |= (dp1->dtype_write_retries != dp2->dtype_write_retries);
2514 	result |= (dp1->dtype_prefetch_min != dp2->dtype_prefetch_min);
2515 	result |= (dp1->dtype_prefetch_max != dp2->dtype_prefetch_max);
2516 	for (i = 0; i < NSPECIFICS; i++) {
2517 		result |= (dp1->dtype_specifics[i] != dp2->dtype_specifics[i]);
2518 	}
2519 
2520 	cp1 = dp1->dtype_chglist;
2521 	cp2 = dp2->dtype_chglist;
2522 	while (cp1 != NULL && cp2 != NULL) {
2523 		if (cp1 == NULL || cp2 == NULL) {
2524 			result = 1;
2525 			break;
2526 		}
2527 		result |= (cp1->pageno != cp2->pageno);
2528 		result |= (cp1->byteno != cp2->byteno);
2529 		result |= (cp1->mode != cp2->mode);
2530 		result |= (cp1->value != cp2->value);
2531 		cp1 = cp1->next;
2532 		cp2 = cp2->next;
2533 	}
2534 
2535 	if (result) {
2536 		err_print("Inconsistent definitions for disk type '%s'\n",
2537 		    dp1->dtype_asciilabel);
2538 		if (dp1->dtype_filename != NULL &&
2539 		    dp2->dtype_filename != NULL) {
2540 			err_print("%s (%d) - %s (%d)\n",
2541 			    dp1->dtype_filename, dp1->dtype_lineno,
2542 			    dp2->dtype_filename, dp2->dtype_lineno);
2543 		}
2544 		fullabort();
2545 	}
2546 }
2547 
2548 
2549 /*
2550  * Determine if two particular partition definitions are inconsistent.
2551  * Ie:  same name, but different characteristics.
2552  * If so, print an error message and abort.
2553  */
2554 static void
check_pinfo_for_inconsistency(struct partition_info * pp1,struct partition_info * pp2)2555 check_pinfo_for_inconsistency(struct partition_info *pp1,
2556     struct partition_info *pp2)
2557 {
2558 	int		i;
2559 	int		result;
2560 	struct dk_map32	*map1;
2561 	struct dk_map32	*map2;
2562 
2563 #if defined(_SUNOS_VTOC_8)
2564 	struct dk_map2	*vp1;
2565 	struct dk_map2	*vp2;
2566 
2567 #elif defined(_SUNOS_VTOC_16)
2568 	struct dkl_partition    *vp1;
2569 	struct dkl_partition    *vp2;
2570 #else
2571 #error No VTOC layout defined.
2572 #endif /* defined(_SUNOS_VTOC_8) */
2573 
2574 	/*
2575 	 * If the name's different, we're ok
2576 	 */
2577 	if (strcmp(pp1->pinfo_name, pp2->pinfo_name) != 0) {
2578 		return;
2579 	}
2580 
2581 	/*
2582 	 * Compare all the partitions' characteristics
2583 	 */
2584 	result = 0;
2585 	map1 = pp1->pinfo_map;
2586 	map2 = pp2->pinfo_map;
2587 	for (i = 0; i < NDKMAP; i++, map1++, map2++) {
2588 		result |= (map1->dkl_cylno != map2->dkl_cylno);
2589 		result |= (map1->dkl_nblk != map2->dkl_nblk);
2590 	}
2591 
2592 	/*
2593 	 * Compare the significant portions of the vtoc information
2594 	 */
2595 	vp1 = pp1->vtoc.v_part;
2596 	vp2 = pp2->vtoc.v_part;
2597 	for (i = 0; i < NDKMAP; i++, vp1++, vp2++) {
2598 		result |= (vp1->p_tag != vp2->p_tag);
2599 		result |= (vp1->p_flag != vp2->p_flag);
2600 	}
2601 
2602 	if (result) {
2603 		err_print("Inconsistent definitions for partition type '%s'\n",
2604 		    pp1->pinfo_name);
2605 		if (pp1->pinfo_filename != NULL &&
2606 		    pp2->pinfo_filename != NULL) {
2607 			err_print("%s (%d) - %s (%d)\n",
2608 			    pp1->pinfo_filename, pp1->pinfo_lineno,
2609 			    pp2->pinfo_filename, pp2->pinfo_lineno);
2610 		}
2611 		fullabort();
2612 	}
2613 }
2614 
2615 /*
2616  * Convert a string of digits into a block number.
2617  * The digits are assumed to be a block number unless the
2618  * the string is terminated by 'c', in which case it is
2619  * assumed to be in units of cylinders.  Accept a 'b'
2620  * to explictly specify blocks, for consistency.
2621  *
2622  * NB: uses the macro spc(), which requires that the
2623  * globals nhead/nsect/acyl be set up correctly.
2624  *
2625  * Returns -1 in the case of an error.
2626  */
2627 static uint_t
str2blks(char * str)2628 str2blks(char *str)
2629 {
2630 	int	blks;
2631 	char	*p;
2632 
2633 	blks = (int)strtol(str, &p, 0);
2634 	/*
2635 	 * Check what terminated the conversion.
2636 	 */
2637 	if (*p != 0) {
2638 		/*
2639 		 * Units specifier of 'c': convert cylinders to blocks
2640 		 */
2641 		if (*p == 'c') {
2642 			p++;
2643 			blks = blks * spc();
2644 		/*
2645 		 * Ignore a 'b' specifier.
2646 		 */
2647 		} else if (*p == 'b') {
2648 			p++;
2649 		}
2650 		/*
2651 		 * Anthing left over is an error
2652 		 */
2653 		if (*p != 0) {
2654 			blks = -1;
2655 		}
2656 	}
2657 
2658 	return (blks);
2659 }
2660 /*
2661  * Convert a string of digits into a cylinder number.
2662  * Accept a an optional 'c' specifier, for consistency.
2663  *
2664  * Returns -1 in the case of an error.
2665  */
2666 int
str2cyls(char * str)2667 str2cyls(char *str)
2668 {
2669 	int	cyls;
2670 	char	*p;
2671 
2672 	cyls = (int)strtol(str, &p, 0);
2673 	/*
2674 	 * Check what terminated the conversion.
2675 	 */
2676 	if (*p != 0) {
2677 		/*
2678 		 * Units specifier of 'c': accept it.
2679 		 */
2680 		if (*p == 'c') {
2681 			p++;
2682 		}
2683 		/*
2684 		 * Anthing left over is an error
2685 		 */
2686 		if (*p != 0) {
2687 			cyls = -1;
2688 		}
2689 	}
2690 
2691 	return (cyls);
2692 }
2693 
2694 
2695 /*
2696  * Create a new chg_list structure, and append it onto the
2697  * end of the current chg_list under construction.  By
2698  * applying changes in the order in which listed in the
2699  * data file, the changes we make are deterministic.
2700  * Return a pointer to the new structure, so that the
2701  * caller can fill in the appropriate information.
2702  */
2703 static struct chg_list *
new_chg_list(struct disk_type * disk)2704 new_chg_list(struct disk_type *disk)
2705 {
2706 	struct chg_list		*cp;
2707 	struct chg_list		*nc;
2708 
2709 	nc = zalloc(sizeof (struct chg_list));
2710 
2711 	if (disk->dtype_chglist == NULL) {
2712 		disk->dtype_chglist = nc;
2713 	} else {
2714 		for (cp = disk->dtype_chglist; cp->next; cp = cp->next)
2715 			;
2716 		cp->next = nc;
2717 	}
2718 	nc->next = NULL;
2719 	return (nc);
2720 }
2721 
2722 
2723 /*
2724  * Follow symbolic links from the logical device name to
2725  * the /devfs physical device name.  To be complete, we
2726  * handle the case of multiple links.  This function
2727  * either returns NULL (no links, or some other error),
2728  * or the physical device name, alloc'ed on the heap.
2729  *
2730  * Note that the standard /devices prefix is stripped from
2731  * the final pathname, if present.  The trailing options
2732  * are also removed (":c, raw").
2733  */
2734 static char *
get_physical_name(char * path)2735 get_physical_name(char *path)
2736 {
2737 	struct stat	stbuf;
2738 	int		i;
2739 	int		level;
2740 	char		*p;
2741 	char		s[MAXPATHLEN];
2742 	char		buf[MAXPATHLEN];
2743 	char		dir[MAXPATHLEN];
2744 	char		savedir[MAXPATHLEN];
2745 	char		*result = NULL;
2746 
2747 	if (getcwd(savedir, sizeof (savedir)) == NULL) {
2748 		err_print("getcwd() failed - %s\n", strerror(errno));
2749 		return (NULL);
2750 	}
2751 
2752 	(void) strcpy(s, path);
2753 	if ((p = strrchr(s, '/')) != NULL) {
2754 		*p = 0;
2755 	}
2756 	if (s[0] == 0) {
2757 		(void) strcpy(s, "/");
2758 	}
2759 	if (chdir(s) == -1) {
2760 		err_print("cannot chdir() to %s - %s\n",
2761 		    s, strerror(errno));
2762 		goto exit;
2763 	}
2764 
2765 	level = 0;
2766 	(void) strcpy(s, path);
2767 	for (;;) {
2768 		/*
2769 		 * See if there's a real file out there.  If not,
2770 		 * we have a dangling link and we ignore it.
2771 		 */
2772 		if (stat(s, &stbuf) == -1) {
2773 			goto exit;
2774 		}
2775 		if (lstat(s, &stbuf) == -1) {
2776 			err_print("%s: lstat() failed - %s\n",
2777 			    s, strerror(errno));
2778 			goto exit;
2779 		}
2780 		/*
2781 		 * If the file is not a link, we're done one
2782 		 * way or the other.  If there were links,
2783 		 * return the full pathname of the resulting
2784 		 * file.
2785 		 */
2786 		if (!S_ISLNK(stbuf.st_mode)) {
2787 			if (level > 0) {
2788 				/*
2789 				 * Strip trailing options from the
2790 				 * physical device name
2791 				 */
2792 				if ((p = strrchr(s, ':')) != NULL) {
2793 					*p = 0;
2794 				}
2795 				/*
2796 				 * Get the current directory, and
2797 				 * glue the pieces together.
2798 				 */
2799 				if (getcwd(dir, sizeof (dir)) == NULL) {
2800 					err_print("getcwd() failed - %s\n",
2801 					    strerror(errno));
2802 					goto exit;
2803 				}
2804 				(void) strcat(dir, "/");
2805 				(void) strcat(dir, s);
2806 				/*
2807 				 * If we have the standard fixed
2808 				 * /devices prefix, remove it.
2809 				 */
2810 				p = (strstr(dir, DEVFS_PREFIX) == dir) ?
2811 				    dir+strlen(DEVFS_PREFIX) : dir;
2812 				result = alloc_string(p);
2813 			}
2814 			goto exit;
2815 		}
2816 		i = readlink(s, buf, sizeof (buf));
2817 		if (i == -1) {
2818 			err_print("%s: readlink() failed - %s\n",
2819 			    s, strerror(errno));
2820 			goto exit;
2821 		}
2822 		level++;
2823 		buf[i] = 0;
2824 
2825 		/*
2826 		 * Break up the pathname into the directory
2827 		 * reference, if applicable and simple filename.
2828 		 * chdir()'ing to the directory allows us to
2829 		 * handle links with relative pathnames correctly.
2830 		 */
2831 		(void) strcpy(dir, buf);
2832 		if ((p = strrchr(dir, '/')) != NULL) {
2833 			*p = 0;
2834 			if (chdir(dir) == -1) {
2835 				err_print("cannot chdir() to %s - %s\n",
2836 				    dir, strerror(errno));
2837 				goto exit;
2838 			}
2839 			(void) strcpy(s, p+1);
2840 		} else {
2841 			(void) strcpy(s, buf);
2842 		}
2843 	}
2844 
2845 exit:
2846 	if (chdir(savedir) == -1) {
2847 		err_print("cannot chdir() to %s - %s\n",
2848 		    savedir, strerror(errno));
2849 	}
2850 
2851 	return (result);
2852 }
2853 
2854 
2855 static void
sort_disk_list(void)2856 sort_disk_list(void)
2857 {
2858 	int			n;
2859 	struct disk_info	**disks;
2860 	struct disk_info	*d;
2861 	struct disk_info	**dp;
2862 	struct disk_info	**dp2;
2863 
2864 	/*
2865 	 * Count the number of disks in the list
2866 	 */
2867 	n = 0;
2868 	for (d = disk_list; d != NULL; d = d->disk_next) {
2869 		n++;
2870 	}
2871 	if (n == 0) {
2872 		return;
2873 	}
2874 
2875 	/*
2876 	 * Allocate a simple disk list array and fill it in
2877 	 */
2878 	disks = (struct disk_info **)
2879 	    zalloc((n+1) * sizeof (struct disk_info *));
2880 
2881 	dp = disks;
2882 	for (d = disk_list; d != NULL; d = d->disk_next) {
2883 		*dp++ = d;
2884 	}
2885 	*dp = NULL;
2886 
2887 	/*
2888 	 * Sort the disk list array
2889 	 */
2890 	qsort((void *) disks, n, sizeof (struct disk_info *),
2891 	    disk_name_compare);
2892 
2893 	/*
2894 	 * Rebuild the linked list disk list structure
2895 	 */
2896 	dp = disks;
2897 	disk_list = *dp;
2898 	dp2 = dp + 1;
2899 	do {
2900 		(*dp++)->disk_next = *dp2++;
2901 	} while (*dp != NULL);
2902 
2903 	/*
2904 	 * Clean up
2905 	 */
2906 	(void) destroy_data((void *)disks);
2907 }
2908 
2909 
2910 /*
2911  * Compare two disk names
2912  */
2913 static int
disk_name_compare(const void * arg1,const void * arg2)2914 disk_name_compare(
2915 	const void	*arg1,
2916 	const void	*arg2)
2917 {
2918 	char		*s1;
2919 	char		*s2;
2920 	int		n1;
2921 	int		n2;
2922 	char		*p1;
2923 	char		*p2;
2924 
2925 	s1 = (*((struct disk_info **)arg1))->disk_name;
2926 	s2 = (*((struct disk_info **)arg2))->disk_name;
2927 
2928 	for (;;) {
2929 		if (*s1 == 0 || *s2 == 0)
2930 			break;
2931 		if (isdigit(*s1) && isdigit(*s2)) {
2932 			n1 = strtol(s1, &p1, 10);
2933 			n2 = strtol(s2, &p2, 10);
2934 			if (n1 != n2) {
2935 				return (n1 - n2);
2936 			}
2937 			s1 = p1;
2938 			s2 = p2;
2939 		} else if (*s1 != *s2) {
2940 			break;
2941 		} else {
2942 			s1++;
2943 			s2++;
2944 		}
2945 	}
2946 
2947 	return (*s1 - *s2);
2948 }
2949 
2950 static void
make_controller_list(void)2951 make_controller_list(void)
2952 {
2953 	int	x;
2954 	struct	mctlr_list	*ctlrp;
2955 
2956 	ctlrp = controlp;
2957 
2958 	for (x = nctypes; x != 0; x--) {
2959 		ctlrp = zalloc(sizeof (struct mctlr_list));
2960 		ctlrp->next = controlp;
2961 		ctlrp->ctlr_type = &ctlr_types[x - 1];
2962 		controlp = ctlrp;
2963 
2964 	}
2965 }
2966 
2967 static void
check_for_duplicate_disknames(char * arglist[])2968 check_for_duplicate_disknames(char *arglist[])
2969 {
2970 	char			*directory = "/dev/rdsk/";
2971 	char			**disklist;
2972 	int			len;
2973 	char			s[MAXPATHLEN], t[MAXPATHLEN];
2974 	int			diskno = 0;
2975 	int			i;
2976 
2977 
2978 	len = strlen(directory);
2979 	disklist = arglist;
2980 	for (; *disklist != NULL; disklist++) {
2981 		if (strncmp(directory, *disklist, len) == 0) {
2982 			/* Disk is in conventional format */
2983 			canonicalize_name(s, *disklist);
2984 			/*
2985 			 *  check if the disk is already present in
2986 			 *  disk list.
2987 			 */
2988 			for (i = 0; i < diskno; i++) {
2989 				canonicalize_name(t, arglist[i]);
2990 				if (strncmp(s, t, strlen(t)) == 0)
2991 					break;
2992 			}
2993 			if (i != diskno)
2994 				continue;
2995 		}
2996 		(void) strcpy(arglist[diskno], *disklist);
2997 		diskno++;
2998 	}
2999 	arglist[diskno] = NULL;
3000 }
3001 
3002 #define	DISK_PREFIX	"/dev/rdsk/"
3003 
3004 /*
3005  * This Function checks if the non-conventional name is a a link to
3006  * one of the conventional whole disk name.
3007  */
3008 static int
name_represents_wholedisk(char * name)3009 name_represents_wholedisk(char	*name)
3010 {
3011 	char	symname[MAXPATHLEN];
3012 	char	localname[MAXPATHLEN];
3013 	char	*nameptr;
3014 	ssize_t symname_size;
3015 
3016 	if (strlcpy(localname, name, MAXPATHLEN) >= MAXPATHLEN)
3017 		return (1); /* buffer overflow, reject this name */
3018 
3019 	while ((symname_size = readlink(
3020 	    localname, symname, MAXPATHLEN - 1)) != -1) {
3021 		symname[symname_size] = '\0';
3022 		nameptr = symname;
3023 		if (strncmp(symname, DISK_PREFIX,
3024 		    (sizeof (DISK_PREFIX) - 1)) == 0)
3025 			nameptr += (sizeof (DISK_PREFIX) - 1);
3026 
3027 		if (conventional_name(nameptr)) {
3028 			if (whole_disk_name(nameptr))
3029 				return (0);
3030 			else
3031 				return (1);
3032 		}
3033 
3034 		(void) strcpy(localname, symname);
3035 	}
3036 	return (0);
3037 }
3038