xref: /illumos-gate/usr/src/cmd/format/startup.c (revision 73a801acfbef73aca0012eca23d6a1b842f5a104)
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
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
290 usage()
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
314 sup_init()
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
414 sup_prxfile()
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
489 sup_setpath()
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
553 sup_setdtype()
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;
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 	 * Check to be sure all the necessary attributes have been defined.
783 	 * If any are missing, it's an error.  Also, log options for later
784 	 * use by specific driver.
785 	 */
786 	dtype->dtype_options = flags;
787 	if ((flags & SUP_MIN_DRIVE) != SUP_MIN_DRIVE) {
788 		datafile_error("Incomplete specification", "");
789 		return;
790 	}
791 	if ((!(ctype->ctype_flags & CF_SCSI)) && (!(flags & SUP_BPT)) &&
792 	    (!(ctype->ctype_flags & CF_NOFORMAT))) {
793 		datafile_error("Incomplete specification", "");
794 		return;
795 	}
796 	if ((ctype->ctype_flags & CF_SMD_DEFS) && (!(flags & SUP_BPS))) {
797 		datafile_error("Incomplete specification", "");
798 		return;
799 	}
800 	/*
801 	 * Add this disk type to the list for the ctlr type
802 	 */
803 	assert(flags & SUP_CTLR);
804 	type = ctype->ctype_dlist;
805 	if (type == NULL) {
806 		ctype->ctype_dlist = dtype;
807 	} else {
808 		while (type->dtype_next != NULL)
809 			type = type->dtype_next;
810 		type->dtype_next = dtype;
811 	}
812 }
813 
814 
815 /*
816  * Parse a SCSI mode page change specification.
817  *
818  * Return:
819  *		0:  not change specification, continue parsing
820  *		1:  was change specification, it was ok,
821  *		    or we already handled the error.
822  */
823 static int
824 sup_change_spec(struct disk_type *disk, char *id)
825 {
826 	char		*p;
827 	char		*p2;
828 	int		pageno;
829 	int		byteno;
830 	int		mode;
831 	int		value;
832 	TOKEN		token;
833 	TOKEN		ident;
834 	struct chg_list	*cp;
835 	int		tilde;
836 	int		i;
837 
838 	/*
839 	 * Syntax: p[<nn>|0x<xx>]
840 	 */
841 	if (*id != 'p') {
842 		return (0);
843 	}
844 	pageno = (int)strtol(id+1, &p2, 0);
845 	if (*p2 != 0) {
846 		return (0);
847 	}
848 	/*
849 	 * Once we get this far, we know we have the
850 	 * beginnings of a change specification.
851 	 * If there's a problem now, report the problem,
852 	 * and return 1, so that the caller can restart
853 	 * parsing at the next expression.
854 	 */
855 	if (!scsi_supported_page(pageno)) {
856 		datafile_error("Unsupported mode page '%s'", id);
857 		return (1);
858 	}
859 	/*
860 	 * Next token should be the byte offset
861 	 */
862 	if (sup_gettoken(token) != SUP_STRING) {
863 		datafile_error("Unexpected value '%s'", token);
864 		return (1);
865 	}
866 	clean_token(ident, token);
867 
868 	/*
869 	 * Syntax: b[<nn>|0x<xx>]
870 	 */
871 	p = ident;
872 	if (*p++ != 'b') {
873 		datafile_error("Unknown keyword '%s'", ident);
874 		return (1);
875 	}
876 	byteno = (int)strtol(p, &p2, 10);
877 	if (*p2 != 0) {
878 		datafile_error("Unknown keyword '%s'", ident);
879 		return (1);
880 	}
881 	if (byteno == 0 || byteno == 1) {
882 		datafile_error("Unsupported byte offset '%s'", ident);
883 		return (1);
884 	}
885 
886 	/*
887 	 * Get the operator for this expression
888 	 */
889 	mode = CHG_MODE_UNDEFINED;
890 	switch (sup_gettoken(token)) {
891 	case SUP_EQL:
892 		mode = CHG_MODE_ABS;
893 		break;
894 	case SUP_OR:
895 		if (sup_gettoken(token) == SUP_EQL)
896 			mode = CHG_MODE_SET;
897 		break;
898 	case SUP_AND:
899 		if (sup_gettoken(token) == SUP_EQL)
900 			mode = CHG_MODE_CLR;
901 		break;
902 	}
903 	if (mode == CHG_MODE_UNDEFINED) {
904 		datafile_error("Unexpected operator: '%s'", token);
905 		return (1);
906 	}
907 
908 	/*
909 	 * Get right-hand of expression - accept optional tilde
910 	 */
911 	tilde = 0;
912 	if ((i = sup_gettoken(token)) == SUP_TILDE) {
913 		tilde = 1;
914 		i = sup_gettoken(token);
915 	}
916 	if (i != SUP_STRING) {
917 		datafile_error("Expecting value, found '%s'", token);
918 		return (1);
919 	}
920 	clean_token(ident, token);
921 	value = (int)strtol(ident, &p, 0);
922 	if (*p != 0) {
923 		datafile_error("Expecting value, found '%s'", token);
924 		return (1);
925 	}
926 
927 	/*
928 	 * Apply the tilde operator, if found.
929 	 * Constrain to a byte value.
930 	 */
931 	if (tilde) {
932 		value = ~value;
933 	}
934 	value &= 0xff;
935 
936 	/*
937 	 * We parsed a successful change specification expression.
938 	 * Add it to the list for this disk type.
939 	 */
940 	cp = new_chg_list(disk);
941 	cp->pageno = pageno;
942 	cp->byteno = byteno;
943 	cp->mode = mode;
944 	cp->value = value;
945 	return (1);
946 }
947 
948 
949 /*
950  * This routine processes a 'partition' line in the data file.  It defines
951  * a known partition map for a particular disk type on a particular
952  * controller type.
953  */
954 static void
955 sup_setpart()
956 {
957 	TOKEN	token, cleaned, disk, ctlr, ident;
958 	struct	disk_type *dtype = NULL;
959 	struct	ctlr_type *ctype = NULL;
960 	struct	partition_info *pinfo, *parts;
961 	char	*pinfo_name;
962 	int	i, index, status, flags = 0;
963 	uint_t	val1, val2;
964 	ushort_t	vtoc_tag;
965 	ushort_t	vtoc_flag;
966 	struct	mctlr_list	*mlp;
967 
968 	/*
969 	 * Pull in some grammar.
970 	 */
971 	status = sup_gettoken(token);
972 	if (status != SUP_EQL) {
973 		datafile_error("Expecting '=', found '%s'", token);
974 		return;
975 	}
976 	/*
977 	 * Pull in the name of the map.
978 	 */
979 	status = sup_gettoken(token);
980 	if (status != SUP_STRING) {
981 		datafile_error("Expecting value, found '%s'", token);
982 		return;
983 	}
984 	clean_token(cleaned, token);
985 	/*
986 	 * Allocate space for the partition map and fill in the name.
987 	 */
988 	pinfo_name = (char *)zalloc(strlen(cleaned) + 1);
989 	(void) strcpy(pinfo_name, cleaned);
990 	pinfo = (struct partition_info *)zalloc(sizeof (struct partition_info));
991 	pinfo->pinfo_name = pinfo_name;
992 	/*
993 	 * Save the filename/linenumber where this partition was defined
994 	 */
995 	pinfo->pinfo_filename = file_name;
996 	pinfo->pinfo_lineno = data_lineno;
997 
998 	/*
999 	 * Install default vtoc information into the new partition table
1000 	 */
1001 	set_vtoc_defaults(pinfo);
1002 
1003 	/*
1004 	 * Loop for each attribute in the line.
1005 	 */
1006 	for (;;) {
1007 		/*
1008 		 * Pull in some grammar.
1009 		 */
1010 		status = sup_gettoken(token);
1011 		/*
1012 		 * If we hit end of line, we're done.
1013 		 */
1014 		if (status == SUP_EOL)
1015 			break;
1016 		if (status != SUP_COLON) {
1017 			datafile_error("Expecting ':', found '%s'", token);
1018 			return;
1019 		}
1020 		/*
1021 		 * Pull in the attribute.
1022 		 */
1023 		status = sup_gettoken(token);
1024 		/*
1025 		 * If we hit end of line, we're done.
1026 		 */
1027 		if (status == SUP_EOL)
1028 			break;
1029 		if (status != SUP_STRING) {
1030 			datafile_error("Expecting keyword, found '%s'", token);
1031 			return;
1032 		}
1033 		clean_token(ident, token);
1034 		/*
1035 		 * Pull in more grammar.
1036 		 */
1037 		status = sup_gettoken(token);
1038 		if (status != SUP_EQL) {
1039 			datafile_error("Expecting '=', found '%s'", token);
1040 			return;
1041 		}
1042 		/*
1043 		 * Pull in the value of the attribute.
1044 		 */
1045 		status = sup_gettoken(token);
1046 		/*
1047 		 * If we hit a key character, it's an error.
1048 		 */
1049 		if (status != SUP_STRING) {
1050 			datafile_error("Expecting value, found '%s'", token);
1051 			return;
1052 		}
1053 		clean_token(cleaned, token);
1054 		/*
1055 		 * If the attribute is the ctlr, save the ctlr name and
1056 		 * mark it defined.
1057 		 */
1058 		if (strcmp(ident, "ctlr") == 0) {
1059 			(void) strcpy(ctlr, cleaned);
1060 			flags |= SUP_CTLR;
1061 			continue;
1062 		/*
1063 		 * If the attribute is the disk, save the disk name and
1064 		 * mark it defined.
1065 		 */
1066 		} else if (strcmp(ident, "disk") == 0) {
1067 			(void) strcpy(disk, cleaned);
1068 			flags |= SUP_DISK;
1069 			continue;
1070 		}
1071 		/*
1072 		 * If we now know both the controller name and the
1073 		 * disk name, let's see if we can find the controller
1074 		 * and disk type.  This will give us the geometry,
1075 		 * which can permit us to accept partitions specs
1076 		 * in cylinders or blocks.
1077 		 */
1078 		if (((flags & (SUP_DISK|SUP_CTLR)) == (SUP_DISK|SUP_CTLR)) &&
1079 		    dtype == NULL && ctype == NULL) {
1080 			/*
1081 			 * Attempt to match the specified ctlr to a known type.
1082 			 */
1083 			mlp = controlp;
1084 
1085 			while (mlp != NULL) {
1086 				if (strcmp(mlp->ctlr_type->ctype_name,
1087 				    ctlr) == 0)
1088 					break;
1089 				mlp = mlp->next;
1090 			}
1091 			/*
1092 			 * If no match is found, it's an error.
1093 			 */
1094 			if (mlp == NULL) {
1095 				for (i = 0; i < OTHER_CTLRS; i++) {
1096 					if (strcmp(other_ctlrs[i], ctlr) == 0) {
1097 						datafile_error(NULL, NULL);
1098 						return;
1099 					}
1100 				}
1101 				if (i == OTHER_CTLRS) {
1102 					datafile_error(
1103 					    "Unknown controller '%s'", ctlr);
1104 					return;
1105 				}
1106 			}
1107 			ctype = mlp->ctlr_type;
1108 			/*
1109 			 * Attempt to match the specified disk to a known type.
1110 			 */
1111 			for (dtype = ctype->ctype_dlist; dtype != NULL;
1112 			    dtype = dtype->dtype_next) {
1113 				if (strcmp(dtype->dtype_asciilabel, disk) == 0)
1114 					break;
1115 			}
1116 			/*
1117 			 * If no match is found, it's an error.
1118 			 */
1119 			if (dtype == NULL) {
1120 				datafile_error("Unknown disk '%s'", disk);
1121 				return;
1122 			}
1123 			/*
1124 			 * Now that we know the disk type, set up the
1125 			 * globals that let that magic macro "spc()"
1126 			 * do it's thing.  Sorry that this is glued
1127 			 * together so poorly...
1128 			 */
1129 			nhead = dtype->dtype_nhead;
1130 			nsect = dtype->dtype_nsect;
1131 			acyl = dtype->dtype_acyl;
1132 			ncyl = dtype->dtype_ncyl;
1133 		}
1134 		/*
1135 		 * By now, the disk and controller type must be defined
1136 		 */
1137 		if (dtype == NULL || ctype == NULL) {
1138 			datafile_error("Incomplete specification", "");
1139 			return;
1140 		}
1141 		/*
1142 		 * The rest of the attributes are all single letters.
1143 		 * Make sure the specified attribute is a single letter.
1144 		 */
1145 		if (strlen(ident) != 1) {
1146 			datafile_error("Unknown keyword '%s'", ident);
1147 			return;
1148 		}
1149 		/*
1150 		 * Also make sure it is within the legal range of letters.
1151 		 */
1152 		if (ident[0] < PARTITION_BASE || ident[0] > PARTITION_BASE+9) {
1153 			datafile_error("Unknown keyword '%s'", ident);
1154 			return;
1155 		}
1156 		/*
1157 		 * Here's the index of the partition we're dealing with
1158 		 */
1159 		index = ident[0] - PARTITION_BASE;
1160 		/*
1161 		 * For SunOS 5.0, we support the additional syntax:
1162 		 *	[<tag>, ] [<flag>, ] <start>, <end>
1163 		 * instead of:
1164 		 *	<start>, <end>
1165 		 *
1166 		 * <tag> may be one of: boot, root, swap, etc.
1167 		 * <flag> consists of two characters:
1168 		 *	W (writable) or R (read-only)
1169 		 *	M (mountable) or U (unmountable)
1170 		 *
1171 		 * Start with the defaults assigned above:
1172 		 */
1173 		vtoc_tag = pinfo->vtoc.v_part[index].p_tag;
1174 		vtoc_flag = pinfo->vtoc.v_part[index].p_flag;
1175 
1176 		/*
1177 		 * First try to match token against possible tag values
1178 		 */
1179 		if (find_value(ptag_choices, cleaned, &i) == 1) {
1180 			/*
1181 			 * Found valid tag. Use it and advance parser
1182 			 */
1183 			vtoc_tag = (ushort_t)i;
1184 			status = sup_gettoken(token);
1185 			if (status != SUP_COMMA) {
1186 				datafile_error(
1187 				    "Expecting ', ', found '%s'", token);
1188 				return;
1189 			}
1190 			status = sup_gettoken(token);
1191 			if (status != SUP_STRING) {
1192 				datafile_error("Expecting value, found '%s'",
1193 				    token);
1194 				return;
1195 			}
1196 			clean_token(cleaned, token);
1197 		}
1198 
1199 		/*
1200 		 * Try to match token against possible flag values
1201 		 */
1202 		if (find_value(pflag_choices, cleaned, &i) == 1) {
1203 			/*
1204 			 * Found valid flag. Use it and advance parser
1205 			 */
1206 			vtoc_flag = (ushort_t)i;
1207 			status = sup_gettoken(token);
1208 			if (status != SUP_COMMA) {
1209 				datafile_error("Expecting ', ', found '%s'",
1210 				    token);
1211 				return;
1212 			}
1213 			status = sup_gettoken(token);
1214 			if (status != SUP_STRING) {
1215 				datafile_error("Expecting value, found '%s'",
1216 				    token);
1217 				return;
1218 			}
1219 			clean_token(cleaned, token);
1220 		}
1221 		/*
1222 		 * All other attributes have a pair of numeric values.
1223 		 * Convert the first value to a number.  This value
1224 		 * is the starting cylinder number of the partition.
1225 		 */
1226 		val1 = str2cyls(cleaned);
1227 		if (val1 == (uint_t)(-1)) {
1228 			datafile_error("Expecting an integer, found '%s'",
1229 			    cleaned);
1230 			return;
1231 		}
1232 		/*
1233 		 * Pull in some grammar.
1234 		 */
1235 		status = sup_gettoken(token);
1236 		if (status != SUP_COMMA) {
1237 			datafile_error("Expecting ', ', found '%s'", token);
1238 			return;
1239 		}
1240 		/*
1241 		 * Pull in the second value.
1242 		 */
1243 		status = sup_gettoken(token);
1244 		if (status != SUP_STRING) {
1245 			datafile_error("Expecting value, found '%s'", token);
1246 			return;
1247 		}
1248 		clean_token(cleaned, token);
1249 		/*
1250 		 * Convert the second value to a number.  This value
1251 		 * is the number of blocks composing the partition.
1252 		 * If the token is terminated with a 'c', the units
1253 		 * are cylinders, not blocks.  Also accept a 'b', if
1254 		 * they choose to be so specific.
1255 		 */
1256 		val2 = str2blks(cleaned);
1257 		if (val2 == (uint_t)(-1)) {
1258 			datafile_error("Expecting an integer, found '%s'",
1259 			    cleaned);
1260 			return;
1261 		}
1262 		/*
1263 		 * Fill in the appropriate map entry with the values.
1264 		 */
1265 		pinfo->pinfo_map[index].dkl_cylno = val1;
1266 		pinfo->pinfo_map[index].dkl_nblk = val2;
1267 		pinfo->vtoc.v_part[index].p_tag = vtoc_tag;
1268 		pinfo->vtoc.v_part[index].p_flag = vtoc_flag;
1269 
1270 #if defined(_SUNOS_VTOC_16)
1271 		pinfo->vtoc.v_part[index].p_start = val1 * (nhead * nsect);
1272 		pinfo->vtoc.v_part[index].p_size = val2;
1273 
1274 		if (val2 == 0) {
1275 			pinfo->vtoc.v_part[index].p_tag = 0;
1276 			pinfo->vtoc.v_part[index].p_flag = 0;
1277 			pinfo->vtoc.v_part[index].p_start = 0;
1278 			pinfo->pinfo_map[index].dkl_cylno = 0;
1279 		}
1280 #endif /* defined(_SUNOS_VTOC_16) */
1281 
1282 	}
1283 	/*
1284 	 * Check to be sure that all necessary attributes were defined.
1285 	 */
1286 	if ((flags & SUP_MIN_PART) != SUP_MIN_PART) {
1287 		datafile_error("Incomplete specification", "");
1288 		return;
1289 	}
1290 	/*
1291 	 * Add this partition map to the list of known maps for the
1292 	 * specified disk/ctlr.
1293 	 */
1294 	parts = dtype->dtype_plist;
1295 	if (parts == NULL)
1296 		dtype->dtype_plist = pinfo;
1297 	else {
1298 		while (parts->pinfo_next != NULL)
1299 			parts = parts->pinfo_next;
1300 		parts->pinfo_next = pinfo;
1301 	}
1302 }
1303 
1304 /*
1305  * Open the disk device - just a wrapper for open.
1306  */
1307 int
1308 open_disk(char *diskname, int flags)
1309 {
1310 	return (open(diskname, flags));
1311 }
1312 
1313 /*
1314  * This routine performs the disk search during startup.  It looks for
1315  * all the disks in the search path, and creates a list of those that
1316  * are found.
1317  */
1318 void
1319 do_search(char *arglist[])
1320 {
1321 	char			**sp;
1322 	DIR			*dir;
1323 	struct dirent		*dp;
1324 	char			s[MAXPATHLEN];
1325 	char			path[MAXPATHLEN];
1326 	char			curdir[MAXPATHLEN];
1327 	char			*directory = "/dev/rdsk";
1328 	struct disk_info	*disk;
1329 	int			i;
1330 
1331 	/*
1332 	 * Change directory to the device directory.  This
1333 	 * gives us the most efficient access to that directory.
1334 	 * Remember where we were, and return there when finished.
1335 	 */
1336 	if (getcwd(curdir, sizeof (curdir)) == NULL) {
1337 		err_print("Cannot get current directory - %s\n",
1338 		    strerror(errno));
1339 		fullabort();
1340 	}
1341 	if (chdir(directory) == -1) {
1342 		err_print("Cannot set directory to %s - %s\n",
1343 		    directory, strerror(errno));
1344 		fullabort();
1345 	}
1346 
1347 	/*
1348 	 * If there were disks specified on the command line,
1349 	 * use those disks, and nothing but those disks.
1350 	 */
1351 	if (arglist != NULL) {
1352 		check_for_duplicate_disknames(arglist);
1353 		for (; *arglist != NULL; arglist++) {
1354 			search_for_logical_dev(*arglist);
1355 		}
1356 	} else {
1357 		/*
1358 		 * If there were no disks specified on the command line,
1359 		 * search for all disks attached to the system.
1360 		 */
1361 		fmt_print("Searching for disks...");
1362 		(void) fflush(stdout);
1363 		need_newline = 1;
1364 
1365 		/*
1366 		 * Find all disks specified in search_path definitions
1367 		 * in whatever format.dat files were processed.
1368 		 */
1369 		sp = search_path;
1370 		if (sp != NULL) {
1371 			while (*sp != NULL) {
1372 				search_for_logical_dev(*sp++);
1373 			}
1374 		}
1375 
1376 		/*
1377 		 * Open the device directory
1378 		 */
1379 		if ((dir = opendir(".")) == NULL) {
1380 			err_print("Cannot open %s - %s\n",
1381 			    directory, strerror(errno));
1382 			fullabort();
1383 		}
1384 
1385 		/*
1386 		 * Now find all usable nodes in /dev/rdsk (or /dev, if 4.x)
1387 		 * First find all nodes which do not conform to
1388 		 * standard disk naming conventions.  This permits
1389 		 * all user-defined names to override the default names.
1390 		 */
1391 		while ((dp = readdir(dir)) != NULL) {
1392 			if (strcmp(dp->d_name, ".") == 0 ||
1393 			    strcmp(dp->d_name, "..") == 0)
1394 				continue;
1395 			if (!conventional_name(dp->d_name)) {
1396 				if (!fdisk_physical_name(dp->d_name)) {
1397 					/*
1398 					 * If non-conventional name represents
1399 					 * a link to non-s2 slice , ignore it.
1400 					 */
1401 					if (!name_represents_wholedisk
1402 					    (dp->d_name)) {
1403 						(void) strcpy(path, directory);
1404 						(void) strcat(path, "/");
1405 						(void) strcat(path, dp->d_name);
1406 						add_device_to_disklist(
1407 						    dp->d_name, path);
1408 					}
1409 				}
1410 			}
1411 		}
1412 		rewinddir(dir);
1413 
1414 
1415 		/*
1416 		 * Now find all nodes corresponding to the standard
1417 		 * device naming conventions.
1418 		 */
1419 		while ((dp = readdir(dir)) != NULL) {
1420 			if (strcmp(dp->d_name, ".") == 0 ||
1421 			    strcmp(dp->d_name, "..") == 0)
1422 				continue;
1423 			if (whole_disk_name(dp->d_name)) {
1424 				(void) strcpy(path, directory);
1425 				(void) strcat(path, "/");
1426 				(void) strcat(path, dp->d_name);
1427 				canonicalize_name(s, dp->d_name);
1428 				add_device_to_disklist(s, path);
1429 			}
1430 		}
1431 		/*
1432 		 * Close the directory
1433 		 */
1434 		if (closedir(dir) == -1) {
1435 			err_print("Cannot close directory %s - %s\n",
1436 			    directory, strerror(errno));
1437 			fullabort();
1438 		}
1439 
1440 		need_newline = 0;
1441 		fmt_print("done\n");
1442 	}
1443 
1444 	/*
1445 	 * Return to whence we came
1446 	 */
1447 	if (chdir(curdir) == -1) {
1448 		err_print("Cannot set directory to %s - %s\n",
1449 		    curdir, strerror(errno));
1450 		fullabort();
1451 	}
1452 
1453 	/*
1454 	 * If we didn't find any disks, give up.
1455 	 */
1456 	if (disk_list == NULL) {
1457 		if (geteuid() == 0) {
1458 			err_print("No disks found!\n");
1459 		} else {
1460 			err_print("No permission (or no disks found)!\n");
1461 		}
1462 		(void) fflush(stdout);
1463 		fullabort();
1464 	}
1465 
1466 	sort_disk_list();
1467 
1468 	/*
1469 	 * Tell user the results of the auto-configure process
1470 	 */
1471 	i = 0;
1472 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
1473 		float			scaled;
1474 		diskaddr_t		nblks;
1475 		struct disk_type	*type;
1476 		if (disk->disk_flags & DSK_AUTO_CONFIG) {
1477 			if (i++ == 0) {
1478 				fmt_print("\n");
1479 			}
1480 			fmt_print("%s: ", disk->disk_name);
1481 			if (disk->disk_flags & DSK_LABEL_DIRTY) {
1482 				fmt_print("configured ");
1483 			} else {
1484 				fmt_print("configured and labeled ");
1485 			}
1486 			type = disk->disk_type;
1487 			nblks = type->dtype_ncyl * type->dtype_nhead *
1488 			    type->dtype_nsect;
1489 			if (disk->label_type == L_TYPE_SOLARIS)
1490 				scaled = bn2mb(nblks);
1491 			else
1492 				scaled = bn2mb(type->capacity);
1493 			fmt_print("with capacity of ");
1494 			if (scaled > 1024.0) {
1495 				fmt_print("%1.2fGB\n", scaled/1024.0);
1496 			} else {
1497 				fmt_print("%1.2fMB\n", scaled);
1498 			}
1499 		}
1500 	}
1501 }
1502 
1503 
1504 /*
1505  * For a given "logical" disk name as specified in a format.dat
1506  * search path, try to find the device it actually refers to.
1507  * Since we are trying to maintain 4.x naming convention
1508  * compatibility in 5.0, this involves a little bit of work.
1509  * We also want to be able to function under 4.x, if needed.
1510  *
1511  * canonical:	standard name reference.  append a partition
1512  *	reference, and open that file in the device directory.
1513  *	examples:	SVR4:	c0t0d0
1514  *			4.x:	sd0
1515  *
1516  * absolute:	begins with a '/', and is assumed to be an
1517  *	absolute pathname to some node.
1518  *
1519  * relative:	non-canonical, doesn't begin with a '/'.
1520  *	assumed to be the name of a file in the appropriate
1521  *	device directory.
1522  */
1523 static void
1524 search_for_logical_dev(char *devname)
1525 {
1526 	char		path[MAXPATHLEN];
1527 	char		*directory = "/dev/rdsk/";
1528 	char		*partition = "s2";
1529 
1530 	/*
1531 	 * If the name is an absolute path name, accept it as is
1532 	 */
1533 	if (*devname == '/') {
1534 		(void) strcpy(path, devname);
1535 	} else if (canonical_name(devname)) {
1536 		/*
1537 		 * If canonical name, construct a standard path name.
1538 		 */
1539 		(void) strcpy(path, directory);
1540 		(void) strcat(path, devname);
1541 		(void) strcat(path, partition);
1542 	} else if (canonical4x_name(devname)) {
1543 		/*
1544 		 * Check to see if it's a 4.x file name in the /dev
1545 		 * directory on 5.0.  Here, we only accept the
1546 		 * canonicalized form: sd0.
1547 		 */
1548 		(void) strcpy(path, "/dev/r");
1549 		(void) strcat(path, devname);
1550 		(void) strcat(path, "c");
1551 	} else {
1552 		/*
1553 		 * If it's not a canonical name, then it may be a
1554 		 * reference to an actual file name in the device
1555 		 * directory itself.
1556 		 */
1557 		(void) strcpy(path, directory);
1558 		(void) strcat(path, devname);
1559 	}
1560 
1561 	/* now add the device */
1562 	add_device_to_disklist(devname, path);
1563 }
1564 
1565 /*
1566  * Get the disk name from the inquiry data
1567  */
1568 static void
1569 get_disk_name(int fd, char *disk_name, struct disk_info *disk_info)
1570 {
1571 	struct scsi_inquiry	inquiry;
1572 	char			*vid, *pid, *rid;
1573 
1574 	if (get_disk_inquiry_prop(disk_info->devfs_name, &vid, &pid, &rid)
1575 	    == 0) {
1576 		(void) snprintf(disk_name, MAXNAMELEN - 1, "%s-%s-%s",
1577 		    vid, pid, rid);
1578 		free(vid);
1579 		free(pid);
1580 		free(rid);
1581 
1582 		return;
1583 	}
1584 
1585 	if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
1586 		if (option_msg)
1587 			err_print("\nInquiry failed - %s\n", strerror(errno));
1588 		(void) strcpy(disk_name, "Unknown-Unknown-0001");
1589 		return;
1590 	}
1591 
1592 	(void) get_generic_disk_name(disk_name, &inquiry);
1593 }
1594 
1595 /*
1596  * Add a device to the disk list, if it appears to be a disk,
1597  * and we haven't already found it under some other name.
1598  */
1599 static void
1600 add_device_to_disklist(char *devname, char *devpath)
1601 {
1602 	struct disk_info	*search_disk;
1603 	struct ctlr_info	*search_ctlr;
1604 	struct disk_type	*search_dtype, *efi_disk;
1605 	struct partition_info	*search_parts;
1606 	struct disk_info	*dptr;
1607 	struct ctlr_info	*cptr;
1608 	struct disk_type	*type;
1609 	struct partition_info	*parts;
1610 	struct dk_label		search_label;
1611 	struct dk_cinfo		dkinfo;
1612 	struct stat		stbuf;
1613 	struct ctlr_type	*ctlr, *tctlr;
1614 	struct	mctlr_list	*mlp;
1615 	struct	efi_info	efi_info;
1616 	struct dk_minfo		mediainfo;
1617 	int			search_file;
1618 	int			status;
1619 	int			i;
1620 	int			access_flags = 0;
1621 	char			disk_name[MAXNAMELEN];
1622 
1623 	/*
1624 	 * Attempt to open the disk.  If it fails, skip it.
1625 	 */
1626 	if ((search_file = open_disk(devpath, O_RDWR | O_NDELAY)) < 0) {
1627 		return;
1628 	}
1629 	/*
1630 	 * Must be a character device
1631 	 */
1632 	if (fstat(search_file, &stbuf) == -1 || !S_ISCHR(stbuf.st_mode)) {
1633 		(void) close(search_file);
1634 		return;
1635 	}
1636 	/*
1637 	 * Attempt to read the configuration info on the disk.
1638 	 * Again, if it fails, we assume the disk's not there.
1639 	 * Note we must close the file for the disk before we
1640 	 * continue.
1641 	 */
1642 	if (ioctl(search_file, DKIOCINFO, &dkinfo) < 0) {
1643 		(void) close(search_file);
1644 		return;
1645 	}
1646 
1647 	/* If it is a removable media, skip it. */
1648 
1649 	if (!expert_mode) {
1650 		int isremovable, ret;
1651 		ret = ioctl(search_file, DKIOCREMOVABLE, &isremovable);
1652 		if ((ret >= 0) && (isremovable != 0)) {
1653 			(void) close(search_file);
1654 			return;
1655 		}
1656 	}
1657 
1658 	if (ioctl(search_file, DKIOCGMEDIAINFO, &mediainfo) == -1) {
1659 		cur_blksz = DEV_BSIZE;
1660 	} else {
1661 		cur_blksz = mediainfo.dki_lbsize;
1662 	}
1663 
1664 	/*
1665 	 * If the type of disk is one we don't know about,
1666 	 * add it to the list.
1667 	 */
1668 	mlp = controlp;
1669 
1670 	while (mlp != NULL) {
1671 		if (mlp->ctlr_type->ctype_ctype == dkinfo.dki_ctype) {
1672 			break;
1673 		}
1674 		mlp = mlp->next;
1675 	}
1676 
1677 	if (mlp == NULL) {
1678 		if (dkinfo.dki_ctype == DKC_CDROM) {
1679 			if (ioctl(search_file, DKIOCGMEDIAINFO,
1680 			    &mediainfo) < 0) {
1681 				mediainfo.dki_media_type = DK_UNKNOWN;
1682 			}
1683 		}
1684 		/*
1685 		 * Skip CDROM devices, they are read only.
1686 		 * But not devices like Iomega Rev Drive which
1687 		 * identifies itself as a CDROM, but has a removable
1688 		 * disk.
1689 		 */
1690 		if ((dkinfo.dki_ctype == DKC_CDROM) &&
1691 		    (mediainfo.dki_media_type != DK_REMOVABLE_DISK)) {
1692 			(void) close(search_file);
1693 			return;
1694 		}
1695 		/*
1696 		 * create the new ctlr_type structure and fill it in.
1697 		 */
1698 		tctlr = zalloc(sizeof (struct ctlr_type));
1699 		tctlr->ctype_ctype = dkinfo.dki_ctype;
1700 		tctlr->ctype_name = zalloc(DK_DEVLEN);
1701 		if (strlcpy(tctlr->ctype_name, dkinfo.dki_cname,
1702 		    DK_DEVLEN) > DK_DEVLEN) {
1703 			/*
1704 			 * DKIOCINFO returned a controller name longer
1705 			 * than DK_DEVLEN bytes, which means more of the
1706 			 * dk_cinfo structure may be corrupt.  We don't
1707 			 * allow the user to perform any operations on
1708 			 * the device in this case
1709 			 */
1710 			err_print("\nError: Device %s: controller "
1711 			    "name (%s)\nis invalid.  Device will not "
1712 			    "be displayed.\n", devname, dkinfo.dki_cname);
1713 			(void) close(search_file);
1714 			destroy_data(tctlr->ctype_name);
1715 			destroy_data((char *)tctlr);
1716 			return;
1717 		} else {
1718 			tctlr->ctype_ops = zalloc(sizeof (struct ctlr_ops));
1719 
1720 			/*
1721 			 * copy the generic disk ops structure into local copy.
1722 			 */
1723 			*(tctlr->ctype_ops) = genericops;
1724 
1725 			tctlr->ctype_flags = CF_WLIST;
1726 
1727 			mlp = controlp;
1728 
1729 			while (mlp->next != NULL) {
1730 				mlp = mlp->next;
1731 			}
1732 
1733 			mlp->next = zalloc(sizeof (struct mctlr_list));
1734 			mlp->next->ctlr_type = tctlr;
1735 		}
1736 	}
1737 
1738 	/*
1739 	 * Search through all disks known at this time, to
1740 	 * determine if we're already identified this disk.
1741 	 * If so, then there's no need to include it a
1742 	 * second time.  This permits the user-defined names
1743 	 * to supercede the standard conventional names.
1744 	 */
1745 	if (disk_is_known(&dkinfo)) {
1746 		(void) close(search_file);
1747 		return;
1748 	}
1749 #if defined(sparc)
1750 	/*
1751 	 * Because opening id with FNDELAY always succeeds,
1752 	 * read the label early on to see whether the device
1753 	 * really exists.  A result of DSK_RESERVED
1754 	 * means the disk may be reserved.
1755 	 * In the future, it will be good
1756 	 * to move these into controller specific files and have a common
1757 	 * generic check for reserved disks here, including intel disks.
1758 	 */
1759 	if (dkinfo.dki_ctype == DKC_SCSI_CCS) {
1760 		char	*first_sector;
1761 
1762 		first_sector = zalloc(cur_blksz);
1763 		i = scsi_rdwr(DIR_READ, search_file, (diskaddr_t)0,
1764 		    1, first_sector, F_SILENT, NULL);
1765 		switch (i) {
1766 		case DSK_RESERVED:
1767 			access_flags |= DSK_RESERVED;
1768 			break;
1769 		case DSK_UNAVAILABLE:
1770 			access_flags |= DSK_UNAVAILABLE;
1771 			break;
1772 		default:
1773 			break;
1774 		}
1775 		free(first_sector);
1776 	}
1777 #endif /* defined(sparc) */
1778 
1779 	/*
1780 	 * The disk appears to be present.  Allocate space for the
1781 	 * disk structure and add it to the list of found disks.
1782 	 */
1783 	search_disk = (struct disk_info *)zalloc(sizeof (struct disk_info));
1784 	if (disk_list == NULL)
1785 		disk_list = search_disk;
1786 	else {
1787 		for (dptr = disk_list; dptr->disk_next != NULL;
1788 		    dptr = dptr->disk_next)
1789 			;
1790 		dptr->disk_next = search_disk;
1791 	}
1792 	/*
1793 	 * Fill in some info from the ioctls.
1794 	 */
1795 	search_disk->disk_dkinfo = dkinfo;
1796 	if (is_efi_type(search_file)) {
1797 		search_disk->label_type = L_TYPE_EFI;
1798 	} else {
1799 		search_disk->label_type = L_TYPE_SOLARIS;
1800 	}
1801 	/*
1802 	 * Remember the names of the disk
1803 	 */
1804 	search_disk->disk_name = alloc_string(devname);
1805 	search_disk->disk_path = alloc_string(devpath);
1806 
1807 	/*
1808 	 * Remember the lba size of the disk
1809 	 */
1810 	search_disk->disk_lbasize = cur_blksz;
1811 
1812 	(void) strcpy(x86_devname, devname);
1813 
1814 	/*
1815 	 * Determine if this device is linked to a physical name.
1816 	 */
1817 	search_disk->devfs_name = get_physical_name(devpath);
1818 
1819 	/*
1820 	 * Try to match the ctlr for this disk with a ctlr we
1821 	 * have already found.  A match is assumed if the ctlrs
1822 	 * are at the same address && ctypes agree
1823 	 */
1824 	for (search_ctlr = ctlr_list; search_ctlr != NULL;
1825 	    search_ctlr = search_ctlr->ctlr_next)
1826 		if (search_ctlr->ctlr_addr == dkinfo.dki_addr &&
1827 		    search_ctlr->ctlr_space == dkinfo.dki_space &&
1828 		    search_ctlr->ctlr_ctype->ctype_ctype ==
1829 		    dkinfo.dki_ctype)
1830 			break;
1831 	/*
1832 	 * If no match was found, we need to identify this ctlr.
1833 	 */
1834 	if (search_ctlr == NULL) {
1835 		/*
1836 		 * Match the type of the ctlr to a known type.
1837 		 */
1838 		mlp = controlp;
1839 
1840 		while (mlp != NULL) {
1841 			if (mlp->ctlr_type->ctype_ctype == dkinfo.dki_ctype)
1842 				break;
1843 			mlp = mlp->next;
1844 		}
1845 		/*
1846 		 * If no match was found, it's an error.
1847 		 * Close the disk and report the error.
1848 		 */
1849 		if (mlp == NULL) {
1850 			err_print("\nError: found disk attached to ");
1851 			err_print("unsupported controller type '%d'.\n",
1852 			    dkinfo.dki_ctype);
1853 			(void) close(search_file);
1854 			return;
1855 		}
1856 		/*
1857 		 * Allocate space for the ctlr structure and add it
1858 		 * to the list of found ctlrs.
1859 		 */
1860 		search_ctlr = (struct ctlr_info *)
1861 		    zalloc(sizeof (struct ctlr_info));
1862 		search_ctlr->ctlr_ctype = mlp->ctlr_type;
1863 		if (ctlr_list == NULL)
1864 			ctlr_list = search_ctlr;
1865 		else {
1866 			for (cptr = ctlr_list; cptr->ctlr_next != NULL;
1867 			    cptr = cptr->ctlr_next)
1868 				;
1869 			cptr->ctlr_next = search_ctlr;
1870 		}
1871 		/*
1872 		 * Fill in info from the ioctl.
1873 		 */
1874 		for (i = 0; i < DK_DEVLEN; i++) {
1875 			search_ctlr->ctlr_cname[i] = dkinfo.dki_cname[i];
1876 			search_ctlr->ctlr_dname[i] = dkinfo.dki_dname[i];
1877 		}
1878 		/*
1879 		 * Make sure these can be used as simple strings
1880 		 */
1881 		search_ctlr->ctlr_cname[i] = 0;
1882 		search_ctlr->ctlr_dname[i] = 0;
1883 
1884 		search_ctlr->ctlr_flags = dkinfo.dki_flags;
1885 		search_ctlr->ctlr_num = dkinfo.dki_cnum;
1886 		search_ctlr->ctlr_addr = dkinfo.dki_addr;
1887 		search_ctlr->ctlr_space = dkinfo.dki_space;
1888 		search_ctlr->ctlr_prio = dkinfo.dki_prio;
1889 		search_ctlr->ctlr_vec = dkinfo.dki_vec;
1890 	}
1891 	/*
1892 	 * By this point, we have a known ctlr.  Link the disk
1893 	 * to the ctlr.
1894 	 */
1895 	search_disk->disk_ctlr = search_ctlr;
1896 	if (access_flags & (DSK_RESERVED | DSK_UNAVAILABLE)) {
1897 		if (access_flags & DSK_RESERVED)
1898 			search_disk->disk_flags |= DSK_RESERVED;
1899 		else
1900 			search_disk->disk_flags |= DSK_UNAVAILABLE;
1901 		(void) close(search_file);
1902 		return;
1903 	} else {
1904 		search_disk->disk_flags &= ~(DSK_RESERVED | DSK_UNAVAILABLE);
1905 	}
1906 
1907 	/*
1908 	 * Attempt to read the primary label.
1909 	 * (Note that this is really through the DKIOCGVTOC
1910 	 * ioctl, then converted from vtoc to label.)
1911 	 */
1912 	if (search_disk->label_type == L_TYPE_SOLARIS) {
1913 		status = read_label(search_file, &search_label);
1914 	} else {
1915 		status = read_efi_label(search_file, &efi_info, search_disk);
1916 	}
1917 	/*
1918 	 * If reading the label failed, and this is a SCSI
1919 	 * disk, we can attempt to auto-sense the disk
1920 	 * Configuration.
1921 	 */
1922 	ctlr = search_ctlr->ctlr_ctype;
1923 	if ((status == -1) && (ctlr->ctype_ctype == DKC_SCSI_CCS)) {
1924 		if (option_msg && diag_msg) {
1925 			err_print("%s: attempting auto configuration\n",
1926 			    search_disk->disk_name);
1927 		}
1928 
1929 		switch (search_disk->label_type) {
1930 		case (L_TYPE_SOLARIS):
1931 			if (auto_sense(search_file, 0, &search_label) != NULL) {
1932 			/*
1933 			 * Auto config worked, so we now have
1934 			 * a valid label for the disk.  Mark
1935 			 * the disk as needing the label flushed.
1936 			 */
1937 				status = 0;
1938 				search_disk->disk_flags |=
1939 				    (DSK_LABEL_DIRTY | DSK_AUTO_CONFIG);
1940 			}
1941 			break;
1942 		case (L_TYPE_EFI):
1943 			efi_disk = auto_efi_sense(search_file, &efi_info);
1944 			if (efi_disk != NULL) {
1945 				/*
1946 				 * Auto config worked, so we now have
1947 				 * a valid label for the disk.
1948 				 */
1949 				status = 0;
1950 				search_disk->disk_flags |=
1951 				    (DSK_LABEL_DIRTY | DSK_AUTO_CONFIG);
1952 			}
1953 			break;
1954 		default:
1955 			/* Should never happen */
1956 			break;
1957 		}
1958 	}
1959 
1960 	/*
1961 	 * If we didn't successfully read the label, or the label
1962 	 * appears corrupt, just leave the disk as an unknown type.
1963 	 */
1964 	if (status == -1) {
1965 		(void) close(search_file);
1966 		return;
1967 	}
1968 
1969 	if (search_disk->label_type == L_TYPE_SOLARIS) {
1970 		if (!checklabel(&search_label)) {
1971 			(void) close(search_file);
1972 			return;
1973 		}
1974 		if (trim_id(search_label.dkl_asciilabel)) {
1975 			(void) close(search_file);
1976 			return;
1977 		}
1978 	}
1979 	/*
1980 	 * The label looks ok.  Mark the disk as labeled.
1981 	 */
1982 	search_disk->disk_flags |= DSK_LABEL;
1983 
1984 	if (search_disk->label_type == L_TYPE_EFI) {
1985 		search_dtype = (struct disk_type *)
1986 		    zalloc(sizeof (struct disk_type));
1987 		type = search_ctlr->ctlr_ctype->ctype_dlist;
1988 		if (type == NULL) {
1989 			search_ctlr->ctlr_ctype->ctype_dlist =
1990 			    search_dtype;
1991 		} else {
1992 			while (type->dtype_next != NULL) {
1993 				type = type->dtype_next;
1994 			}
1995 			type->dtype_next = search_dtype;
1996 		}
1997 		search_dtype->dtype_next = NULL;
1998 
1999 		search_dtype->vendor = strdup(efi_info.vendor);
2000 		search_dtype->product = strdup(efi_info.product);
2001 		search_dtype->revision = strdup(efi_info.revision);
2002 
2003 		if (search_dtype->vendor == NULL ||
2004 		    search_dtype->product == NULL ||
2005 		    search_dtype->revision == NULL) {
2006 			free(search_dtype->vendor);
2007 			free(search_dtype->product);
2008 			free(search_dtype->revision);
2009 			free(search_dtype);
2010 			goto out;
2011 		}
2012 
2013 		search_dtype->capacity = efi_info.capacity;
2014 		search_disk->disk_type = search_dtype;
2015 
2016 		search_parts = (struct partition_info *)
2017 		    zalloc(sizeof (struct partition_info));
2018 		search_dtype->dtype_plist = search_parts;
2019 
2020 		search_parts->pinfo_name = alloc_string("original");
2021 		search_parts->pinfo_next = NULL;
2022 		search_parts->etoc = efi_info.e_parts;
2023 		search_disk->disk_parts = search_parts;
2024 
2025 		/*
2026 		 * Copy the volume name, if present
2027 		 */
2028 		for (i = 0; i < search_parts->etoc->efi_nparts; i++) {
2029 			if (search_parts->etoc->efi_parts[i].p_tag ==
2030 			    V_RESERVED) {
2031 				if (search_parts->etoc->efi_parts[i].p_name) {
2032 					bcopy(search_parts->etoc->efi_parts[i]
2033 					    .p_name, search_disk->v_volume,
2034 					    LEN_DKL_VVOL);
2035 				} else {
2036 					bzero(search_disk->v_volume,
2037 					    LEN_DKL_VVOL);
2038 				}
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
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
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
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
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
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
2409 search_duplicate_dtypes()
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
2442 search_duplicate_pinfo()
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
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
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
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
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 *
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 *
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
2856 sort_disk_list()
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
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
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
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
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