xref: /illumos-gate/usr/src/cmd/format/auto_sense.c (revision 2850d85b7b93f31e578520dc3b3feb24db609c62)
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 2008 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 functions to implement automatic configuration
30  * of scsi disks.
31  */
32 #include "global.h"
33 
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <stdlib.h>
39 #include <ctype.h>
40 
41 #include "misc.h"
42 #include "param.h"
43 #include "ctlr_scsi.h"
44 #include "auto_sense.h"
45 #include "partition.h"
46 #include "label.h"
47 #include "startup.h"
48 #include "analyze.h"
49 #include "io.h"
50 #include "hardware_structs.h"
51 #include "menu_fdisk.h"
52 
53 
54 #define	DISK_NAME_MAX		256
55 
56 extern	int			nctypes;
57 extern	struct	ctlr_type	ctlr_types[];
58 
59 
60 /*
61  * Marker for free hog partition
62  */
63 #define	HOG		(-1)
64 
65 
66 
67 /*
68  * Default partition tables
69  *
70  *	Disk capacity		root	swap	usr
71  *	-------------		----	----	---
72  *	0mb to 64mb		0	0	remainder
73  *	64mb to 180mb		16mb	16mb	remainder
74  *	180mb to 280mb		16mb	32mb	remainder
75  *	280mb to 380mb		24mb	32mb	remainder
76  *	380mb to 600mb		32mb	32mb	remainder
77  *	600mb to 1gb		32mb	64mb	remainder
78  *	1gb to 2gb		64mb	128mb	remainder
79  *	2gb on up		128mb	128mb	remainder
80  */
81 struct part_table {
82 	int	partitions[NDKMAP];
83 };
84 
85 static struct part_table part_table_64mb = {
86 	{ 0,	0,	0,	0,	0,	0,	HOG,	0}
87 };
88 
89 static struct part_table part_table_180mb = {
90 	{ 16,	16,	0,	0,	0,	0,	HOG,	0}
91 };
92 
93 static struct part_table part_table_280mb = {
94 	{ 16,	32,	0,	0,	0,	0,	HOG,	0}
95 };
96 
97 static struct part_table part_table_380mb = {
98 	{ 24,	32,	0,	0,	0,	0,	HOG,	0}
99 };
100 
101 static struct part_table part_table_600mb = {
102 	{ 32,	32,	0,	0,	0,	0,	HOG,	0}
103 };
104 
105 static struct part_table part_table_1gb = {
106 	{ 32,	64,	0,	0,	0,	0,	HOG,	0}
107 };
108 
109 static struct part_table part_table_2gb = {
110 	{ 64,	128,	0,	0,	0,	0,	HOG,	0}
111 };
112 
113 static struct part_table part_table_infinity = {
114 	{ 128,	128,	0,	0,	0,	0,	HOG,	0}
115 };
116 
117 
118 static struct default_partitions {
119 	long			min_capacity;
120 	long			max_capacity;
121 	struct part_table	*part_table;
122 } default_partitions[] = {
123 	{ 0,	64,		&part_table_64mb },	/* 0 to 64 mb */
124 	{ 64,	180,		&part_table_180mb },	/* 64 to 180 mb */
125 	{ 180,	280,		&part_table_280mb },	/* 180 to 280 mb */
126 	{ 280,	380,		&part_table_380mb },	/* 280 to 380 mb */
127 	{ 380,	600,		&part_table_600mb },	/* 380 to 600 mb */
128 	{ 600,	1024,		&part_table_1gb },	/* 600 to 1 gb */
129 	{ 1024,	2048,		&part_table_2gb },	/* 1 to 2 gb */
130 	{ 2048,	INFINITY,	&part_table_infinity },	/* 2 gb on up */
131 };
132 
133 #define	DEFAULT_PARTITION_TABLE_SIZE	\
134 	(sizeof (default_partitions) / sizeof (struct default_partitions))
135 
136 /*
137  * msgs for check()
138  */
139 #define	FORMAT_MSG	"Auto configuration via format.dat"
140 #define	GENERIC_MSG	"Auto configuration via generic SCSI-2"
141 
142 /*
143  * Disks on symbios(Hardwire raid controller) return a fixed number
144  * of heads(64)/cylinders(64) and adjust the cylinders depending
145  * capacity of the configured lun.
146  * In such a case we get number of physical cylinders < 3 which
147  * is the minimum required by solaris(2 reserved + 1 data cylinders).
148  * Hence try to adjust the cylinders by reducing the "nsect/nhead".
149  *
150  */
151 /*
152  * assuming a minimum of 32 block cylinders.
153  */
154 #define	MINIMUM_NO_HEADS	2
155 #define	MINIMUM_NO_SECTORS	16
156 
157 #define	MINIMUM_NO_CYLINDERS	128
158 
159 #if defined(_SUNOS_VTOC_8)
160 
161 /* These are 16-bit fields */
162 #define	MAXIMUM_NO_HEADS	65535
163 #define	MAXIMUM_NO_SECTORS	65535
164 #define	MAXIMUM_NO_CYLINDERS	65535
165 
166 #endif	/* defined(_SUNOS_VTOC_8) */
167 
168 /*
169  * minimum number of cylinders required by Solaris.
170  */
171 #define	SUN_MIN_CYL		3
172 
173 
174 
175 /*
176  * ANSI prototypes for local static functions
177  */
178 static struct disk_type	*generic_disk_sense(
179 				int		fd,
180 				int		can_prompt,
181 				struct dk_label	*label,
182 				struct scsi_inquiry *inquiry,
183 				struct scsi_capacity_16 *capacity,
184 				char		*disk_name);
185 static int		use_existing_disk_type(
186 				int		fd,
187 				int		can_prompt,
188 				struct dk_label	*label,
189 				struct scsi_inquiry *inquiry,
190 				struct disk_type *disk_type,
191 				struct scsi_capacity_16 *capacity);
192 int			build_default_partition(struct dk_label *label,
193 				int ctrl_type);
194 static struct disk_type	*find_scsi_disk_type(
195 				char		*disk_name,
196 				struct dk_label	*label);
197 static struct disk_type	*find_scsi_disk_by_name(
198 				char		*disk_name);
199 static struct ctlr_type	*find_scsi_ctlr_type(void);
200 static struct ctlr_info	*find_scsi_ctlr_info(
201 				struct dk_cinfo	*dkinfo);
202 static struct disk_type	*new_scsi_disk_type(
203 				int		fd,
204 				char		*disk_name,
205 				struct dk_label	*label);
206 static struct disk_info	*find_scsi_disk_info(
207 				struct dk_cinfo	*dkinfo);
208 
209 static struct disk_type *new_direct_disk_type(int fd, char *disk_name,
210     struct dk_label *label);
211 
212 static struct disk_info *find_direct_disk_info(struct dk_cinfo *dkinfo);
213 static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
214 static int auto_label_init(struct dk_label *label);
215 static struct ctlr_type *find_direct_ctlr_type(void);
216 static struct ctlr_info *find_direct_ctlr_info(struct dk_cinfo	*dkinfo);
217 static  struct disk_info *find_direct_disk_info(struct dk_cinfo *dkinfo);
218 
219 static char		*get_sun_disk_name(
220 				char		*disk_name,
221 				struct scsi_inquiry *inquiry);
222 static char		*get_generic_disk_name(
223 				char		*disk_name,
224 				struct scsi_inquiry *inquiry);
225 static int		force_blocksize(int fd);
226 static int		raw_format(int fd);
227 static char		*strcopy(
228 				char	*dst,
229 				char	*src,
230 				int	n);
231 static	int		adjust_disk_geometry(int capacity, int *cyl,
232 						int *nsect, int *nhead);
233 #if defined(_SUNOS_VTOC_8)
234 static int square_box(
235 			int capacity,
236 			int *dim1, int lim1,
237 			int *dim2, int lim2,
238 			int *dim3, int lim3);
239 #endif	/* defined(_SUNOS_VTOC_8) */
240 
241 
242 /*
243  * We need to get information necessary to construct a *new* efi
244  * label type
245  */
246 struct disk_type *
247 auto_efi_sense(int fd, struct efi_info *label)
248 {
249 
250 	struct dk_gpt	*vtoc;
251 	int		i;
252 
253 	struct disk_type *disk, *dp;
254 	struct disk_info *disk_info;
255 	struct ctlr_info *ctlr;
256 	struct dk_cinfo dkinfo;
257 	struct partition_info *part;
258 
259 	/*
260 	 * get vendor, product, revision and capacity info.
261 	 */
262 	if (get_disk_info(fd, label) == -1) {
263 		return ((struct disk_type *)NULL);
264 	}
265 	/*
266 	 * Now build the default partition table
267 	 */
268 	if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
269 		err_print("efi_alloc_and_init failed. \n");
270 		return ((struct disk_type *)NULL);
271 	}
272 
273 	label->e_parts = vtoc;
274 
275 	/*
276 	 * Create a whole hog EFI partition table:
277 	 * S0 takes the whole disk except the primary EFI label,
278 	 * backup EFI label, and the reserved partition.
279 	 */
280 	vtoc->efi_parts[0].p_tag = V_USR;
281 	vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba;
282 	vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba - vtoc->efi_first_u_lba
283 	    - EFI_MIN_RESV_SIZE + 1;
284 
285 	/*
286 	 * S1-S6 are unassigned slices.
287 	 */
288 	for (i = 1; i < vtoc->efi_nparts - 2; i ++) {
289 		vtoc->efi_parts[i].p_tag = V_UNASSIGNED;
290 		vtoc->efi_parts[i].p_start = 0;
291 		vtoc->efi_parts[i].p_size = 0;
292 	}
293 
294 	/*
295 	 * The reserved slice
296 	 */
297 	vtoc->efi_parts[vtoc->efi_nparts - 1].p_tag = V_RESERVED;
298 	vtoc->efi_parts[vtoc->efi_nparts - 1].p_start =
299 	    vtoc->efi_last_u_lba - EFI_MIN_RESV_SIZE + 1;
300 	vtoc->efi_parts[vtoc->efi_nparts - 1].p_size = EFI_MIN_RESV_SIZE;
301 
302 	/*
303 	 * Now stick all of it into the disk_type struct
304 	 */
305 
306 	if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
307 	    if (option_msg && diag_msg) {
308 		err_print("DKIOCINFO failed\n");
309 	    }
310 	    return (NULL);
311 	}
312 	if ((cur_ctype != NULL) && (cur_ctype->ctype_ctype == DKC_DIRECT)) {
313 		ctlr = find_direct_ctlr_info(&dkinfo);
314 		disk_info = find_direct_disk_info(&dkinfo);
315 	} else {
316 		ctlr = find_scsi_ctlr_info(&dkinfo);
317 		disk_info = find_scsi_disk_info(&dkinfo);
318 	}
319 	disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
320 	assert(disk_info->disk_ctlr == ctlr);
321 	dp = ctlr->ctlr_ctype->ctype_dlist;
322 	if (dp == NULL) {
323 		ctlr->ctlr_ctype->ctype_dlist = dp;
324 	} else {
325 		while (dp->dtype_next != NULL) {
326 			dp = dp->dtype_next;
327 		}
328 		dp->dtype_next = disk;
329 	}
330 	disk->dtype_next = NULL;
331 
332 	(void) strlcpy(disk->vendor, label->vendor,
333 		    sizeof (disk->vendor));
334 	(void) strlcpy(disk->product, label->product,
335 		    sizeof (disk->product));
336 	(void) strlcpy(disk->revision, label->revision,
337 		    sizeof (disk->revision));
338 	disk->capacity = label->capacity;
339 
340 	part = (struct partition_info *)
341 	    zalloc(sizeof (struct partition_info));
342 	disk->dtype_plist = part;
343 
344 	part->pinfo_name = alloc_string("default");
345 	part->pinfo_next = NULL;
346 	part->etoc = vtoc;
347 
348 	bzero(disk_info->v_volume, LEN_DKL_VVOL);
349 	disk_info->disk_parts = part;
350 	return (disk);
351 }
352 
353 static int
354 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
355 {
356 	void *data = dk_ioc->dki_data;
357 	int error;
358 
359 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
360 	error = ioctl(fd, cmd, (void *)dk_ioc);
361 	dk_ioc->dki_data = data;
362 
363 	return (error);
364 }
365 
366 static struct ctlr_type *
367 find_direct_ctlr_type()
368 {
369 	struct	mctlr_list	*mlp;
370 
371 	mlp = controlp;
372 
373 	while (mlp != NULL) {
374 		if (mlp->ctlr_type->ctype_ctype == DKC_DIRECT) {
375 			return (mlp->ctlr_type);
376 		}
377 		mlp = mlp->next;
378 	}
379 
380 	impossible("no DIRECT controller type");
381 
382 	return ((struct ctlr_type *)NULL);
383 }
384 
385 static struct ctlr_info *
386 find_direct_ctlr_info(
387 	struct dk_cinfo		*dkinfo)
388 {
389 	struct ctlr_info	*ctlr;
390 
391 	if (dkinfo->dki_ctype != DKC_DIRECT)
392 		return (NULL);
393 
394 	for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) {
395 		if (ctlr->ctlr_addr == dkinfo->dki_addr &&
396 		    ctlr->ctlr_space == dkinfo->dki_space &&
397 		    ctlr->ctlr_ctype->ctype_ctype == DKC_DIRECT) {
398 			return (ctlr);
399 		}
400 	}
401 
402 	impossible("no DIRECT controller info");
403 	/*NOTREACHED*/
404 }
405 
406 static  struct disk_info *
407 find_direct_disk_info(
408 	struct dk_cinfo		*dkinfo)
409 {
410 	struct disk_info	*disk;
411 	struct dk_cinfo		*dp;
412 
413 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
414 		assert(dkinfo->dki_ctype == DKC_DIRECT);
415 		dp = &disk->disk_dkinfo;
416 		if (dp->dki_ctype == dkinfo->dki_ctype &&
417 		    dp->dki_cnum == dkinfo->dki_cnum &&
418 		    dp->dki_unit == dkinfo->dki_unit &&
419 		    strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) {
420 			return (disk);
421 		}
422 	}
423 
424 	impossible("No DIRECT disk info instance\n");
425 	/*NOTREACHED*/
426 }
427 
428 /*
429  * To convert EFI to SMI labels, we need to get label geometry.
430  * Unfortunately at this time there is no good way to do so.
431  * DKIOCGGEOM will fail if disk is EFI labeled. So we hack around
432  * it and clear EFI label, do a DKIOCGGEOM and put the EFI label
433  * back on disk.
434  * This routine gets the label geometry and initializes the label
435  * It uses cur_file as opened device.
436  * returns 0 if succeeds or -1 if failed.
437  */
438 static int
439 auto_label_init(struct dk_label *label)
440 {
441 	dk_efi_t	dk_ioc;
442 	dk_efi_t	dk_ioc_back;
443 	efi_gpt_t	*data = NULL;
444 	efi_gpt_t	*databack = NULL;
445 	struct dk_geom	disk_geom;
446 	struct dk_minfo	disk_info;
447 	efi_gpt_t 	*backsigp;
448 	int		fd = cur_file;
449 	int		rval = -1;
450 	int		efisize = EFI_LABEL_SIZE * 2;
451 	int		success = 0;
452 	uint64_t	sig;
453 	uint64_t	backsig;
454 
455 	if ((data = calloc(efisize, 1)) == NULL) {
456 		err_print("auto_label_init: calloc failed\n");
457 		goto auto_label_init_out;
458 	}
459 
460 	dk_ioc.dki_data = data;
461 	dk_ioc.dki_lba = 1;
462 	dk_ioc.dki_length = efisize;
463 
464 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) != 0) {
465 		err_print("auto_label_init: GETEFI failed\n");
466 		goto auto_label_init_out;
467 	}
468 
469 	if ((databack = calloc(efisize, 1)) == NULL) {
470 		err_print("auto_label_init calloc2 failed");
471 		goto auto_label_init_out;
472 	}
473 
474 	/* get the LBA size and capacity */
475 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
476 		err_print("auto_label_init: dkiocgmediainfo failed\n");
477 		goto auto_label_init_out;
478 	}
479 
480 	if (disk_info.dki_lbsize == 0) {
481 		if (option_msg && diag_msg) {
482 			err_print("auto_lbal_init: assuming 512 byte"
483 			    "block size");
484 		}
485 		disk_info.dki_lbsize = DEV_BSIZE;
486 	}
487 
488 	if (disk_info.dki_lbsize != DEV_BSIZE) {
489 		err_print("auto_label_init: lbasize is not 512\n");
490 		goto auto_label_init_out;
491 	}
492 
493 	dk_ioc_back.dki_data = databack;
494 
495 	/*
496 	 * back up efi label goes to capacity - 1, we are reading an extra block
497 	 * before the back up label.
498 	 */
499 	dk_ioc_back.dki_lba = disk_info.dki_capacity - 1 - 1;
500 	dk_ioc_back.dki_length = efisize;
501 
502 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc_back) != 0) {
503 		err_print("auto_label_init: GETEFI backup failed\n");
504 		goto auto_label_init_out;
505 	}
506 
507 	sig = dk_ioc.dki_data->efi_gpt_Signature;
508 	dk_ioc.dki_data->efi_gpt_Signature = 0x0;
509 
510 	enter_critical();
511 
512 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
513 		err_print("auto_label_init: SETEFI failed\n");
514 		exit_critical();
515 		goto auto_label_init_out;
516 	}
517 
518 	backsigp = (efi_gpt_t *)((uintptr_t)dk_ioc_back.dki_data + DEV_BSIZE);
519 
520 	backsig = backsigp->efi_gpt_Signature;
521 
522 	backsigp->efi_gpt_Signature = 0;
523 
524 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc_back) == -1) {
525 		err_print("auto_label_init: SETEFI backup failed\n");
526 	}
527 
528 	if (ioctl(cur_file, DKIOCGGEOM, &disk_geom) != 0)
529 		err_print("auto_label_init: GGEOM failed\n");
530 	else
531 		success = 1;
532 
533 	dk_ioc.dki_data->efi_gpt_Signature = sig;
534 	backsigp->efi_gpt_Signature = backsig;
535 
536 	if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc_back) == -1) {
537 		err_print("auto_label_init: SETEFI revert backup failed\n");
538 		success = 0;
539 	}
540 
541 	if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc) == -1) {
542 		err_print("auto_label_init: SETEFI revert failed\n");
543 		success = 0;
544 	}
545 
546 	exit_critical();
547 
548 	if (success == 0)
549 		goto auto_label_init_out;
550 
551 	ncyl = disk_geom.dkg_ncyl;
552 	acyl = disk_geom.dkg_acyl;
553 	nhead =  disk_geom.dkg_nhead;
554 	nsect = disk_geom.dkg_nsect;
555 	pcyl = ncyl + acyl;
556 
557 	label->dkl_pcyl = pcyl;
558 	label->dkl_ncyl = ncyl;
559 	label->dkl_acyl = acyl;
560 	label->dkl_nhead = nhead;
561 	label->dkl_nsect = nsect;
562 	label->dkl_apc = 0;
563 	label->dkl_intrlv = 1;
564 	label->dkl_rpm = disk_geom.dkg_rpm;
565 
566 	label->dkl_magic = DKL_MAGIC;
567 
568 	(void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
569 	    "%s cyl %d alt %d hd %d sec %d",
570 	    "DEFAULT", ncyl, acyl, nhead, nsect);
571 
572 	rval = 0;
573 #if defined(_FIRMWARE_NEEDS_FDISK)
574 	(void) auto_solaris_part(label);
575 	ncyl = label->dkl_ncyl;
576 
577 #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
578 
579 	if (!build_default_partition(label, DKC_DIRECT)) {
580 		rval = -1;
581 	}
582 
583 	(void) checksum(label, CK_MAKESUM);
584 
585 
586 auto_label_init_out:
587 	if (data)
588 		free(data);
589 	if (databack)
590 		free(databack);
591 
592 	return (rval);
593 }
594 
595 static struct disk_type *
596 new_direct_disk_type(
597 	int		fd,
598 	char		*disk_name,
599 	struct dk_label	*label)
600 {
601 	struct disk_type	*dp;
602 	struct disk_type	*disk;
603 	struct ctlr_info	*ctlr;
604 	struct dk_cinfo		dkinfo;
605 	struct partition_info	*part = NULL;
606 	struct partition_info	*pt;
607 	struct disk_info	*disk_info;
608 	int			i;
609 
610 	/*
611 	 * Get the disk controller info for this disk
612 	 */
613 	if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
614 		if (option_msg && diag_msg) {
615 			err_print("DKIOCINFO failed\n");
616 		}
617 		return (NULL);
618 	}
619 
620 	/*
621 	 * Find the ctlr_info for this disk.
622 	 */
623 	ctlr = find_direct_ctlr_info(&dkinfo);
624 
625 	/*
626 	 * Allocate a new disk type for the direct controller.
627 	 */
628 	disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
629 
630 	/*
631 	 * Find the disk_info instance for this disk.
632 	 */
633 	disk_info = find_direct_disk_info(&dkinfo);
634 
635 	/*
636 	 * The controller and the disk should match.
637 	 */
638 	assert(disk_info->disk_ctlr == ctlr);
639 
640 	/*
641 	 * Link the disk into the list of disks
642 	 */
643 	dp = ctlr->ctlr_ctype->ctype_dlist;
644 	if (dp == NULL) {
645 		ctlr->ctlr_ctype->ctype_dlist = dp;
646 	} else {
647 		while (dp->dtype_next != NULL) {
648 			dp = dp->dtype_next;
649 		}
650 		dp->dtype_next = disk;
651 	}
652 	disk->dtype_next = NULL;
653 
654 	/*
655 	 * Allocate and initialize the disk name.
656 	 */
657 	disk->dtype_asciilabel = alloc_string(disk_name);
658 
659 	/*
660 	 * Initialize disk geometry info
661 	 */
662 	disk->dtype_pcyl = label->dkl_pcyl;
663 	disk->dtype_ncyl = label->dkl_ncyl;
664 	disk->dtype_acyl = label->dkl_acyl;
665 	disk->dtype_nhead = label->dkl_nhead;
666 	disk->dtype_nsect = label->dkl_nsect;
667 	disk->dtype_rpm = label->dkl_rpm;
668 
669 	part = (struct partition_info *)
670 		zalloc(sizeof (struct partition_info));
671 	pt = disk->dtype_plist;
672 	if (pt == NULL) {
673 		disk->dtype_plist = part;
674 	} else {
675 		while (pt->pinfo_next != NULL) {
676 			pt = pt->pinfo_next;
677 		}
678 		pt->pinfo_next = part;
679 	}
680 
681 	part->pinfo_next = NULL;
682 
683 	/*
684 	 * Set up the partition name
685 	 */
686 	part->pinfo_name = alloc_string("default");
687 
688 	/*
689 	 * Fill in the partition info from the label
690 	 */
691 	for (i = 0; i < NDKMAP; i++) {
692 
693 #if defined(_SUNOS_VTOC_8)
694 		part->pinfo_map[i] = label->dkl_map[i];
695 
696 #elif defined(_SUNOS_VTOC_16)
697 		part->pinfo_map[i].dkl_cylno =
698 			label->dkl_vtoc.v_part[i].p_start /
699 				((int)(disk->dtype_nhead *
700 					disk->dtype_nsect - apc));
701 		part->pinfo_map[i].dkl_nblk =
702 			label->dkl_vtoc.v_part[i].p_size;
703 #else
704 #error No VTOC format defined.
705 #endif				/* defined(_SUNOS_VTOC_8) */
706 	}
707 
708 	/*
709 	 * Use the VTOC if valid, or install a default
710 	 */
711 	if (label->dkl_vtoc.v_version == V_VERSION) {
712 		(void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume,
713 			LEN_DKL_VVOL);
714 		part->vtoc = label->dkl_vtoc;
715 	} else {
716 		(void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL);
717 		set_vtoc_defaults(part);
718 	}
719 
720 	/*
721 	 * Link the disk to the partition map
722 	 */
723 	disk_info->disk_parts = part;
724 
725 	return (disk);
726 }
727 
728 /*
729  * Get a disk type that has label info. This is used to convert
730  * EFI label to SMI label
731  */
732 struct disk_type *
733 auto_direct_get_geom_label(int fd, struct dk_label *label)
734 {
735 	struct disk_type		*disk_type;
736 
737 	if (auto_label_init(label) != 0) {
738 		err_print("auto_direct_get_geom_label: failed to get label"
739 		    "geometry");
740 		return (NULL);
741 	} else {
742 		disk_type = new_direct_disk_type(fd, "DEFAULT", label);
743 		return (disk_type);
744 	}
745 }
746 
747 /*
748  * Auto-sense a scsi disk configuration, ie get the information
749  * necessary to construct a label.  We have two different
750  * ways to auto-sense a scsi disk:
751  *	- format.dat override, via inquiry name
752  *	- generic scsi, via standard mode sense and inquiry
753  * Depending on how and when we are called, and/or
754  * change geometry and reformat.
755  */
756 struct disk_type *
757 auto_sense(
758 	int		fd,
759 	int		can_prompt,
760 	struct dk_label	*label)
761 {
762 	struct scsi_inquiry		inquiry;
763 	struct scsi_capacity_16		capacity;
764 	struct disk_type		*disk_type;
765 	char				disk_name[DISK_NAME_MAX];
766 	int				force_format_dat = 0;
767 	int				force_generic = 0;
768 	u_ioparam_t			ioparam;
769 	int				deflt;
770 
771 	/*
772 	 * First, if expert mode, find out if the user
773 	 * wants to override any of the standard methods.
774 	 */
775 	if (can_prompt && expert_mode) {
776 		deflt = 1;
777 		ioparam.io_charlist = confirm_list;
778 		if (input(FIO_MSTR, FORMAT_MSG, '?', &ioparam,
779 				&deflt, DATA_INPUT) == 0) {
780 			force_format_dat = 1;
781 		} else if (input(FIO_MSTR, GENERIC_MSG, '?', &ioparam,
782 				&deflt, DATA_INPUT) == 0) {
783 			force_generic = 1;
784 		}
785 	}
786 
787 	/*
788 	 * Get the Inquiry data.  If this fails, there's
789 	 * no hope for this disk, so give up.
790 	 */
791 	if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
792 		return ((struct disk_type *)NULL);
793 	}
794 	if (option_msg && diag_msg) {
795 		err_print("Product id: ");
796 		print_buf(inquiry.inq_pid, sizeof (inquiry.inq_pid));
797 		err_print("\n");
798 	}
799 
800 	/*
801 	 * Get the Read Capacity
802 	 */
803 	if (uscsi_read_capacity(fd, &capacity)) {
804 		return ((struct disk_type *)NULL);
805 	}
806 
807 	/*
808 	 * If the reported capacity is set to zero, then the disk
809 	 * is not usable. If the reported capacity is set to all
810 	 * 0xf's, then this disk is too large.  These could only
811 	 * happen with a device that supports LBAs larger than 64
812 	 * bits which are not defined by any current T10 standards
813 	 * or by error responding from target.
814 	 */
815 	if ((capacity.sc_capacity == 0) ||
816 	    (capacity.sc_capacity == UINT_MAX64)) {
817 		if (option_msg && diag_msg) {
818 			err_print("Invalid capacity\n");
819 		}
820 		return ((struct disk_type *)NULL);
821 	}
822 	if (option_msg && diag_msg) {
823 		err_print("blocks:  %llu (0x%llx)\n",
824 			capacity.sc_capacity, capacity.sc_capacity);
825 		err_print("blksize: %u\n", capacity.sc_lbasize);
826 	}
827 
828 	/*
829 	 * Extract the disk name for the format.dat override
830 	 */
831 	(void) get_sun_disk_name(disk_name, &inquiry);
832 	if (option_msg && diag_msg) {
833 		err_print("disk name:  `%s`\n", disk_name);
834 	}
835 
836 	/*
837 	 * Figure out which method we use for auto sense.
838 	 * If a particular method fails, we fall back to
839 	 * the next possibility.
840 	 */
841 
842 	if (force_generic) {
843 		return (generic_disk_sense(fd, can_prompt, label,
844 			&inquiry, &capacity, disk_name));
845 	}
846 
847 	/*
848 	 * Try for an existing format.dat first
849 	 */
850 	if ((disk_type = find_scsi_disk_by_name(disk_name)) != NULL) {
851 		if (use_existing_disk_type(fd, can_prompt, label,
852 				&inquiry, disk_type, &capacity)) {
853 			return (disk_type);
854 		}
855 		if (force_format_dat) {
856 			return (NULL);
857 		}
858 	}
859 
860 	/*
861 	 * Otherwise, try using generic SCSI-2 sense and inquiry.
862 	 */
863 
864 	return (generic_disk_sense(fd, can_prompt, label,
865 			&inquiry, &capacity, disk_name));
866 }
867 
868 
869 
870 /*ARGSUSED*/
871 static struct disk_type *
872 generic_disk_sense(
873 	int			fd,
874 	int			can_prompt,
875 	struct dk_label		*label,
876 	struct scsi_inquiry	*inquiry,
877 	struct scsi_capacity_16	*capacity,
878 	char			*disk_name)
879 {
880 	struct disk_type		*disk;
881 	int				i;
882 	int				setdefault = 0;
883 	int				pcyl = 0;
884 	int				ncyl = 0;
885 	int				acyl = 0;
886 	int				nhead = 0;
887 	int				nsect = 0;
888 	int				rpm = 0;
889 	long				nblocks = 0;
890 	union {
891 		struct mode_format	page3;
892 		uchar_t			buf3[MAX_MODE_SENSE_SIZE];
893 	} u_page3;
894 	union {
895 		struct mode_geometry	page4;
896 		uchar_t			buf4[MAX_MODE_SENSE_SIZE];
897 	} u_page4;
898 	struct scsi_capacity_16		new_capacity;
899 	struct mode_format		*page3 = &u_page3.page3;
900 	struct mode_geometry		*page4 = &u_page4.page4;
901 	struct scsi_ms_header		header;
902 	/* refer cmlb_convert_geometry in cmlb.c */
903 	static const struct chs_values {
904 		uint_t max_cap;		/* Max Capacity for this HS. */
905 		uint_t nhead;		/* Heads to use. */
906 		uint_t nsect;		/* SPT to use. */
907 	} CHS_values[] = {
908 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
909 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
910 		{MAXBLKS(255,  63)},		/* 502.02GB or smaller disk. */
911 		{MAXBLKS(255, 126)},		/* .98TB or smaller disk. */
912 		{INFINITY, 255, 189}		/* Max size is just under 1TB */
913 	};
914 
915 
916 	/*
917 	 * If the name of this disk appears to be "SUN", use it,
918 	 * otherwise construct a name out of the generic
919 	 * Inquiry info.  If it turns out that we already
920 	 * have a SUN disk type of this name that differs
921 	 * in geometry, we will revert to the generic name
922 	 * anyway.
923 	 */
924 	if (memcmp(disk_name, "SUN", strlen("SUN")) != 0) {
925 		(void) get_generic_disk_name(disk_name, inquiry);
926 	}
927 
928 	/*
929 	 * If the device's block size is not 512, we have to
930 	 * change block size, reformat, and then sense the
931 	 * geometry.  To do this, we must be able to prompt
932 	 * the user.
933 	 */
934 	if (capacity->sc_lbasize != DEV_BSIZE) {
935 		if (!can_prompt) {
936 			return (NULL);
937 		}
938 		if (force_blocksize(fd)) {
939 			goto err;
940 		}
941 
942 		/*
943 		 * Get the capacity again, since this has changed
944 		 */
945 		if (uscsi_read_capacity(fd, &new_capacity)) {
946 			goto err;
947 		}
948 		if (option_msg && diag_msg) {
949 			err_print("blocks:  %llu (0x%llx)\n",
950 				new_capacity.sc_capacity,
951 				    new_capacity.sc_capacity);
952 			err_print("blksize: %u\n", new_capacity.sc_lbasize);
953 		}
954 		capacity = &new_capacity;
955 		if (capacity->sc_lbasize != DEV_BSIZE) {
956 			goto err;
957 		}
958 	}
959 
960 	/*
961 	 * Get the number of blocks from Read Capacity data. Note that
962 	 * the logical block address range from 0 to capacity->sc_capacity.
963 	 */
964 	nblocks = (long)(capacity->sc_capacity + 1);
965 
966 	/*
967 	 * Get current Page 3 - Format Parameters page
968 	 */
969 	if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, MODE_SENSE_PC_CURRENT,
970 			(caddr_t)&u_page3, MAX_MODE_SENSE_SIZE, &header)) {
971 		setdefault = 1;
972 	}
973 
974 	/*
975 	 * Get current Page 4 - Drive Geometry page
976 	 */
977 	if (uscsi_mode_sense(fd, DAD_MODE_GEOMETRY, MODE_SENSE_PC_CURRENT,
978 			(caddr_t)&u_page4, MAX_MODE_SENSE_SIZE, &header)) {
979 		setdefault = 1;
980 	}
981 
982 	if (setdefault != 1) {
983 		/* The inquiry of mode page 3 & page 4 are successful */
984 		/*
985 		 * Correct for byte order if necessary
986 		 */
987 		page4->rpm = BE_16(page4->rpm);
988 		page4->step_rate = BE_16(page4->step_rate);
989 		page3->tracks_per_zone = BE_16(page3->tracks_per_zone);
990 		page3->alt_sect_zone = BE_16(page3->alt_sect_zone);
991 		page3->alt_tracks_zone = BE_16(page3->alt_tracks_zone);
992 		page3->alt_tracks_vol = BE_16(page3->alt_tracks_vol);
993 		page3->sect_track = BE_16(page3->sect_track);
994 		page3->data_bytes_sect = BE_16(page3->data_bytes_sect);
995 		page3->interleave = BE_16(page3->interleave);
996 		page3->track_skew = BE_16(page3->track_skew);
997 		page3->cylinder_skew = BE_16(page3->cylinder_skew);
998 
999 
1000 		/*
1001 		 * Construct a new label out of the sense data,
1002 		 * Inquiry and Capacity.
1003 		 */
1004 		pcyl = (page4->cyl_ub << 16) + (page4->cyl_mb << 8) + page4->cyl_lb;
1005 		nhead = page4->heads;
1006 		nsect = page3->sect_track;
1007 		rpm = page4->rpm;
1008 
1009 		/*
1010 		 * If the number of physical cylinders reported is less
1011 		 * the SUN_MIN_CYL(3) then try to adjust the geometry so that
1012 		 * we have atleast SUN_MIN_CYL cylinders.
1013 		 */
1014 		if (pcyl < SUN_MIN_CYL) {
1015 			if (nhead <= 0 || nsect <= 0) {
1016 				setdefault = 1;
1017 			} else if (adjust_disk_geometry((int)(capacity->sc_capacity + 1),
1018 			    &pcyl, &nhead, &nsect)) {
1019 				setdefault = 1;
1020 			}
1021 		}
1022 	}
1023 
1024 	if (setdefault == 1) {
1025 		/*
1026 		 * If the number of cylinders or the number of heads reported
1027 		 * is zero, we think the inquiry of page 3 and page 4 failed.
1028 		 * We will set the geometry infomation by ourselves.
1029 		 */
1030 		err_print("\nThe device does not support mode page 3 or page 4,");
1031 		err_print("\nor the reported geometry info is invalid.");
1032 		err_print("\nWARNING: Disk geometry is based on capacity data.\n\n");
1033 
1034 		/* convert capacity to nsect * nhead * pcyl */
1035 		/* Unlabeled SCSI floppy device */
1036 		if (nblocks <= 0x1000) {
1037 			nhead = 2;
1038 			pcyl = 80;
1039 			nsect = nblocks / (nhead * pcyl);
1040 		} else {
1041 			for (i = 0; CHS_values[i].max_cap < nblocks &&
1042 			    CHS_values[i].max_cap != INFINITY; i++)
1043 				;
1044 
1045 			nhead = CHS_values[i].nhead;
1046 			nsect = CHS_values[i].nsect;
1047 			pcyl = nblocks / (nhead * nsect);
1048 		}
1049 	}
1050 
1051 	/*
1052 	 * The sd driver reserves 2 cylinders the backup disk label and
1053 	 * the deviceid.  Set the number of data cylinders to pcyl-acyl.
1054 	 */
1055 	acyl = DK_ACYL;
1056 	ncyl = pcyl - acyl;
1057 
1058 	if (option_msg && diag_msg) {
1059 		err_print("Geometry:\n");
1060 		err_print("    pcyl:    %d\n", pcyl);
1061 		err_print("    ncyl:    %d\n", ncyl);
1062 		err_print("    heads:   %d\n", nhead);
1063 		err_print("    nsects:  %d\n", nsect);
1064 		err_print("    acyl:    %d\n", acyl);
1065 
1066 #if defined(_SUNOS_VTOC_16)
1067 		err_print("    bcyl:    %d\n", bcyl);
1068 #endif			/* defined(_SUNOS_VTOC_16) */
1069 
1070 		err_print("    rpm:     %d\n", rpm);
1071 	}
1072 
1073 	/*
1074 	 * Some drives do not support page4 or report 0 for page4->rpm,
1075 	 * adjust it to AVG_RPM, 3600.
1076 	 */
1077 	if (rpm < MIN_RPM || rpm > MAX_RPM) {
1078 		err_print("The current rpm value %d is invalid,"
1079 			" adjusting it to %d\n", rpm, AVG_RPM);
1080 		rpm = AVG_RPM;
1081 	}
1082 
1083 	/*
1084 	 * Some drives report 0 for nsect (page 3, byte 10 and 11) if they
1085 	 * have variable number of sectors per track. So adjust nsect.
1086 	 * Also the value is defined as vendor specific, hence check if
1087 	 * it is in a tolerable range. The values (32 and 4 below) are
1088 	 * chosen so that this change below does not generate a different
1089 	 * geometry for currently supported sun disks.
1090 	 */
1091 	if ((nsect <= 0) ||
1092 	    (pcyl * nhead * nsect) < (nblocks - nblocks/32) ||
1093 	    (pcyl * nhead * nsect) > (nblocks + nblocks/4)) {
1094 		err_print("Mode sense page(3) reports nsect value as %d, "
1095 		    "adjusting it to %ld\n", nsect, nblocks / (pcyl * nhead));
1096 		nsect = nblocks / (pcyl * nhead);
1097 	}
1098 
1099 	/*
1100 	 * Some drives report their physical geometry such that
1101 	 * it is greater than the actual capacity.  Adjust the
1102 	 * geometry to allow for this, so we don't run off
1103 	 * the end of the disk.
1104 	 */
1105 	if ((pcyl * nhead * nsect) > nblocks) {
1106 		int	p = pcyl;
1107 		if (option_msg && diag_msg) {
1108 			err_print("Computed capacity (%ld) exceeds actual "
1109 				"disk capacity (%ld)\n",
1110 				(long)(pcyl * nhead * nsect), nblocks);
1111 		}
1112 		do {
1113 			pcyl--;
1114 		} while ((pcyl * nhead * nsect) > nblocks);
1115 
1116 		if (can_prompt && expert_mode && !option_f) {
1117 			/*
1118 			 * Try to adjust nsect instead of pcyl to see if we
1119 			 * can optimize. For compatability reasons do this
1120 			 * only in expert mode (refer to bug 1144812).
1121 			 */
1122 			int	n = nsect;
1123 			do {
1124 				n--;
1125 			} while ((p * nhead * n) > nblocks);
1126 			if ((p * nhead * n) > (pcyl * nhead * nsect)) {
1127 				u_ioparam_t	ioparam;
1128 				int		deflt = 1;
1129 				/*
1130 				 * Ask the user for a choice here.
1131 				 */
1132 				ioparam.io_bounds.lower = 1;
1133 				ioparam.io_bounds.upper = 2;
1134 				err_print("1. Capacity = %d, with pcyl = %d "
1135 					"nhead = %d nsect = %d\n",
1136 					(pcyl * nhead * nsect),
1137 					pcyl, nhead, nsect);
1138 				err_print("2. Capacity = %d, with pcyl = %d "
1139 					"nhead = %d nsect = %d\n",
1140 					(p * nhead * n),
1141 					p, nhead, n);
1142 				if (input(FIO_INT, "Select one of the above "
1143 				    "choices ", ':', &ioparam,
1144 					&deflt, DATA_INPUT) == 2) {
1145 					pcyl = p;
1146 					nsect = n;
1147 				}
1148 			}
1149 		}
1150 	}
1151 
1152 #if defined(_SUNOS_VTOC_8)
1153 	/*
1154 	 * Finally, we need to make sure we don't overflow any of the
1155 	 * fields in our disk label.  To do this we need to `square
1156 	 * the box' so to speak.  We will lose bits here.
1157 	 */
1158 
1159 	if ((pcyl > MAXIMUM_NO_CYLINDERS &&
1160 		((nsect > MAXIMUM_NO_SECTORS) ||
1161 		(nhead > MAXIMUM_NO_HEADS))) ||
1162 		((nsect > MAXIMUM_NO_SECTORS) &&
1163 		(nhead > MAXIMUM_NO_HEADS))) {
1164 		err_print("This disk is too big to label. "
1165 			" You will lose some blocks.\n");
1166 	}
1167 	if ((pcyl > MAXIMUM_NO_CYLINDERS) ||
1168 		(nsect > MAXIMUM_NO_SECTORS) ||
1169 		(nhead > MAXIMUM_NO_HEADS)) {
1170 		u_ioparam_t	ioparam;
1171 		int		order;
1172 		char		msg[256];
1173 
1174 		order = ((ncyl > nhead)<<2) |
1175 			((ncyl > nsect)<<1) |
1176 			(nhead > nsect);
1177 		switch (order) {
1178 		case 0x7: /* ncyl > nhead > nsect */
1179 			nblocks =
1180 				square_box(nblocks,
1181 					&pcyl, MAXIMUM_NO_CYLINDERS,
1182 					&nhead, MAXIMUM_NO_HEADS,
1183 					&nsect, MAXIMUM_NO_SECTORS);
1184 			break;
1185 		case 0x6: /* ncyl > nsect > nhead */
1186 			nblocks =
1187 				square_box(nblocks,
1188 					&pcyl, MAXIMUM_NO_CYLINDERS,
1189 					&nsect, MAXIMUM_NO_SECTORS,
1190 					&nhead, MAXIMUM_NO_HEADS);
1191 			break;
1192 		case 0x4: /* nsect > ncyl > nhead */
1193 			nblocks =
1194 				square_box(nblocks,
1195 					&nsect, MAXIMUM_NO_SECTORS,
1196 					&pcyl, MAXIMUM_NO_CYLINDERS,
1197 					&nhead, MAXIMUM_NO_HEADS);
1198 			break;
1199 		case 0x0: /* nsect > nhead > ncyl */
1200 			nblocks =
1201 				square_box(nblocks,
1202 					&nsect, MAXIMUM_NO_SECTORS,
1203 					&nhead, MAXIMUM_NO_HEADS,
1204 					&pcyl, MAXIMUM_NO_CYLINDERS);
1205 			break;
1206 		case 0x3: /* nhead > ncyl > nsect */
1207 			nblocks =
1208 				square_box(nblocks,
1209 					&nhead, MAXIMUM_NO_HEADS,
1210 					&pcyl, MAXIMUM_NO_CYLINDERS,
1211 					&nsect, MAXIMUM_NO_SECTORS);
1212 			break;
1213 		case 0x1: /* nhead > nsect > ncyl */
1214 			nblocks =
1215 				square_box(nblocks,
1216 					&nhead, MAXIMUM_NO_HEADS,
1217 					&nsect, MAXIMUM_NO_SECTORS,
1218 					&pcyl, MAXIMUM_NO_CYLINDERS);
1219 			break;
1220 		default:
1221 			/* How did we get here? */
1222 			impossible("label overflow adjustment");
1223 
1224 			/* Do something useful */
1225 			nblocks =
1226 				square_box(nblocks,
1227 					&nhead, MAXIMUM_NO_HEADS,
1228 					&nsect, MAXIMUM_NO_SECTORS,
1229 					&pcyl, MAXIMUM_NO_CYLINDERS);
1230 			break;
1231 		}
1232 		if (option_msg && diag_msg &&
1233 		    (capacity->sc_capacity + 1 != nblocks)) {
1234 			err_print("After adjusting geometry you lost"
1235 				" %llu of %lld blocks.\n",
1236 				(capacity->sc_capacity + 1 - nblocks),
1237 				capacity->sc_capacity + 1);
1238 		}
1239 		while (can_prompt && expert_mode && !option_f) {
1240 			int				deflt = 1;
1241 
1242 			/*
1243 			 * Allow user to modify this by hand if desired.
1244 			 */
1245 			(void) sprintf(msg,
1246 				"\nGeometry: %d heads, %d sectors %d "
1247 				" cylinders result in %d out of %lld blocks.\n"
1248 				"Do you want to modify the device geometry",
1249 				nhead, nsect, pcyl,
1250 				(int)nblocks, capacity->sc_capacity + 1);
1251 
1252 			ioparam.io_charlist = confirm_list;
1253 			if (input(FIO_MSTR, msg, '?', &ioparam,
1254 				&deflt, DATA_INPUT) != 0)
1255 				break;
1256 
1257 			ioparam.io_bounds.lower = MINIMUM_NO_HEADS;
1258 			ioparam.io_bounds.upper = MAXIMUM_NO_HEADS;
1259 			nhead = input(FIO_INT, "Number of heads", ':',
1260 				&ioparam, &nhead, DATA_INPUT);
1261 			ioparam.io_bounds.lower = MINIMUM_NO_SECTORS;
1262 			ioparam.io_bounds.upper = MAXIMUM_NO_SECTORS;
1263 			nsect = input(FIO_INT,
1264 				"Number of sectors per track",
1265 				':', &ioparam, &nsect, DATA_INPUT);
1266 			ioparam.io_bounds.lower = SUN_MIN_CYL;
1267 			ioparam.io_bounds.upper = MAXIMUM_NO_CYLINDERS;
1268 			pcyl = input(FIO_INT, "Number of cylinders",
1269 				':', &ioparam, &pcyl, DATA_INPUT);
1270 			nblocks = nhead * nsect * pcyl;
1271 			if (nblocks > capacity->sc_capacity + 1) {
1272 				err_print("Warning: %ld blocks exceeds "
1273 					"disk capacity of %lld blocks\n",
1274 					nblocks,
1275 					capacity->sc_capacity + 1);
1276 			}
1277 		}
1278 	}
1279 #endif		/* defined(_SUNOS_VTOC_8) */
1280 
1281 	ncyl = pcyl - acyl;
1282 
1283 	if (option_msg && diag_msg) {
1284 		err_print("\nGeometry after adjusting for capacity:\n");
1285 		err_print("    pcyl:    %d\n", pcyl);
1286 		err_print("    ncyl:    %d\n", ncyl);
1287 		err_print("    heads:   %d\n", nhead);
1288 		err_print("    nsects:  %d\n", nsect);
1289 		err_print("    acyl:    %d\n", acyl);
1290 		err_print("    rpm:     %d\n", rpm);
1291 	}
1292 
1293 	(void) memset((char *)label, 0, sizeof (struct dk_label));
1294 
1295 	label->dkl_magic = DKL_MAGIC;
1296 
1297 	(void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
1298 		"%s cyl %d alt %d hd %d sec %d",
1299 		disk_name, ncyl, acyl, nhead, nsect);
1300 
1301 	label->dkl_pcyl = pcyl;
1302 	label->dkl_ncyl = ncyl;
1303 	label->dkl_acyl = acyl;
1304 	label->dkl_nhead = nhead;
1305 	label->dkl_nsect = nsect;
1306 	label->dkl_apc = 0;
1307 	label->dkl_intrlv = 1;
1308 	label->dkl_rpm = rpm;
1309 
1310 #if defined(_FIRMWARE_NEEDS_FDISK)
1311 	(void) auto_solaris_part(label);
1312 	ncyl = label->dkl_ncyl;
1313 #endif		/* defined(_FIRMWARE_NEEDS_FDISK) */
1314 
1315 
1316 	if (!build_default_partition(label, DKC_SCSI_CCS)) {
1317 		goto err;
1318 	}
1319 
1320 	(void) checksum(label, CK_MAKESUM);
1321 
1322 	/*
1323 	 * Find an existing disk type defined for this disk.
1324 	 * For this to work, both the name and geometry must
1325 	 * match.  If there is no such type, but there already
1326 	 * is a disk defined with that name, but with a different
1327 	 * geometry, construct a new generic disk name out of
1328 	 * the inquiry information.  Whatever name we're
1329 	 * finally using, if there's no such disk type defined,
1330 	 * build a new disk definition.
1331 	 */
1332 	if ((disk = find_scsi_disk_type(disk_name, label)) == NULL) {
1333 		if (find_scsi_disk_by_name(disk_name) != NULL) {
1334 			char	old_name[DISK_NAME_MAX];
1335 			(void) strcpy(old_name, disk_name);
1336 			(void) get_generic_disk_name(disk_name,
1337 				inquiry);
1338 			if (option_msg && diag_msg) {
1339 				err_print(
1340 "Changing disk type name from '%s' to '%s'\n", old_name, disk_name);
1341 			}
1342 			(void) snprintf(label->dkl_asciilabel,
1343 				sizeof (label->dkl_asciilabel),
1344 				"%s cyl %d alt %d hd %d sec %d",
1345 				disk_name, ncyl, acyl, nhead, nsect);
1346 			(void) checksum(label, CK_MAKESUM);
1347 			disk = find_scsi_disk_type(disk_name, label);
1348 		}
1349 		if (disk == NULL) {
1350 			disk = new_scsi_disk_type(fd, disk_name, label);
1351 			if (disk == NULL)
1352 				goto err;
1353 		}
1354 	}
1355 
1356 	return (disk);
1357 
1358 err:
1359 	if (option_msg && diag_msg) {
1360 		err_print(
1361 		"Configuration via generic SCSI-2 information failed\n");
1362 	}
1363 	return (NULL);
1364 }
1365 
1366 
1367 /*ARGSUSED*/
1368 static int
1369 use_existing_disk_type(
1370 	int			fd,
1371 	int			can_prompt,
1372 	struct dk_label		*label,
1373 	struct scsi_inquiry	*inquiry,
1374 	struct disk_type	*disk_type,
1375 	struct scsi_capacity_16	*capacity)
1376 {
1377 	struct scsi_capacity_16	new_capacity;
1378 	int			pcyl;
1379 	int			acyl;
1380 	int			nhead;
1381 	int			nsect;
1382 	int			rpm;
1383 
1384 	/*
1385 	 * If the device's block size is not 512, we have to
1386 	 * change block size, reformat, and then sense the
1387 	 * geometry.  To do this, we must be able to prompt
1388 	 * the user.
1389 	 */
1390 	if (capacity->sc_lbasize != DEV_BSIZE) {
1391 		if (!can_prompt) {
1392 			return (0);
1393 		}
1394 		if (force_blocksize(fd)) {
1395 			goto err;
1396 		}
1397 
1398 		/*
1399 		 * Get the capacity again, since this has changed
1400 		 */
1401 		if (uscsi_read_capacity(fd, &new_capacity)) {
1402 			goto err;
1403 		}
1404 
1405 		if (option_msg && diag_msg) {
1406 			err_print("blocks:  %llu (0x%llx)\n",
1407 			    new_capacity.sc_capacity,
1408 			    new_capacity.sc_capacity);
1409 			err_print("blksize: %u\n", new_capacity.sc_lbasize);
1410 		}
1411 
1412 		capacity = &new_capacity;
1413 		if (capacity->sc_lbasize != DEV_BSIZE) {
1414 			goto err;
1415 		}
1416 	}
1417 
1418 	/*
1419 	 * Construct a new label out of the format.dat
1420 	 */
1421 	pcyl = disk_type->dtype_pcyl;
1422 	acyl = disk_type->dtype_acyl;
1423 	ncyl = disk_type->dtype_ncyl;
1424 	nhead = disk_type->dtype_nhead;
1425 	nsect = disk_type->dtype_nsect;
1426 	rpm = disk_type->dtype_rpm;
1427 
1428 	if (option_msg && diag_msg) {
1429 		err_print("Format.dat geometry:\n");
1430 		err_print("    pcyl:    %d\n", pcyl);
1431 		err_print("    heads:   %d\n", nhead);
1432 		err_print("    nsects:  %d\n", nsect);
1433 		err_print("    acyl:    %d\n", acyl);
1434 		err_print("    rpm:     %d\n", rpm);
1435 	}
1436 
1437 	(void) memset((char *)label, 0, sizeof (struct dk_label));
1438 
1439 	label->dkl_magic = DKL_MAGIC;
1440 
1441 	(void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
1442 		"%s cyl %d alt %d hd %d sec %d",
1443 		disk_type->dtype_asciilabel,
1444 		ncyl, acyl, nhead, nsect);
1445 
1446 	label->dkl_pcyl = pcyl;
1447 	label->dkl_ncyl = ncyl;
1448 	label->dkl_acyl = acyl;
1449 	label->dkl_nhead = nhead;
1450 	label->dkl_nsect = nsect;
1451 	label->dkl_apc = 0;
1452 	label->dkl_intrlv = 1;
1453 	label->dkl_rpm = rpm;
1454 
1455 	if (!build_default_partition(label, DKC_SCSI_CCS)) {
1456 		goto err;
1457 	}
1458 
1459 	(void) checksum(label, CK_MAKESUM);
1460 	return (1);
1461 
1462 err:
1463 	if (option_msg && diag_msg) {
1464 		err_print(
1465 			"Configuration via format.dat geometry failed\n");
1466 	}
1467 	return (0);
1468 }
1469 
1470 int
1471 build_default_partition(
1472 	struct dk_label			*label,
1473 	int				ctrl_type)
1474 {
1475 	int				i;
1476 	int				ncyls[NDKMAP];
1477 	int				nblks;
1478 	int				cyl;
1479 	struct dk_vtoc			*vtoc;
1480 	struct part_table		*pt;
1481 	struct default_partitions	*dpt;
1482 	long				capacity;
1483 	int				freecyls;
1484 	int				blks_per_cyl;
1485 	int				ncyl;
1486 
1487 #ifdef lint
1488 	ctrl_type = ctrl_type;
1489 #endif
1490 
1491 	/*
1492 	 * Install a default vtoc
1493 	 */
1494 	vtoc = &label->dkl_vtoc;
1495 	vtoc->v_version = V_VERSION;
1496 	vtoc->v_nparts = NDKMAP;
1497 	vtoc->v_sanity = VTOC_SANE;
1498 
1499 	for (i = 0; i < NDKMAP; i++) {
1500 		vtoc->v_part[i].p_tag = default_vtoc_map[i].p_tag;
1501 		vtoc->v_part[i].p_flag = default_vtoc_map[i].p_flag;
1502 	}
1503 
1504 	/*
1505 	 * Find a partition that matches this disk.  Capacity
1506 	 * is in integral number of megabytes.
1507 	 */
1508 	capacity = (long)(label->dkl_ncyl * label->dkl_nhead *
1509 		label->dkl_nsect) / (long)((1024 * 1024) / DEV_BSIZE);
1510 	dpt = default_partitions;
1511 	for (i = 0; i < DEFAULT_PARTITION_TABLE_SIZE; i++, dpt++) {
1512 		if (capacity >= dpt->min_capacity &&
1513 				capacity < dpt->max_capacity) {
1514 			break;
1515 		}
1516 	}
1517 	if (i == DEFAULT_PARTITION_TABLE_SIZE) {
1518 		if (option_msg && diag_msg) {
1519 			err_print("No matching default partition (%ld)\n",
1520 				capacity);
1521 		}
1522 		return (0);
1523 	}
1524 	pt = dpt->part_table;
1525 
1526 	/*
1527 	 * Go through default partition table, finding fixed
1528 	 * sized entries.
1529 	 */
1530 	freecyls = label->dkl_ncyl;
1531 	blks_per_cyl = label->dkl_nhead * label->dkl_nsect;
1532 	for (i = 0; i < NDKMAP; i++) {
1533 		if (pt->partitions[i] == HOG || pt->partitions[i] == 0) {
1534 			ncyls[i] = 0;
1535 		} else {
1536 			/*
1537 			 * Calculate number of cylinders necessary
1538 			 * for specified size, rounding up to
1539 			 * the next greatest integral number of
1540 			 * cylinders.  Always give what they
1541 			 * asked or more, never less.
1542 			 */
1543 			nblks = pt->partitions[i] * ((1024*1024)/DEV_BSIZE);
1544 			nblks += (blks_per_cyl - 1);
1545 			ncyls[i] = nblks / blks_per_cyl;
1546 			freecyls -= ncyls[i];
1547 		}
1548 	}
1549 
1550 	if (freecyls < 0) {
1551 		if (option_msg && diag_msg) {
1552 			for (i = 0; i < NDKMAP; i++) {
1553 				if (ncyls[i] == 0)
1554 					continue;
1555 				err_print("Partition %d: %d cyls\n",
1556 					i, ncyls[i]);
1557 			}
1558 			err_print("Free cylinders exhausted (%d)\n",
1559 				freecyls);
1560 		}
1561 		return (0);
1562 	}
1563 #if defined(i386)
1564 	/*
1565 	 * Set the default boot partition to 1 cylinder
1566 	 */
1567 	ncyls[8] = 1;
1568 	freecyls -= 1;
1569 
1570 	/*
1571 	 * If current disk type is not a SCSI disk,
1572 	 * set the default alternates partition to 2 cylinders
1573 	 */
1574 	if (ctrl_type != DKC_SCSI_CCS) {
1575 		ncyls[9] = 2;
1576 		freecyls -= 2;
1577 	}
1578 #endif			/* defined(i386) */
1579 
1580 	/*
1581 	 * Set the free hog partition to whatever space remains.
1582 	 * It's an error to have more than one HOG partition,
1583 	 * but we don't verify that here.
1584 	 */
1585 	for (i = 0; i < NDKMAP; i++) {
1586 		if (pt->partitions[i] == HOG) {
1587 			assert(ncyls[i] == 0);
1588 			ncyls[i] = freecyls;
1589 			break;
1590 		}
1591 	}
1592 
1593 	/*
1594 	 * Error checking
1595 	 */
1596 	ncyl = 0;
1597 	for (i = 0; i < NDKMAP; i++) {
1598 		ncyl += ncyls[i];
1599 	}
1600 	assert(ncyl == (label->dkl_ncyl));
1601 
1602 	/*
1603 	 * Finally, install the partition in the label.
1604 	 */
1605 	cyl = 0;
1606 
1607 #if defined(_SUNOS_VTOC_16)
1608 	for (i = NDKMAP/2; i < NDKMAP; i++) {
1609 		if (i == 2 || ncyls[i] == 0)
1610 			continue;
1611 		label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
1612 		label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
1613 		cyl += ncyls[i];
1614 	}
1615 	for (i = 0; i < NDKMAP/2; i++) {
1616 
1617 #elif defined(_SUNOS_VTOC_8)
1618 	for (i = 0; i < NDKMAP; i++) {
1619 
1620 #else
1621 #error No VTOC format defined.
1622 #endif				/* defined(_SUNOS_VTOC_16) */
1623 
1624 		if (i == 2 || ncyls[i] == 0) {
1625 #if defined(_SUNOS_VTOC_8)
1626 			if (i != 2) {
1627 				label->dkl_map[i].dkl_cylno = 0;
1628 				label->dkl_map[i].dkl_nblk = 0;
1629 			}
1630 #endif
1631 			continue;
1632 		}
1633 #if defined(_SUNOS_VTOC_8)
1634 		label->dkl_map[i].dkl_cylno = cyl;
1635 		label->dkl_map[i].dkl_nblk = ncyls[i] * blks_per_cyl;
1636 #elif defined(_SUNOS_VTOC_16)
1637 		label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
1638 		label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
1639 
1640 #else
1641 #error No VTOC format defined.
1642 #endif				/* defined(_SUNOS_VTOC_8) */
1643 
1644 		cyl += ncyls[i];
1645 	}
1646 
1647 	/*
1648 	 * Set the whole disk partition
1649 	 */
1650 #if defined(_SUNOS_VTOC_8)
1651 	label->dkl_map[2].dkl_cylno = 0;
1652 	label->dkl_map[2].dkl_nblk =
1653 		label->dkl_ncyl * label->dkl_nhead * label->dkl_nsect;
1654 
1655 #elif defined(_SUNOS_VTOC_16)
1656 	label->dkl_vtoc.v_part[2].p_start = 0;
1657 	label->dkl_vtoc.v_part[2].p_size =
1658 		(label->dkl_ncyl + label->dkl_acyl) * label->dkl_nhead *
1659 			label->dkl_nsect;
1660 #else
1661 #error No VTOC format defined.
1662 #endif				/* defined(_SUNOS_VTOC_8) */
1663 
1664 
1665 	if (option_msg && diag_msg) {
1666 		float	scaled;
1667 		err_print("\n");
1668 		for (i = 0; i < NDKMAP; i++) {
1669 #if defined(_SUNOS_VTOC_8)
1670 			if (label->dkl_map[i].dkl_nblk == 0)
1671 
1672 #elif defined(_SUNOS_VTOC_16)
1673 			if (label->dkl_vtoc.v_part[i].p_size == 0)
1674 
1675 #else
1676 #error No VTOC format defined.
1677 #endif				/* defined(_SUNOS_VTOC_8) */
1678 
1679 				continue;
1680 			err_print("Partition %d:   ", i);
1681 #if defined(_SUNOS_VTOC_8)
1682 			scaled = bn2mb(label->dkl_map[i].dkl_nblk);
1683 
1684 #elif defined(_SUNOS_VTOC_16)
1685 
1686 			scaled = bn2mb(label->dkl_vtoc.v_part[i].p_size);
1687 #else
1688 #error No VTOC format defined.
1689 #endif				/* defined(_SUNOS_VTOC_8) */
1690 
1691 			if (scaled > 1024.0) {
1692 				err_print("%6.2fGB  ", scaled/1024.0);
1693 			} else {
1694 				err_print("%6.2fMB  ", scaled);
1695 			}
1696 			err_print(" %6d cylinders\n",
1697 #if defined(_SUNOS_VTOC_8)
1698 			    label->dkl_map[i].dkl_nblk/blks_per_cyl);
1699 
1700 #elif defined(_SUNOS_VTOC_16)
1701 			    label->dkl_vtoc.v_part[i].p_size/blks_per_cyl);
1702 
1703 #else
1704 #error No VTOC format defined.
1705 #endif				/* defined(_SUNOS_VTOC_8) */
1706 
1707 		}
1708 		err_print("\n");
1709 	}
1710 
1711 	return (1);
1712 }
1713 
1714 
1715 
1716 /*
1717  * Find an existing scsi disk definition by this name,
1718  * if possible.
1719  */
1720 static struct disk_type *
1721 find_scsi_disk_type(
1722 	char			*disk_name,
1723 	struct dk_label		*label)
1724 {
1725 	struct ctlr_type	*ctlr;
1726 	struct disk_type	*dp;
1727 
1728 	ctlr = find_scsi_ctlr_type();
1729 	for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
1730 	    if (dp->dtype_asciilabel) {
1731 		if ((strcmp(dp->dtype_asciilabel, disk_name) == 0) &&
1732 				dp->dtype_pcyl == label->dkl_pcyl &&
1733 				dp->dtype_ncyl == label->dkl_ncyl &&
1734 				dp->dtype_acyl == label->dkl_acyl &&
1735 				dp->dtype_nhead == label->dkl_nhead &&
1736 				dp->dtype_nsect == label->dkl_nsect) {
1737 			return (dp);
1738 		}
1739 	    }
1740 	}
1741 
1742 	return ((struct disk_type *)NULL);
1743 }
1744 
1745 
1746 /*
1747  * Find an existing scsi disk definition by this name,
1748  * if possible.
1749  */
1750 static struct disk_type *
1751 find_scsi_disk_by_name(
1752 	char			*disk_name)
1753 {
1754 	struct ctlr_type	*ctlr;
1755 	struct disk_type	*dp;
1756 
1757 	ctlr = find_scsi_ctlr_type();
1758 	for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
1759 	    if (dp->dtype_asciilabel) {
1760 		if ((strcmp(dp->dtype_asciilabel, disk_name) == 0)) {
1761 			return (dp);
1762 		}
1763 	    }
1764 	}
1765 
1766 	return ((struct disk_type *)NULL);
1767 }
1768 
1769 
1770 /*
1771  * Return a pointer to the ctlr_type structure for SCSI
1772  * disks.  This list is built into the program, so there's
1773  * no chance of not being able to find it, unless someone
1774  * totally mangles the code.
1775  */
1776 static struct ctlr_type *
1777 find_scsi_ctlr_type()
1778 {
1779 	struct	mctlr_list	*mlp;
1780 
1781 	mlp = controlp;
1782 
1783 	while (mlp != NULL) {
1784 		if (mlp->ctlr_type->ctype_ctype == DKC_SCSI_CCS) {
1785 			return (mlp->ctlr_type);
1786 		}
1787 	mlp = mlp->next;
1788 	}
1789 
1790 	impossible("no SCSI controller type");
1791 
1792 	return ((struct ctlr_type *)NULL);
1793 }
1794 
1795 
1796 
1797 /*
1798  * Return a pointer to the scsi ctlr_info structure.  This
1799  * structure is allocated the first time format sees a
1800  * disk on this controller, so it must be present.
1801  */
1802 static struct ctlr_info *
1803 find_scsi_ctlr_info(
1804 	struct dk_cinfo		*dkinfo)
1805 {
1806 	struct ctlr_info	*ctlr;
1807 
1808 	if (dkinfo->dki_ctype != DKC_SCSI_CCS) {
1809 		return (NULL);
1810 	}
1811 
1812 	for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) {
1813 		if (ctlr->ctlr_addr == dkinfo->dki_addr &&
1814 			ctlr->ctlr_space == dkinfo->dki_space &&
1815 				ctlr->ctlr_ctype->ctype_ctype ==
1816 					DKC_SCSI_CCS) {
1817 			return (ctlr);
1818 		}
1819 	}
1820 
1821 	impossible("no SCSI controller info");
1822 
1823 	return ((struct ctlr_info *)NULL);
1824 }
1825 
1826 
1827 
1828 static struct disk_type *
1829 new_scsi_disk_type(
1830 	int		fd,
1831 	char		*disk_name,
1832 	struct dk_label	*label)
1833 {
1834 	struct disk_type	*dp;
1835 	struct disk_type	*disk;
1836 	struct ctlr_info	*ctlr;
1837 	struct dk_cinfo		dkinfo;
1838 	struct partition_info	*part;
1839 	struct partition_info	*pt;
1840 	struct disk_info	*disk_info;
1841 	int			i;
1842 
1843 	/*
1844 	 * Get the disk controller info for this disk
1845 	 */
1846 	if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
1847 		if (option_msg && diag_msg) {
1848 			err_print("DKIOCINFO failed\n");
1849 		}
1850 		return (NULL);
1851 	}
1852 
1853 	/*
1854 	 * Find the ctlr_info for this disk.
1855 	 */
1856 	ctlr = find_scsi_ctlr_info(&dkinfo);
1857 
1858 	/*
1859 	 * Allocate a new disk type for the SCSI controller.
1860 	 */
1861 	disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
1862 
1863 	/*
1864 	 * Find the disk_info instance for this disk.
1865 	 */
1866 	disk_info = find_scsi_disk_info(&dkinfo);
1867 
1868 	/*
1869 	 * The controller and the disk should match.
1870 	 */
1871 	assert(disk_info->disk_ctlr == ctlr);
1872 
1873 	/*
1874 	 * Link the disk into the list of disks
1875 	 */
1876 	dp = ctlr->ctlr_ctype->ctype_dlist;
1877 	if (dp == NULL) {
1878 		ctlr->ctlr_ctype->ctype_dlist = dp;
1879 	} else {
1880 		while (dp->dtype_next != NULL) {
1881 			dp = dp->dtype_next;
1882 		}
1883 		dp->dtype_next = disk;
1884 	}
1885 	disk->dtype_next = NULL;
1886 
1887 	/*
1888 	 * Allocate and initialize the disk name.
1889 	 */
1890 	disk->dtype_asciilabel = alloc_string(disk_name);
1891 
1892 	/*
1893 	 * Initialize disk geometry info
1894 	 */
1895 	disk->dtype_pcyl = label->dkl_pcyl;
1896 	disk->dtype_ncyl = label->dkl_ncyl;
1897 	disk->dtype_acyl = label->dkl_acyl;
1898 	disk->dtype_nhead = label->dkl_nhead;
1899 	disk->dtype_nsect = label->dkl_nsect;
1900 	disk->dtype_rpm = label->dkl_rpm;
1901 
1902 	/*
1903 	 * Attempt to match the partition map in the label
1904 	 * with a know partition for this disk type.
1905 	 */
1906 	for (part = disk->dtype_plist; part; part = part->pinfo_next) {
1907 		if (parts_match(label, part)) {
1908 			break;
1909 		}
1910 	}
1911 
1912 	/*
1913 	 * If no match was made, we need to create a partition
1914 	 * map for this disk.
1915 	 */
1916 	if (part == NULL) {
1917 		part = (struct partition_info *)
1918 			zalloc(sizeof (struct partition_info));
1919 		pt = disk->dtype_plist;
1920 		if (pt == NULL) {
1921 			disk->dtype_plist = part;
1922 		} else {
1923 			while (pt->pinfo_next != NULL) {
1924 				pt = pt->pinfo_next;
1925 			}
1926 			pt->pinfo_next = part;
1927 		}
1928 		part->pinfo_next = NULL;
1929 
1930 		/*
1931 		 * Set up the partition name
1932 		 */
1933 		part->pinfo_name = alloc_string("default");
1934 
1935 		/*
1936 		 * Fill in the partition info from the label
1937 		 */
1938 		for (i = 0; i < NDKMAP; i++) {
1939 
1940 #if defined(_SUNOS_VTOC_8)
1941 			part->pinfo_map[i] = label->dkl_map[i];
1942 
1943 #elif defined(_SUNOS_VTOC_16)
1944 			part->pinfo_map[i].dkl_cylno =
1945 				label->dkl_vtoc.v_part[i].p_start /
1946 					((int)(disk->dtype_nhead *
1947 						disk->dtype_nsect - apc));
1948 			part->pinfo_map[i].dkl_nblk =
1949 				label->dkl_vtoc.v_part[i].p_size;
1950 #else
1951 #error No VTOC format defined.
1952 #endif				/* defined(_SUNOS_VTOC_8) */
1953 
1954 		}
1955 	}
1956 
1957 
1958 	/*
1959 	 * Use the VTOC if valid, or install a default
1960 	 */
1961 	if (label->dkl_vtoc.v_version == V_VERSION) {
1962 		(void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume,
1963 			LEN_DKL_VVOL);
1964 		part->vtoc = label->dkl_vtoc;
1965 	} else {
1966 		(void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL);
1967 		set_vtoc_defaults(part);
1968 	}
1969 
1970 	/*
1971 	 * Link the disk to the partition map
1972 	 */
1973 	disk_info->disk_parts = part;
1974 
1975 	return (disk);
1976 }
1977 
1978 
1979 /*
1980  * Delete a disk type from disk type list.
1981  */
1982 int
1983 delete_disk_type(
1984 		struct disk_type *disk_type)
1985 {
1986 	struct ctlr_type	*ctlr;
1987 	struct disk_type	*dp, *disk;
1988 
1989 	if (cur_ctype->ctype_ctype == DKC_DIRECT)
1990 		ctlr = find_direct_ctlr_type();
1991 	else
1992 		ctlr = find_scsi_ctlr_type();
1993 	if (ctlr == NULL || ctlr->ctype_dlist == NULL) {
1994 		return (-1);
1995 	}
1996 
1997 	disk = ctlr->ctype_dlist;
1998 	if (disk == disk_type) {
1999 		ctlr->ctype_dlist = disk->dtype_next;
2000 		if (cur_label == L_TYPE_EFI)
2001 			free(disk->dtype_plist->etoc);
2002 		free(disk->dtype_plist);
2003 		free(disk);
2004 		return (0);
2005 	} else {
2006 		for (dp = disk->dtype_next; dp != NULL;
2007 		    disk = disk->dtype_next, dp = dp->dtype_next) {
2008 			if (dp == disk_type) {
2009 				disk->dtype_next = dp->dtype_next;
2010 				if (cur_label == L_TYPE_EFI)
2011 					free(dp->dtype_plist->etoc);
2012 				free(dp->dtype_plist);
2013 				free(dp);
2014 				return (0);
2015 			}
2016 		}
2017 		return (-1);
2018 	}
2019 }
2020 
2021 
2022 static struct disk_info *
2023 find_scsi_disk_info(
2024 	struct dk_cinfo		*dkinfo)
2025 {
2026 	struct disk_info	*disk;
2027 	struct dk_cinfo		*dp;
2028 
2029 	for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
2030 		assert(dkinfo->dki_ctype == DKC_SCSI_CCS);
2031 		dp = &disk->disk_dkinfo;
2032 		if (dp->dki_ctype == dkinfo->dki_ctype &&
2033 			dp->dki_cnum == dkinfo->dki_cnum &&
2034 			dp->dki_unit == dkinfo->dki_unit &&
2035 			strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) {
2036 			return (disk);
2037 		}
2038 	}
2039 
2040 	impossible("No SCSI disk info instance\n");
2041 
2042 	return ((struct disk_info *)NULL);
2043 }
2044 
2045 
2046 static char *
2047 get_sun_disk_name(
2048 	char			*disk_name,
2049 	struct scsi_inquiry	*inquiry)
2050 {
2051 	/*
2052 	 * Extract the sun name of the disk
2053 	 */
2054 	(void) memset(disk_name, 0, DISK_NAME_MAX);
2055 	(void) memcpy(disk_name, (char *)&inquiry->inq_pid[9], 7);
2056 
2057 	return (disk_name);
2058 }
2059 
2060 
2061 static char *
2062 get_generic_disk_name(
2063 	char			*disk_name,
2064 	struct scsi_inquiry	*inquiry)
2065 {
2066 	char	*p;
2067 
2068 	(void) memset(disk_name, 0, DISK_NAME_MAX);
2069 	p = strcopy(disk_name, inquiry->inq_vid,
2070 		sizeof (inquiry->inq_vid));
2071 	*p++ = '-';
2072 	p = strcopy(p, inquiry->inq_pid, sizeof (inquiry->inq_pid));
2073 	*p++ = '-';
2074 	p = strcopy(p, inquiry->inq_revision,
2075 		sizeof (inquiry->inq_revision));
2076 
2077 	return (disk_name);
2078 }
2079 
2080 
2081 
2082 static int
2083 force_blocksize(
2084 	int	fd)
2085 {
2086 	union {
2087 		struct mode_format	page3;
2088 		uchar_t			buf3[MAX_MODE_SENSE_SIZE];
2089 	} u_page3;
2090 	struct mode_format		*page3 = &u_page3.page3;
2091 	struct scsi_ms_header		header;
2092 
2093 	if (check("\
2094 Must reformat device to 512-byte blocksize.  Continue") == 0) {
2095 
2096 		/*
2097 		 * Get current Page 3 - Format Parameters page
2098 		 */
2099 		if (uscsi_mode_sense(fd, DAD_MODE_FORMAT,
2100 			MODE_SENSE_PC_CURRENT, (caddr_t)&u_page3,
2101 			MAX_MODE_SENSE_SIZE, &header)) {
2102 			goto err;
2103 		}
2104 
2105 		/*
2106 		 * Make our changes to the geometry
2107 		 */
2108 		header.mode_header.length = 0;
2109 		header.mode_header.device_specific = 0;
2110 		page3->mode_page.ps = 0;
2111 		page3->data_bytes_sect = DEV_BSIZE;
2112 
2113 		/*
2114 		 * make sure that logical block size is of
2115 		 * DEV_BSIZE.
2116 		 */
2117 		header.block_descriptor.blksize_hi = (DEV_BSIZE >> 16);
2118 		header.block_descriptor.blksize_mid = (DEV_BSIZE >> 8);
2119 		header.block_descriptor.blksize_lo = (char)(DEV_BSIZE);
2120 		/*
2121 		 * Select current Page 3 - Format Parameters page
2122 		 */
2123 		if (uscsi_mode_select(fd, DAD_MODE_FORMAT,
2124 			MODE_SELECT_PF, (caddr_t)&u_page3,
2125 			MODESENSE_PAGE_LEN(&u_page3), &header)) {
2126 			goto err;
2127 		}
2128 
2129 		/*
2130 		 * Now reformat the device
2131 		 */
2132 		if (raw_format(fd)) {
2133 			goto err;
2134 		}
2135 		return (0);
2136 	}
2137 
2138 err:
2139 	if (option_msg && diag_msg) {
2140 		err_print(
2141 			"Reformat device to 512-byte blocksize failed\n");
2142 	}
2143 	return (1);
2144 }
2145 
2146 static int
2147 raw_format(
2148 	int	fd)
2149 {
2150 	union scsi_cdb			cdb;
2151 	struct uscsi_cmd		ucmd;
2152 	struct scsi_defect_hdr		defect_hdr;
2153 
2154 	(void) memset((char *)&ucmd, 0, sizeof (ucmd));
2155 	(void) memset((char *)&cdb, 0, sizeof (union scsi_cdb));
2156 	(void) memset((char *)&defect_hdr, 0, sizeof (defect_hdr));
2157 	cdb.scc_cmd = SCMD_FORMAT;
2158 	ucmd.uscsi_cdb = (caddr_t)&cdb;
2159 	ucmd.uscsi_cdblen = CDB_GROUP0;
2160 	ucmd.uscsi_bufaddr = (caddr_t)&defect_hdr;
2161 	ucmd.uscsi_buflen = sizeof (defect_hdr);
2162 	cdb.cdb_opaque[1] = FPB_DATA;
2163 
2164 	/*
2165 	 * Issue the format ioctl
2166 	 */
2167 	fmt_print("Formatting...\n");
2168 	(void) fflush(stdout);
2169 	if (uscsi_cmd(fd, &ucmd,
2170 		(option_msg && diag_msg) ? F_NORMAL : F_SILENT)) {
2171 		return (1);
2172 	}
2173 	return (0);
2174 }
2175 
2176 /*
2177  * Copy a string of characters from src to dst, for at
2178  * most n bytes.  Strip all leading and trailing spaces,
2179  * and stop if there are any non-printable characters.
2180  * Return ptr to the next character to be filled.
2181  */
2182 static char *
2183 strcopy(
2184 	char	*dst,
2185 	char	*src,
2186 	int	n)
2187 {
2188 	int	i;
2189 
2190 	while (*src == ' ' && n > 0) {
2191 		src++;
2192 		n--;
2193 	}
2194 
2195 	for (i = 0; n-- > 0 && isascii(*src) && isprint(*src); src++) {
2196 		if (*src == ' ') {
2197 			i++;
2198 		} else {
2199 			while (i-- > 0)
2200 				*dst++ = ' ';
2201 			*dst++ = *src;
2202 		}
2203 	}
2204 
2205 	*dst = 0;
2206 	return (dst);
2207 }
2208 
2209 /*
2210  * adjust disk geometry.
2211  * This is used when disk reports a disk geometry page having
2212  * no of physical cylinders is < 3 which is the minimum required
2213  * by Solaris (2 for storing labels and at least one as a data
2214  * cylinder )
2215  */
2216 int
2217 adjust_disk_geometry(int capacity, int *cyl, int *nhead, int *nsect)
2218 {
2219 	int	lcyl = *cyl;
2220 	int	lnhead = *nhead;
2221 	int	lnsect = *nsect;
2222 
2223 	assert(lcyl < SUN_MIN_CYL);
2224 
2225 	/*
2226 	 * reduce nsect by 2 for each iteration  and re-calculate
2227 	 * the number of cylinders.
2228 	 */
2229 	while (lnsect > MINIMUM_NO_SECTORS &&
2230 			lcyl < MINIMUM_NO_CYLINDERS) {
2231 		/*
2232 		 * make sure that we do not go below MINIMUM_NO_SECTORS.
2233 		 */
2234 		lnsect = max(MINIMUM_NO_SECTORS, lnsect / 2);
2235 		lcyl   = (capacity) / (lnhead * lnsect);
2236 	}
2237 	/*
2238 	 * If the geometry still does not satisfy
2239 	 * MINIMUM_NO_CYLINDERS then try to reduce the
2240 	 * no of heads.
2241 	 */
2242 	while (lnhead > MINIMUM_NO_HEADS &&
2243 			lcyl < MINIMUM_NO_CYLINDERS) {
2244 		lnhead = max(MINIMUM_NO_HEADS, lnhead / 2);
2245 		lcyl =  (capacity) / (lnhead * lnsect);
2246 	}
2247 	/*
2248 	 * now we should have atleast SUN_MIN_CYL cylinders.
2249 	 * If we still do not get SUN_MIN_CYL with MINIMUM_NO_HEADS
2250 	 * and MINIMUM_NO_HEADS then return error.
2251 	 */
2252 	if (lcyl < SUN_MIN_CYL)
2253 		return (1);
2254 	else {
2255 		*cyl = lcyl;
2256 		*nhead = lnhead;
2257 		*nsect = lnsect;
2258 		return (0);
2259 	}
2260 }
2261 
2262 #if defined(_SUNOS_VTOC_8)
2263 /*
2264  * Reduce the size of one dimention below a specified
2265  * limit with a minimum loss of volume.  Dimenstions are
2266  * assumed to be passed in form the largest value (the one
2267  * that needs to be reduced) to the smallest value.  The
2268  * values will be twiddled until they are all less than or
2269  * equal to their limit.  Returns the number in the new geometry.
2270  */
2271 static int
2272 square_box(
2273 		int capacity,
2274 		int *dim1, int lim1,
2275 		int *dim2, int lim2,
2276 		int *dim3, int lim3)
2277 {
2278 	int	i;
2279 
2280 	/*
2281 	 * Although the routine should work with any ordering of
2282 	 * parameters, it's most efficient if they are passed in
2283 	 * in decreasing magnitude.
2284 	 */
2285 	assert(*dim1 >= *dim2);
2286 	assert(*dim2 >= *dim3);
2287 
2288 	/*
2289 	 * This is done in a very arbitrary manner.  We could try to
2290 	 * find better values but I can't come up with a method that
2291 	 * would run in a reasonable amount of time.  That could take
2292 	 * approximately 65535 * 65535 iterations of a dozen flops each
2293 	 * or well over 4G flops.
2294 	 *
2295 	 * First:
2296 	 *
2297 	 * Let's see how far we can go with bitshifts w/o losing
2298 	 * any blocks.
2299 	 */
2300 
2301 	for (i = 0; (((*dim1)>>i)&1) == 0 && ((*dim1)>>i) > lim1; i++);
2302 	if (i) {
2303 		*dim1 = ((*dim1)>>i);
2304 		*dim3 = ((*dim3)<<i);
2305 	}
2306 
2307 	if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) {
2308 		double 	d[4];
2309 
2310 		/*
2311 		 * Second:
2312 		 *
2313 		 * Set the highest value at its limit then calculate errors,
2314 		 * adjusting the 2nd highest value (we get better resolution
2315 		 * that way).
2316 		 */
2317 		d[1] = lim1;
2318 		d[3] = *dim3;
2319 		d[2] = (double)capacity/(d[1]*d[3]);
2320 
2321 		/*
2322 		 * If we overflowed the middle term, set it to its limit and
2323 		 * chose a new low term.
2324 		 */
2325 		if (d[2] > lim2) {
2326 			d[2] = lim2;
2327 			d[3] = (double)capacity/(d[1]*d[2]);
2328 		}
2329 		/*
2330 		 * Convert to integers.
2331 		 */
2332 		*dim1 = (int)d[1];
2333 		*dim2 = (int)d[2];
2334 		*dim3 = (int)d[3];
2335 	}
2336 	/*
2337 	 * Fixup any other possible problems.
2338 	 * If this happens, we need a new disklabel format.
2339 	 */
2340 	if (*dim1 > lim1) *dim1 = lim1;
2341 	if (*dim2 > lim2) *dim2 = lim2;
2342 	if (*dim3 > lim3) *dim3 = lim3;
2343 	return (*dim1 * *dim2 * *dim3);
2344 }
2345 #endif /* defined(_SUNOS_VTOC_8) */
2346