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