Merge commit '2cedd8f0ecbd2b29bf0aac72bb8b7413b0326938' into merges
[unleashed.git] / usr / src / cmd / format / auto_sense.c
blob089e81b49b084eef50891ec3c286492161f55e4f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2011 Gary Mills
24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31 * This file contains functions to implement automatic configuration
32 * of scsi disks.
34 #include "global.h"
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <stdlib.h>
41 #include <ctype.h>
43 #include "misc.h"
44 #include "param.h"
45 #include "ctlr_scsi.h"
46 #include "auto_sense.h"
47 #include "partition.h"
48 #include "label.h"
49 #include "startup.h"
50 #include "analyze.h"
51 #include "io.h"
52 #include "hardware_structs.h"
53 #include "menu_fdisk.h"
56 #define DISK_NAME_MAX 256
58 extern int nctypes;
59 extern struct ctlr_type ctlr_types[];
63 * Marker for free hog partition
65 #define HOG (-1)
70 * Default partition tables
72 * Disk capacity root swap usr
73 * ------------- ---- ---- ---
74 * 0mb to 64mb 0 0 remainder
75 * 64mb to 180mb 16mb 16mb remainder
76 * 180mb to 280mb 16mb 32mb remainder
77 * 280mb to 380mb 24mb 32mb remainder
78 * 380mb to 600mb 32mb 32mb remainder
79 * 600mb to 1gb 32mb 64mb remainder
80 * 1gb to 2gb 64mb 128mb remainder
81 * 2gb on up 128mb 128mb remainder
83 struct part_table {
84 int partitions[NDKMAP];
87 static struct part_table part_table_64mb = {
88 { 0, 0, 0, 0, 0, 0, HOG, 0}
91 static struct part_table part_table_180mb = {
92 { 16, 16, 0, 0, 0, 0, HOG, 0}
95 static struct part_table part_table_280mb = {
96 { 16, 32, 0, 0, 0, 0, HOG, 0}
99 static struct part_table part_table_380mb = {
100 { 24, 32, 0, 0, 0, 0, HOG, 0}
103 static struct part_table part_table_600mb = {
104 { 32, 32, 0, 0, 0, 0, HOG, 0}
107 static struct part_table part_table_1gb = {
108 { 32, 64, 0, 0, 0, 0, HOG, 0}
111 static struct part_table part_table_2gb = {
112 { 64, 128, 0, 0, 0, 0, HOG, 0}
115 static struct part_table part_table_infinity = {
116 { 128, 128, 0, 0, 0, 0, HOG, 0}
120 static struct default_partitions {
121 diskaddr_t min_capacity;
122 diskaddr_t max_capacity;
123 struct part_table *part_table;
124 } default_partitions[] = {
125 { 0, 64, &part_table_64mb }, /* 0 to 64 mb */
126 { 64, 180, &part_table_180mb }, /* 64 to 180 mb */
127 { 180, 280, &part_table_280mb }, /* 180 to 280 mb */
128 { 280, 380, &part_table_380mb }, /* 280 to 380 mb */
129 { 380, 600, &part_table_600mb }, /* 380 to 600 mb */
130 { 600, 1024, &part_table_1gb }, /* 600 to 1 gb */
131 { 1024, 2048, &part_table_2gb }, /* 1 to 2 gb */
132 { 2048, INFINITY, &part_table_infinity }, /* 2 gb on up */
135 #define DEFAULT_PARTITION_TABLE_SIZE \
136 (sizeof (default_partitions) / sizeof (struct default_partitions))
139 * msgs for check()
141 #define FORMAT_MSG "Auto configuration via format.dat"
142 #define GENERIC_MSG "Auto configuration via generic SCSI-2"
145 * Disks on symbios(Hardwire raid controller) return a fixed number
146 * of heads(64)/cylinders(64) and adjust the cylinders depending
147 * capacity of the configured lun.
148 * In such a case we get number of physical cylinders < 3 which
149 * is the minimum required by solaris(2 reserved + 1 data cylinders).
150 * Hence try to adjust the cylinders by reducing the "nsect/nhead".
154 * assuming a minimum of 32 block cylinders.
156 #define MINIMUM_NO_HEADS 2
157 #define MINIMUM_NO_SECTORS 16
159 #define MINIMUM_NO_CYLINDERS 128
161 #if defined(_SUNOS_VTOC_8)
163 /* These are 16-bit fields */
164 #define MAXIMUM_NO_HEADS 65535
165 #define MAXIMUM_NO_SECTORS 65535
166 #define MAXIMUM_NO_CYLINDERS 65535
168 #endif /* defined(_SUNOS_VTOC_8) */
171 * minimum number of cylinders required by Solaris.
173 #define SUN_MIN_CYL 3
178 * ANSI prototypes for local static functions
180 static struct disk_type *generic_disk_sense(
181 int fd,
182 int can_prompt,
183 struct dk_label *label,
184 struct scsi_inquiry *inquiry,
185 struct scsi_capacity_16 *capacity,
186 char *disk_name);
187 static int use_existing_disk_type(
188 int fd,
189 int can_prompt,
190 struct dk_label *label,
191 struct scsi_inquiry *inquiry,
192 struct disk_type *disk_type,
193 struct scsi_capacity_16 *capacity);
194 int build_default_partition(struct dk_label *label,
195 int ctrl_type);
196 static struct disk_type *find_scsi_disk_type(
197 char *disk_name,
198 struct dk_label *label);
199 static struct disk_type *find_scsi_disk_by_name(
200 char *disk_name);
201 static struct ctlr_type *find_scsi_ctlr_type(void);
202 static struct ctlr_info *find_scsi_ctlr_info(
203 struct dk_cinfo *dkinfo);
204 static struct disk_type *new_scsi_disk_type(
205 int fd,
206 char *disk_name,
207 struct dk_label *label);
208 static struct disk_info *find_scsi_disk_info(
209 struct dk_cinfo *dkinfo);
211 static struct disk_type *new_direct_disk_type(int fd, char *disk_name,
212 struct dk_label *label);
214 static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
215 static int auto_label_init(struct dk_label *label);
216 static struct ctlr_type *find_ctlr_type(ushort_t);
217 static struct ctlr_info *find_ctlr_info(struct dk_cinfo *, ushort_t);
218 static struct disk_info *find_disk_info(struct dk_cinfo *, ushort_t);
220 static char *get_sun_disk_name(
221 char *disk_name,
222 struct scsi_inquiry *inquiry);
223 static char *strcopy(
224 char *dst,
225 char *src,
226 int n);
227 static int adjust_disk_geometry(diskaddr_t capacity, uint_t *cyl,
228 uint_t *nsect, uint_t *nhead);
229 static void compute_chs_values(diskaddr_t total_capacity,
230 diskaddr_t usable_capacity, uint_t *pcylp,
231 uint_t *nheadp, uint_t *nsectp);
232 #if defined(_SUNOS_VTOC_8)
233 static diskaddr_t square_box(
234 diskaddr_t capacity,
235 uint_t *dim1, uint_t lim1,
236 uint_t *dim2, uint_t lim2,
237 uint_t *dim3, uint_t lim3);
238 #endif /* defined(_SUNOS_VTOC_8) */
242 * We need to get information necessary to construct a *new* efi
243 * label type
245 struct disk_type *
246 auto_efi_sense(int fd, struct efi_info *label)
249 struct dk_gpt *vtoc;
250 int i;
252 struct disk_type *disk, *dp;
253 struct disk_info *disk_info;
254 struct ctlr_info *ctlr;
255 struct dk_cinfo dkinfo;
256 struct partition_info *part;
258 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
259 if (option_msg && diag_msg) {
260 err_print("DKIOCINFO failed\n");
262 return (NULL);
264 if ((cur_ctype != NULL) && (cur_ctype->ctype_ctype == DKC_DIRECT ||
265 cur_ctype->ctype_ctype == DKC_VBD ||
266 cur_ctype->ctype_ctype == DKC_BLKDEV)) {
267 ctlr = find_ctlr_info(&dkinfo, cur_ctype->ctype_ctype);
268 disk_info = find_disk_info(&dkinfo, cur_ctype->ctype_ctype);
269 } else {
270 ctlr = find_scsi_ctlr_info(&dkinfo);
271 disk_info = find_scsi_disk_info(&dkinfo);
275 * get vendor, product, revision and capacity info.
277 if (get_disk_info(fd, label, disk_info) == -1) {
278 return (NULL);
281 * Now build the default partition table
283 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
284 err_print("efi_alloc_and_init failed. \n");
285 return (NULL);
288 label->e_parts = vtoc;
291 * Create a whole hog EFI partition table:
292 * S0 takes the whole disk except the primary EFI label,
293 * backup EFI label, and the reserved partition.
295 vtoc->efi_parts[0].p_tag = V_USR;
296 vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba;
297 vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba - vtoc->efi_first_u_lba
298 - EFI_MIN_RESV_SIZE + 1;
301 * S1-S6 are unassigned slices.
303 for (i = 1; i < vtoc->efi_nparts - 2; i ++) {
304 vtoc->efi_parts[i].p_tag = V_UNASSIGNED;
305 vtoc->efi_parts[i].p_start = 0;
306 vtoc->efi_parts[i].p_size = 0;
310 * The reserved slice
312 vtoc->efi_parts[vtoc->efi_nparts - 1].p_tag = V_RESERVED;
313 vtoc->efi_parts[vtoc->efi_nparts - 1].p_start =
314 vtoc->efi_last_u_lba - EFI_MIN_RESV_SIZE + 1;
315 vtoc->efi_parts[vtoc->efi_nparts - 1].p_size = EFI_MIN_RESV_SIZE;
318 * Now stick all of it into the disk_type struct
321 disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
322 assert(disk_info->disk_ctlr == ctlr);
323 dp = ctlr->ctlr_ctype->ctype_dlist;
324 if (dp == NULL) {
325 ctlr->ctlr_ctype->ctype_dlist = dp;
326 } else {
327 while (dp->dtype_next != NULL) {
328 dp = dp->dtype_next;
330 dp->dtype_next = disk;
332 disk->dtype_next = NULL;
334 disk->vendor = strdup(label->vendor);
335 disk->product = strdup(label->product);
336 disk->revision = strdup(label->revision);
338 if (disk->vendor == NULL ||
339 disk->product == NULL ||
340 disk->revision == NULL) {
341 free(disk->vendor);
342 free(disk->product);
343 free(disk->revision);
344 free(disk);
345 return (NULL);
348 disk->capacity = label->capacity;
350 part = (struct partition_info *)
351 zalloc(sizeof (struct partition_info));
352 disk->dtype_plist = part;
354 part->pinfo_name = alloc_string("default");
355 part->pinfo_next = NULL;
356 part->etoc = vtoc;
358 bzero(disk_info->v_volume, LEN_DKL_VVOL);
359 disk_info->disk_parts = part;
360 return (disk);
363 static int
364 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
366 void *data = dk_ioc->dki_data;
367 int error;
369 dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
370 error = ioctl(fd, cmd, (void *)dk_ioc);
371 dk_ioc->dki_data = data;
373 return (error);
376 static struct ctlr_type *
377 find_ctlr_type(ushort_t type)
379 struct mctlr_list *mlp;
381 assert(type == DKC_DIRECT ||
382 type == DKC_VBD ||
383 type == DKC_BLKDEV);
385 mlp = controlp;
387 while (mlp != NULL) {
388 if (mlp->ctlr_type->ctype_ctype == type) {
389 return (mlp->ctlr_type);
391 mlp = mlp->next;
394 impossible("no DIRECT/VBD/BLKDEV controller type");
396 return (NULL);
399 static struct ctlr_info *
400 find_ctlr_info(struct dk_cinfo *dkinfo, ushort_t type)
402 struct ctlr_info *ctlr;
404 assert(type == DKC_DIRECT ||
405 type == DKC_VBD ||
406 type == DKC_BLKDEV);
408 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) {
409 if (ctlr->ctlr_addr == dkinfo->dki_addr &&
410 ctlr->ctlr_space == dkinfo->dki_space &&
411 ctlr->ctlr_ctype->ctype_ctype == dkinfo->dki_ctype) {
412 return (ctlr);
416 impossible("no DIRECT/VBD/BLKDEV controller info");
417 /*NOTREACHED*/
418 return (NULL);
421 static struct disk_info *
422 find_disk_info(struct dk_cinfo *dkinfo, ushort_t type)
424 struct disk_info *disk;
425 struct dk_cinfo *dp;
427 assert(type == DKC_DIRECT ||
428 type == DKC_VBD ||
429 type == DKC_BLKDEV);
431 for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
432 dp = &disk->disk_dkinfo;
433 if (dp->dki_ctype == dkinfo->dki_ctype &&
434 dp->dki_cnum == dkinfo->dki_cnum &&
435 dp->dki_unit == dkinfo->dki_unit &&
436 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) {
437 return (disk);
441 impossible("No DIRECT/VBD/BLKDEV disk info instance\n");
442 /*NOTREACHED*/
443 return (NULL);
447 * To convert EFI to SMI labels, we need to get label geometry.
448 * Unfortunately at this time there is no good way to do so.
449 * DKIOCGGEOM will fail if disk is EFI labeled. So we hack around
450 * it and clear EFI label, do a DKIOCGGEOM and put the EFI label
451 * back on disk.
452 * This routine gets the label geometry and initializes the label
453 * It uses cur_file as opened device.
454 * returns 0 if succeeds or -1 if failed.
456 static int
457 auto_label_init(struct dk_label *label)
459 dk_efi_t dk_ioc;
460 dk_efi_t dk_ioc_back;
461 efi_gpt_t *data = NULL;
462 efi_gpt_t *databack = NULL;
463 struct dk_geom disk_geom;
464 struct dk_minfo disk_info;
465 efi_gpt_t *backsigp;
466 int fd = cur_file;
467 int rval = -1;
468 int efisize = EFI_LABEL_SIZE * 2;
469 int success = 0;
470 uint64_t sig;
471 uint64_t backsig;
473 if ((data = calloc(efisize, 1)) == NULL) {
474 err_print("auto_label_init: calloc failed\n");
475 goto auto_label_init_out;
478 dk_ioc.dki_data = data;
479 dk_ioc.dki_lba = 1;
480 dk_ioc.dki_length = efisize;
482 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) != 0) {
483 err_print("auto_label_init: GETEFI failed\n");
484 goto auto_label_init_out;
487 if ((databack = calloc(efisize, 1)) == NULL) {
488 err_print("auto_label_init calloc2 failed");
489 goto auto_label_init_out;
492 /* get the LBA size and capacity */
493 if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
494 err_print("auto_label_init: dkiocgmediainfo failed\n");
495 goto auto_label_init_out;
498 if (disk_info.dki_lbsize == 0) {
499 if (option_msg && diag_msg) {
500 err_print("auto_lbal_init: assuming 512 byte"
501 "block size");
503 disk_info.dki_lbsize = DEV_BSIZE;
506 dk_ioc_back.dki_data = databack;
509 * back up efi label goes to capacity - 1, we are reading an extra block
510 * before the back up label.
512 dk_ioc_back.dki_lba = disk_info.dki_capacity - 1 - 1;
513 dk_ioc_back.dki_length = efisize;
515 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc_back) != 0) {
516 err_print("auto_label_init: GETEFI backup failed\n");
517 goto auto_label_init_out;
520 sig = dk_ioc.dki_data->efi_gpt_Signature;
521 dk_ioc.dki_data->efi_gpt_Signature = 0x0;
523 enter_critical();
525 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
526 err_print("auto_label_init: SETEFI failed\n");
527 exit_critical();
528 goto auto_label_init_out;
531 backsigp = (efi_gpt_t *)((uintptr_t)dk_ioc_back.dki_data + cur_blksz);
533 backsig = backsigp->efi_gpt_Signature;
535 backsigp->efi_gpt_Signature = 0;
537 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc_back) == -1) {
538 err_print("auto_label_init: SETEFI backup failed\n");
541 if (ioctl(cur_file, DKIOCGGEOM, &disk_geom) != 0)
542 err_print("auto_label_init: GGEOM failed\n");
543 else
544 success = 1;
546 dk_ioc.dki_data->efi_gpt_Signature = sig;
547 backsigp->efi_gpt_Signature = backsig;
549 if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc_back) == -1) {
550 err_print("auto_label_init: SETEFI revert backup failed\n");
551 success = 0;
554 if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc) == -1) {
555 err_print("auto_label_init: SETEFI revert failed\n");
556 success = 0;
559 exit_critical();
561 if (success == 0)
562 goto auto_label_init_out;
564 ncyl = disk_geom.dkg_ncyl;
565 acyl = disk_geom.dkg_acyl;
566 nhead = disk_geom.dkg_nhead;
567 nsect = disk_geom.dkg_nsect;
568 pcyl = ncyl + acyl;
570 label->dkl_pcyl = pcyl;
571 label->dkl_ncyl = ncyl;
572 label->dkl_acyl = acyl;
573 label->dkl_nhead = nhead;
574 label->dkl_nsect = nsect;
575 label->dkl_apc = 0;
576 label->dkl_intrlv = 1;
577 label->dkl_rpm = disk_geom.dkg_rpm;
579 label->dkl_magic = DKL_MAGIC;
581 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
582 "%s cyl %u alt %u hd %u sec %u",
583 "DEFAULT", ncyl, acyl, nhead, nsect);
585 rval = 0;
586 #if defined(_FIRMWARE_NEEDS_FDISK)
587 (void) auto_solaris_part(label);
588 ncyl = label->dkl_ncyl;
590 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
592 if (!build_default_partition(label, DKC_DIRECT)) {
593 rval = -1;
596 (void) checksum(label, CK_MAKESUM);
599 auto_label_init_out:
600 free(data);
601 free(databack);
603 return (rval);
606 static struct disk_type *
607 new_direct_disk_type(
608 int fd,
609 char *disk_name,
610 struct dk_label *label)
612 struct disk_type *dp;
613 struct disk_type *disk;
614 struct ctlr_info *ctlr;
615 struct dk_cinfo dkinfo;
616 struct partition_info *part = NULL;
617 struct partition_info *pt;
618 struct disk_info *disk_info;
619 int i;
622 * Get the disk controller info for this disk
624 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
625 if (option_msg && diag_msg) {
626 err_print("DKIOCINFO failed\n");
628 return (NULL);
632 * Find the ctlr_info for this disk.
634 ctlr = find_ctlr_info(&dkinfo, dkinfo.dki_ctype);
637 * Allocate a new disk type for the direct controller.
639 disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
642 * Find the disk_info instance for this disk.
644 disk_info = find_disk_info(&dkinfo, dkinfo.dki_ctype);
647 * The controller and the disk should match.
649 assert(disk_info->disk_ctlr == ctlr);
652 * Link the disk into the list of disks
654 dp = ctlr->ctlr_ctype->ctype_dlist;
655 if (dp == NULL) {
656 ctlr->ctlr_ctype->ctype_dlist = dp;
657 } else {
658 while (dp->dtype_next != NULL) {
659 dp = dp->dtype_next;
661 dp->dtype_next = disk;
663 disk->dtype_next = NULL;
666 * Allocate and initialize the disk name.
668 disk->dtype_asciilabel = alloc_string(disk_name);
671 * Initialize disk geometry info
673 disk->dtype_pcyl = label->dkl_pcyl;
674 disk->dtype_ncyl = label->dkl_ncyl;
675 disk->dtype_acyl = label->dkl_acyl;
676 disk->dtype_nhead = label->dkl_nhead;
677 disk->dtype_nsect = label->dkl_nsect;
678 disk->dtype_rpm = label->dkl_rpm;
680 part = (struct partition_info *)
681 zalloc(sizeof (struct partition_info));
682 pt = disk->dtype_plist;
683 if (pt == NULL) {
684 disk->dtype_plist = part;
685 } else {
686 while (pt->pinfo_next != NULL) {
687 pt = pt->pinfo_next;
689 pt->pinfo_next = part;
692 part->pinfo_next = NULL;
695 * Set up the partition name
697 part->pinfo_name = alloc_string("default");
700 * Fill in the partition info from the label
702 for (i = 0; i < NDKMAP; i++) {
704 #if defined(_SUNOS_VTOC_8)
705 part->pinfo_map[i] = label->dkl_map[i];
707 #elif defined(_SUNOS_VTOC_16)
708 part->pinfo_map[i].dkl_cylno =
709 label->dkl_vtoc.v_part[i].p_start /
710 ((blkaddr_t)(disk->dtype_nhead *
711 disk->dtype_nsect - apc));
712 part->pinfo_map[i].dkl_nblk =
713 label->dkl_vtoc.v_part[i].p_size;
714 #else
715 #error No VTOC format defined.
716 #endif /* defined(_SUNOS_VTOC_8) */
720 * Use the VTOC if valid, or install a default
722 if (label->dkl_vtoc.v_version == V_VERSION) {
723 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume,
724 LEN_DKL_VVOL);
725 part->vtoc = label->dkl_vtoc;
726 } else {
727 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL);
728 set_vtoc_defaults(part);
732 * Link the disk to the partition map
734 disk_info->disk_parts = part;
736 return (disk);
740 * Get a disk type that has label info. This is used to convert
741 * EFI label to SMI label
743 struct disk_type *
744 auto_direct_get_geom_label(int fd, struct dk_label *label)
746 struct disk_type *disk_type;
748 if (auto_label_init(label) != 0) {
749 err_print("auto_direct_get_geom_label: failed to get label"
750 "geometry");
751 return (NULL);
752 } else {
753 disk_type = new_direct_disk_type(fd, "DEFAULT", label);
754 return (disk_type);
759 * Auto-sense a scsi disk configuration, ie get the information
760 * necessary to construct a label. We have two different
761 * ways to auto-sense a scsi disk:
762 * - format.dat override, via inquiry name
763 * - generic scsi, via standard mode sense and inquiry
764 * Depending on how and when we are called, and/or
765 * change geometry and reformat.
767 struct disk_type *
768 auto_sense(
769 int fd,
770 int can_prompt,
771 struct dk_label *label)
773 struct scsi_inquiry inquiry;
774 struct scsi_capacity_16 capacity;
775 struct disk_type *disk_type;
776 char disk_name[DISK_NAME_MAX];
777 int force_format_dat = 0;
778 int force_generic = 0;
779 u_ioparam_t ioparam;
780 int deflt;
781 char *buf;
784 * First, if expert mode, find out if the user
785 * wants to override any of the standard methods.
787 if (can_prompt && expert_mode) {
788 deflt = 1;
789 ioparam.io_charlist = confirm_list;
790 if (input(FIO_MSTR, FORMAT_MSG, '?', &ioparam,
791 &deflt, DATA_INPUT) == 0) {
792 force_format_dat = 1;
793 } else if (input(FIO_MSTR, GENERIC_MSG, '?', &ioparam,
794 &deflt, DATA_INPUT) == 0) {
795 force_generic = 1;
800 * Get the Inquiry data. If this fails, there's
801 * no hope for this disk, so give up.
803 if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
804 return (NULL);
806 if (option_msg && diag_msg) {
807 err_print("Product id: ");
808 print_buf(inquiry.inq_pid, sizeof (inquiry.inq_pid));
809 err_print("\n");
813 * Get the Read Capacity
815 if (uscsi_read_capacity(fd, &capacity)) {
816 return (NULL);
820 * If the reported capacity is set to zero, then the disk
821 * is not usable. If the reported capacity is set to all
822 * 0xf's, then this disk is too large. These could only
823 * happen with a device that supports LBAs larger than 64
824 * bits which are not defined by any current T10 standards
825 * or by error responding from target.
827 if ((capacity.sc_capacity == 0) ||
828 (capacity.sc_capacity == UINT_MAX64)) {
829 if (option_msg && diag_msg) {
830 err_print("Invalid capacity\n");
832 return (NULL);
834 if (option_msg && diag_msg) {
835 err_print("blocks: %llu (0x%llx)\n",
836 capacity.sc_capacity, capacity.sc_capacity);
837 err_print("blksize: %u\n", capacity.sc_lbasize);
841 * Extract the disk name for the format.dat override
843 (void) get_sun_disk_name(disk_name, &inquiry);
844 if (option_msg && diag_msg) {
845 err_print("disk name: `%s`\n", disk_name);
848 buf = zalloc(cur_blksz);
849 if (scsi_rdwr(DIR_READ, fd, (diskaddr_t)0, 1, (caddr_t)buf,
850 F_SILENT, NULL)) {
851 free(buf);
852 return (NULL);
854 free(buf);
857 * Figure out which method we use for auto sense.
858 * If a particular method fails, we fall back to
859 * the next possibility.
862 if (force_generic) {
863 return (generic_disk_sense(fd, can_prompt, label,
864 &inquiry, &capacity, disk_name));
868 * Try for an existing format.dat first
870 if ((disk_type = find_scsi_disk_by_name(disk_name)) != NULL) {
871 if (use_existing_disk_type(fd, can_prompt, label,
872 &inquiry, disk_type, &capacity)) {
873 return (disk_type);
875 if (force_format_dat) {
876 return (NULL);
881 * Otherwise, try using generic SCSI-2 sense and inquiry.
884 return (generic_disk_sense(fd, can_prompt, label,
885 &inquiry, &capacity, disk_name));
890 /*ARGSUSED*/
891 static struct disk_type *
892 generic_disk_sense(
893 int fd,
894 int can_prompt,
895 struct dk_label *label,
896 struct scsi_inquiry *inquiry,
897 struct scsi_capacity_16 *capacity,
898 char *disk_name)
900 struct disk_type *disk;
901 int setdefault = 0;
902 uint_t pcyl = 0;
903 uint_t ncyl = 0;
904 uint_t acyl = 0;
905 uint_t nhead = 0;
906 uint_t nsect = 0;
907 int rpm = 0;
908 diskaddr_t nblocks = 0;
909 diskaddr_t tblocks = 0;
910 union {
911 struct mode_format page3;
912 uchar_t buf3[MAX_MODE_SENSE_SIZE];
913 } u_page3;
914 union {
915 struct mode_geometry page4;
916 uchar_t buf4[MAX_MODE_SENSE_SIZE];
917 } u_page4;
918 struct mode_format *page3 = &u_page3.page3;
919 struct mode_geometry *page4 = &u_page4.page4;
920 struct scsi_ms_header header;
923 * If the name of this disk appears to be "SUN", use it,
924 * otherwise construct a name out of the generic
925 * Inquiry info. If it turns out that we already
926 * have a SUN disk type of this name that differs
927 * in geometry, we will revert to the generic name
928 * anyway.
930 if (memcmp(disk_name, "SUN", strlen("SUN")) != 0) {
931 (void) get_generic_disk_name(disk_name, inquiry);
935 * Get the number of blocks from Read Capacity data. Note that
936 * the logical block address range from 0 to capacity->sc_capacity.
937 * Limit the size to 2 TB (UINT32_MAX) to use with SMI labels.
939 tblocks = (capacity->sc_capacity + 1);
940 if (tblocks > UINT32_MAX)
941 nblocks = UINT32_MAX;
942 else
943 nblocks = tblocks;
946 * Get current Page 3 - Format Parameters page
948 if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, MODE_SENSE_PC_CURRENT,
949 (caddr_t)&u_page3, MAX_MODE_SENSE_SIZE, &header)) {
950 setdefault = 1;
954 * Get current Page 4 - Drive Geometry page
956 if (uscsi_mode_sense(fd, DAD_MODE_GEOMETRY, MODE_SENSE_PC_CURRENT,
957 (caddr_t)&u_page4, MAX_MODE_SENSE_SIZE, &header)) {
958 setdefault = 1;
961 if (setdefault != 1) {
962 /* The inquiry of mode page 3 & page 4 are successful */
964 * Correct for byte order if necessary
966 page4->rpm = BE_16(page4->rpm);
967 page4->step_rate = BE_16(page4->step_rate);
968 page3->tracks_per_zone = BE_16(page3->tracks_per_zone);
969 page3->alt_sect_zone = BE_16(page3->alt_sect_zone);
970 page3->alt_tracks_zone = BE_16(page3->alt_tracks_zone);
971 page3->alt_tracks_vol = BE_16(page3->alt_tracks_vol);
972 page3->sect_track = BE_16(page3->sect_track);
973 page3->data_bytes_sect = BE_16(page3->data_bytes_sect);
974 page3->interleave = BE_16(page3->interleave);
975 page3->track_skew = BE_16(page3->track_skew);
976 page3->cylinder_skew = BE_16(page3->cylinder_skew);
980 * Construct a new label out of the sense data,
981 * Inquiry and Capacity.
983 * If the disk capacity is > 1TB then simply compute
984 * the CHS values based on the total disk capacity and
985 * not use the values from mode-sense data.
987 if (tblocks > INT32_MAX) {
988 compute_chs_values(tblocks, nblocks, &pcyl, &nhead,
989 &nsect);
990 } else {
991 pcyl = (page4->cyl_ub << 16) + (page4->cyl_mb << 8) +
992 page4->cyl_lb;
993 nhead = page4->heads;
994 nsect = page3->sect_track;
997 rpm = page4->rpm;
1000 * If the number of physical cylinders reported is less
1001 * the SUN_MIN_CYL(3) then try to adjust the geometry so that
1002 * we have atleast SUN_MIN_CYL cylinders.
1004 if (pcyl < SUN_MIN_CYL) {
1005 if (nhead == 0 || nsect == 0) {
1006 setdefault = 1;
1007 } else if (adjust_disk_geometry(
1008 (diskaddr_t)(capacity->sc_capacity + 1),
1009 &pcyl, &nhead, &nsect)) {
1010 setdefault = 1;
1016 * Mode sense page 3 and page 4 are obsolete in SCSI-3. For
1017 * newly developed large sector size disk, we will not rely on
1018 * those two pages but compute geometry directly.
1020 if ((setdefault == 1) || (capacity->sc_lbasize != DEV_BSIZE)) {
1022 * If the number of cylinders or the number of heads reported
1023 * is zero, we think the inquiry of page 3 and page 4 failed.
1024 * We will set the geometry infomation by ourselves.
1026 compute_chs_values(tblocks, nblocks, &pcyl, &nhead, &nsect);
1030 * The sd driver reserves 2 cylinders the backup disk label and
1031 * the deviceid. Set the number of data cylinders to pcyl-acyl.
1033 acyl = DK_ACYL;
1034 ncyl = pcyl - acyl;
1036 if (option_msg && diag_msg) {
1037 err_print("Geometry:\n");
1038 err_print(" pcyl: %u\n", pcyl);
1039 err_print(" ncyl: %u\n", ncyl);
1040 err_print(" heads: %u\n", nhead);
1041 err_print(" nsects: %u\n", nsect);
1042 err_print(" acyl: %u\n", acyl);
1044 #if defined(_SUNOS_VTOC_16)
1045 err_print(" bcyl: %u\n", bcyl);
1046 #endif /* defined(_SUNOS_VTOC_16) */
1048 err_print(" rpm: %d\n", rpm);
1049 err_print(" nblocks: %llu\n", nblocks);
1053 * Some drives do not support page4 or report 0 for page4->rpm,
1054 * adjust it to AVG_RPM, 3600.
1056 if (rpm < MIN_RPM || rpm > MAX_RPM) {
1057 if (option_msg && diag_msg)
1058 err_print("The current rpm value %d is invalid,"
1059 " adjusting it to %d\n", rpm, AVG_RPM);
1060 rpm = AVG_RPM;
1064 * Some drives report 0 for nsect (page 3, byte 10 and 11) if they
1065 * have variable number of sectors per track. So adjust nsect.
1066 * Also the value is defined as vendor specific, hence check if
1067 * it is in a tolerable range. The values (32 and 4 below) are
1068 * chosen so that this change below does not generate a different
1069 * geometry for currently supported sun disks.
1071 if ((nsect == 0) ||
1072 ((diskaddr_t)pcyl * nhead * nsect) < (nblocks - nblocks/32) ||
1073 ((diskaddr_t)pcyl * nhead * nsect) > (nblocks + nblocks/4)) {
1074 if (nblocks > (pcyl * nhead)) {
1075 err_print("Mode sense page(3) reports nsect value"
1076 " as %d, adjusting it to %llu\n",
1077 nsect, nblocks / (pcyl * nhead));
1078 nsect = nblocks / (pcyl * nhead);
1079 } else {
1080 /* convert capacity to nsect * nhead * pcyl */
1081 err_print("\nWARNING: Disk geometry is based on "
1082 "capacity data.\n\n");
1083 compute_chs_values(tblocks, nblocks, &pcyl, &nhead,
1084 &nsect);
1085 ncyl = pcyl - acyl;
1086 if (option_msg && diag_msg) {
1087 err_print("Geometry:(after adjustment)\n");
1088 err_print(" pcyl: %u\n", pcyl);
1089 err_print(" ncyl: %u\n", ncyl);
1090 err_print(" heads: %u\n", nhead);
1091 err_print(" nsects: %u\n", nsect);
1092 err_print(" acyl: %u\n", acyl);
1094 #if defined(_SUNOS_VTOC_16)
1095 err_print(" bcyl: %u\n", bcyl);
1096 #endif
1098 err_print(" rpm: %d\n", rpm);
1099 err_print(" nblocks: %llu\n", nblocks);
1105 * Some drives report their physical geometry such that
1106 * it is greater than the actual capacity. Adjust the
1107 * geometry to allow for this, so we don't run off
1108 * the end of the disk.
1110 if (((diskaddr_t)pcyl * nhead * nsect) > nblocks) {
1111 uint_t p = pcyl;
1112 if (option_msg && diag_msg) {
1113 err_print("Computed capacity (%llu) exceeds actual "
1114 "disk capacity (%llu)\n",
1115 (diskaddr_t)pcyl * nhead * nsect, nblocks);
1117 do {
1118 pcyl--;
1119 } while (((diskaddr_t)pcyl * nhead * nsect) > nblocks);
1121 if (can_prompt && expert_mode && !option_f) {
1123 * Try to adjust nsect instead of pcyl to see if we
1124 * can optimize. For compatability reasons do this
1125 * only in expert mode (refer to bug 1144812).
1127 uint_t n = nsect;
1128 do {
1129 n--;
1130 } while (((diskaddr_t)p * nhead * n) > nblocks);
1131 if (((diskaddr_t)p * nhead * n) >
1132 ((diskaddr_t)pcyl * nhead * nsect)) {
1133 u_ioparam_t ioparam;
1134 int deflt = 1;
1136 * Ask the user for a choice here.
1138 ioparam.io_bounds.lower = 1;
1139 ioparam.io_bounds.upper = 2;
1140 err_print("1. Capacity = %llu, with pcyl = %u "
1141 "nhead = %u nsect = %u\n",
1142 ((diskaddr_t)pcyl * nhead * nsect),
1143 pcyl, nhead, nsect);
1144 err_print("2. Capacity = %llu, with pcyl = %u "
1145 "nhead = %u nsect = %u\n",
1146 ((diskaddr_t)p * nhead * n),
1147 p, nhead, n);
1148 if (input(FIO_INT, "Select one of the above "
1149 "choices ", ':', &ioparam,
1150 &deflt, DATA_INPUT) == 2) {
1151 pcyl = p;
1152 nsect = n;
1158 #if defined(_SUNOS_VTOC_8)
1160 * Finally, we need to make sure we don't overflow any of the
1161 * fields in our disk label. To do this we need to `square
1162 * the box' so to speak. We will lose bits here.
1165 if ((pcyl > MAXIMUM_NO_CYLINDERS &&
1166 ((nsect > MAXIMUM_NO_SECTORS) ||
1167 (nhead > MAXIMUM_NO_HEADS))) ||
1168 ((nsect > MAXIMUM_NO_SECTORS) &&
1169 (nhead > MAXIMUM_NO_HEADS))) {
1170 err_print("This disk is too big to label. "
1171 " You will lose some blocks.\n");
1173 if ((pcyl > MAXIMUM_NO_CYLINDERS) ||
1174 (nsect > MAXIMUM_NO_SECTORS) ||
1175 (nhead > MAXIMUM_NO_HEADS)) {
1176 u_ioparam_t ioparam;
1177 int order;
1178 char msg[256];
1180 order = ((pcyl > nhead)<<2) |
1181 ((pcyl > nsect)<<1) |
1182 (nhead > nsect);
1183 switch (order) {
1184 case 0x7: /* pcyl > nhead > nsect */
1185 nblocks =
1186 square_box(nblocks,
1187 &pcyl, MAXIMUM_NO_CYLINDERS,
1188 &nhead, MAXIMUM_NO_HEADS,
1189 &nsect, MAXIMUM_NO_SECTORS);
1190 break;
1191 case 0x6: /* pcyl > nsect > nhead */
1192 nblocks =
1193 square_box(nblocks,
1194 &pcyl, MAXIMUM_NO_CYLINDERS,
1195 &nsect, MAXIMUM_NO_SECTORS,
1196 &nhead, MAXIMUM_NO_HEADS);
1197 break;
1198 case 0x4: /* nsect > pcyl > nhead */
1199 nblocks =
1200 square_box(nblocks,
1201 &nsect, MAXIMUM_NO_SECTORS,
1202 &pcyl, MAXIMUM_NO_CYLINDERS,
1203 &nhead, MAXIMUM_NO_HEADS);
1204 break;
1205 case 0x0: /* nsect > nhead > pcyl */
1206 nblocks =
1207 square_box(nblocks,
1208 &nsect, MAXIMUM_NO_SECTORS,
1209 &nhead, MAXIMUM_NO_HEADS,
1210 &pcyl, MAXIMUM_NO_CYLINDERS);
1211 break;
1212 case 0x3: /* nhead > pcyl > nsect */
1213 nblocks =
1214 square_box(nblocks,
1215 &nhead, MAXIMUM_NO_HEADS,
1216 &pcyl, MAXIMUM_NO_CYLINDERS,
1217 &nsect, MAXIMUM_NO_SECTORS);
1218 break;
1219 case 0x1: /* nhead > nsect > pcyl */
1220 nblocks =
1221 square_box(nblocks,
1222 &nhead, MAXIMUM_NO_HEADS,
1223 &nsect, MAXIMUM_NO_SECTORS,
1224 &pcyl, MAXIMUM_NO_CYLINDERS);
1225 break;
1226 default:
1227 /* How did we get here? */
1228 impossible("label overflow adjustment");
1230 /* Do something useful */
1231 nblocks =
1232 square_box(nblocks,
1233 &nhead, MAXIMUM_NO_HEADS,
1234 &nsect, MAXIMUM_NO_SECTORS,
1235 &pcyl, MAXIMUM_NO_CYLINDERS);
1236 break;
1238 if (option_msg && diag_msg &&
1239 (capacity->sc_capacity + 1 != nblocks)) {
1240 err_print("After adjusting geometry you lost"
1241 " %llu of %llu blocks.\n",
1242 (capacity->sc_capacity + 1 - nblocks),
1243 capacity->sc_capacity + 1);
1245 while (can_prompt && expert_mode && !option_f) {
1246 int deflt = 1;
1249 * Allow user to modify this by hand if desired.
1251 (void) sprintf(msg,
1252 "\nGeometry: %u heads, %u sectors %u cylinders"
1253 " result in %llu out of %llu blocks.\n"
1254 "Do you want to modify the device geometry",
1255 nhead, nsect, pcyl,
1256 nblocks, capacity->sc_capacity + 1);
1258 ioparam.io_charlist = confirm_list;
1259 if (input(FIO_MSTR, msg, '?', &ioparam,
1260 &deflt, DATA_INPUT) != 0)
1261 break;
1263 ioparam.io_bounds.lower = MINIMUM_NO_HEADS;
1264 ioparam.io_bounds.upper = MAXIMUM_NO_HEADS;
1265 nhead = input(FIO_INT, "Number of heads", ':',
1266 &ioparam, (int *)&nhead, DATA_INPUT);
1267 ioparam.io_bounds.lower = MINIMUM_NO_SECTORS;
1268 ioparam.io_bounds.upper = MAXIMUM_NO_SECTORS;
1269 nsect = input(FIO_INT,
1270 "Number of sectors per track",
1271 ':', &ioparam, (int *)&nsect, DATA_INPUT);
1272 ioparam.io_bounds.lower = SUN_MIN_CYL;
1273 ioparam.io_bounds.upper = MAXIMUM_NO_CYLINDERS;
1274 pcyl = input(FIO_INT, "Number of cylinders",
1275 ':', &ioparam, (int *)&pcyl, DATA_INPUT);
1276 nblocks = (diskaddr_t)nhead * nsect * pcyl;
1277 if (nblocks > capacity->sc_capacity + 1) {
1278 err_print("Warning: %llu blocks exceeds "
1279 "disk capacity of %llu blocks\n",
1280 nblocks,
1281 capacity->sc_capacity + 1);
1285 #endif /* defined(_SUNOS_VTOC_8) */
1287 ncyl = pcyl - acyl;
1289 if (option_msg && diag_msg) {
1290 err_print("\nGeometry after adjusting for capacity:\n");
1291 err_print(" pcyl: %u\n", pcyl);
1292 err_print(" ncyl: %u\n", ncyl);
1293 err_print(" heads: %u\n", nhead);
1294 err_print(" nsects: %u\n", nsect);
1295 err_print(" acyl: %u\n", acyl);
1296 err_print(" rpm: %d\n", rpm);
1299 (void) memset((char *)label, 0, sizeof (struct dk_label));
1301 label->dkl_magic = DKL_MAGIC;
1303 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
1304 "%s cyl %u alt %u hd %u sec %u",
1305 disk_name, ncyl, acyl, nhead, nsect);
1307 label->dkl_pcyl = pcyl;
1308 label->dkl_ncyl = ncyl;
1309 label->dkl_acyl = acyl;
1310 label->dkl_nhead = nhead;
1311 label->dkl_nsect = nsect;
1312 label->dkl_apc = 0;
1313 label->dkl_intrlv = 1;
1314 label->dkl_rpm = rpm;
1316 #if defined(_FIRMWARE_NEEDS_FDISK)
1317 if (auto_solaris_part(label) == -1)
1318 goto err;
1319 ncyl = label->dkl_ncyl;
1320 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
1323 if (!build_default_partition(label, DKC_SCSI_CCS)) {
1324 goto err;
1327 (void) checksum(label, CK_MAKESUM);
1330 * Find an existing disk type defined for this disk.
1331 * For this to work, both the name and geometry must
1332 * match. If there is no such type, but there already
1333 * is a disk defined with that name, but with a different
1334 * geometry, construct a new generic disk name out of
1335 * the inquiry information. Whatever name we're
1336 * finally using, if there's no such disk type defined,
1337 * build a new disk definition.
1339 if ((disk = find_scsi_disk_type(disk_name, label)) == NULL) {
1340 if (find_scsi_disk_by_name(disk_name) != NULL) {
1341 char old_name[DISK_NAME_MAX];
1342 (void) strcpy(old_name, disk_name);
1343 (void) get_generic_disk_name(disk_name,
1344 inquiry);
1345 if (option_msg && diag_msg) {
1346 err_print(
1347 "Changing disk type name from '%s' to '%s'\n", old_name, disk_name);
1349 (void) snprintf(label->dkl_asciilabel,
1350 sizeof (label->dkl_asciilabel),
1351 "%s cyl %u alt %u hd %u sec %u",
1352 disk_name, ncyl, acyl, nhead, nsect);
1353 (void) checksum(label, CK_MAKESUM);
1354 disk = find_scsi_disk_type(disk_name, label);
1356 if (disk == NULL) {
1357 disk = new_scsi_disk_type(fd, disk_name, label);
1358 if (disk == NULL)
1359 goto err;
1363 return (disk);
1365 err:
1366 if (option_msg && diag_msg) {
1367 err_print(
1368 "Configuration via generic SCSI-2 information failed\n");
1370 return (NULL);
1374 /*ARGSUSED*/
1375 static int
1376 use_existing_disk_type(
1377 int fd,
1378 int can_prompt,
1379 struct dk_label *label,
1380 struct scsi_inquiry *inquiry,
1381 struct disk_type *disk_type,
1382 struct scsi_capacity_16 *capacity)
1384 int pcyl;
1385 int acyl;
1386 int nhead;
1387 int nsect;
1388 int rpm;
1391 * Construct a new label out of the format.dat
1393 pcyl = disk_type->dtype_pcyl;
1394 acyl = disk_type->dtype_acyl;
1395 ncyl = disk_type->dtype_ncyl;
1396 nhead = disk_type->dtype_nhead;
1397 nsect = disk_type->dtype_nsect;
1398 rpm = disk_type->dtype_rpm;
1400 if (option_msg && diag_msg) {
1401 err_print("Format.dat geometry:\n");
1402 err_print(" pcyl: %u\n", pcyl);
1403 err_print(" heads: %u\n", nhead);
1404 err_print(" nsects: %u\n", nsect);
1405 err_print(" acyl: %u\n", acyl);
1406 err_print(" rpm: %d\n", rpm);
1409 (void) memset((char *)label, 0, sizeof (struct dk_label));
1411 label->dkl_magic = DKL_MAGIC;
1413 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
1414 "%s cyl %u alt %u hd %u sec %u",
1415 disk_type->dtype_asciilabel,
1416 ncyl, acyl, nhead, nsect);
1418 label->dkl_pcyl = pcyl;
1419 label->dkl_ncyl = ncyl;
1420 label->dkl_acyl = acyl;
1421 label->dkl_nhead = nhead;
1422 label->dkl_nsect = nsect;
1423 label->dkl_apc = 0;
1424 label->dkl_intrlv = 1;
1425 label->dkl_rpm = rpm;
1427 if (!build_default_partition(label, DKC_SCSI_CCS)) {
1428 goto err;
1431 (void) checksum(label, CK_MAKESUM);
1432 return (1);
1434 err:
1435 if (option_msg && diag_msg) {
1436 err_print(
1437 "Configuration via format.dat geometry failed\n");
1439 return (0);
1443 build_default_partition(
1444 struct dk_label *label,
1445 int ctrl_type)
1447 int i;
1448 int ncyls[NDKMAP];
1449 diskaddr_t nblks;
1450 int cyl;
1451 struct dk_vtoc *vtoc;
1452 struct part_table *pt;
1453 struct default_partitions *dpt;
1454 diskaddr_t capacity;
1455 int freecyls;
1456 int blks_per_cyl;
1457 int ncyl;
1461 * Install a default vtoc
1463 vtoc = &label->dkl_vtoc;
1464 vtoc->v_version = V_VERSION;
1465 vtoc->v_nparts = NDKMAP;
1466 vtoc->v_sanity = VTOC_SANE;
1468 for (i = 0; i < NDKMAP; i++) {
1469 vtoc->v_part[i].p_tag = default_vtoc_map[i].p_tag;
1470 vtoc->v_part[i].p_flag = default_vtoc_map[i].p_flag;
1474 * Find a partition that matches this disk. Capacity
1475 * is in integral number of megabytes.
1477 capacity = ((diskaddr_t)(label->dkl_ncyl) * label->dkl_nhead *
1478 label->dkl_nsect) / (diskaddr_t)((1024 * 1024) / cur_blksz);
1479 dpt = default_partitions;
1480 for (i = 0; i < DEFAULT_PARTITION_TABLE_SIZE; i++, dpt++) {
1481 if (capacity >= dpt->min_capacity &&
1482 capacity < dpt->max_capacity) {
1483 break;
1486 if (i == DEFAULT_PARTITION_TABLE_SIZE) {
1487 if (option_msg && diag_msg) {
1488 err_print("No matching default partition (%llu)\n",
1489 capacity);
1491 return (0);
1493 pt = dpt->part_table;
1496 * Go through default partition table, finding fixed
1497 * sized entries.
1499 freecyls = label->dkl_ncyl;
1500 blks_per_cyl = label->dkl_nhead * label->dkl_nsect;
1501 for (i = 0; i < NDKMAP; i++) {
1502 if (pt->partitions[i] == HOG || pt->partitions[i] == 0) {
1503 ncyls[i] = 0;
1504 } else {
1506 * Calculate number of cylinders necessary
1507 * for specified size, rounding up to
1508 * the next greatest integral number of
1509 * cylinders. Always give what they
1510 * asked or more, never less.
1512 nblks = pt->partitions[i] * ((1024*1024)/cur_blksz);
1513 nblks += (blks_per_cyl - 1);
1514 ncyls[i] = nblks / blks_per_cyl;
1515 freecyls -= ncyls[i];
1519 if (freecyls < 0) {
1520 if (option_msg && diag_msg) {
1521 for (i = 0; i < NDKMAP; i++) {
1522 if (ncyls[i] == 0)
1523 continue;
1524 err_print("Partition %d: %u cyls\n",
1525 i, ncyls[i]);
1527 err_print("Free cylinders exhausted (%d)\n",
1528 freecyls);
1530 return (0);
1532 #if defined(i386)
1534 * Set the default boot partition to 1 cylinder
1536 ncyls[8] = 1;
1537 freecyls -= 1;
1540 * If current disk type is not a SCSI disk,
1541 * set the default alternates partition to 2 cylinders
1543 if (ctrl_type != DKC_SCSI_CCS) {
1544 ncyls[9] = 2;
1545 freecyls -= 2;
1547 #endif /* defined(i386) */
1550 * Set the free hog partition to whatever space remains.
1551 * It's an error to have more than one HOG partition,
1552 * but we don't verify that here.
1554 for (i = 0; i < NDKMAP; i++) {
1555 if (pt->partitions[i] == HOG) {
1556 assert(ncyls[i] == 0);
1557 ncyls[i] = freecyls;
1558 break;
1563 * Error checking
1565 ncyl = 0;
1566 for (i = 0; i < NDKMAP; i++) {
1567 ncyl += ncyls[i];
1569 assert(ncyl == (label->dkl_ncyl));
1572 * Finally, install the partition in the label.
1574 cyl = 0;
1576 #if defined(_SUNOS_VTOC_16)
1577 for (i = NDKMAP/2; i < NDKMAP; i++) {
1578 if (i == 2 || ncyls[i] == 0)
1579 continue;
1580 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
1581 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
1582 cyl += ncyls[i];
1584 for (i = 0; i < NDKMAP/2; i++) {
1586 #elif defined(_SUNOS_VTOC_8)
1587 for (i = 0; i < NDKMAP; i++) {
1589 #else
1590 #error No VTOC format defined.
1591 #endif /* defined(_SUNOS_VTOC_16) */
1593 if (i == 2 || ncyls[i] == 0) {
1594 #if defined(_SUNOS_VTOC_8)
1595 if (i != 2) {
1596 label->dkl_map[i].dkl_cylno = 0;
1597 label->dkl_map[i].dkl_nblk = 0;
1599 #endif
1600 continue;
1602 #if defined(_SUNOS_VTOC_8)
1603 label->dkl_map[i].dkl_cylno = cyl;
1604 label->dkl_map[i].dkl_nblk = ncyls[i] * blks_per_cyl;
1605 #elif defined(_SUNOS_VTOC_16)
1606 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
1607 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
1609 #else
1610 #error No VTOC format defined.
1611 #endif /* defined(_SUNOS_VTOC_8) */
1613 cyl += ncyls[i];
1617 * Set the whole disk partition
1619 #if defined(_SUNOS_VTOC_8)
1620 label->dkl_map[2].dkl_cylno = 0;
1621 label->dkl_map[2].dkl_nblk =
1622 label->dkl_ncyl * label->dkl_nhead * label->dkl_nsect;
1624 #elif defined(_SUNOS_VTOC_16)
1625 label->dkl_vtoc.v_part[2].p_start = 0;
1626 label->dkl_vtoc.v_part[2].p_size =
1627 (label->dkl_ncyl + label->dkl_acyl) * label->dkl_nhead *
1628 label->dkl_nsect;
1629 #else
1630 #error No VTOC format defined.
1631 #endif /* defined(_SUNOS_VTOC_8) */
1634 if (option_msg && diag_msg) {
1635 float scaled;
1636 err_print("\n");
1637 for (i = 0; i < NDKMAP; i++) {
1638 #if defined(_SUNOS_VTOC_8)
1639 if (label->dkl_map[i].dkl_nblk == 0)
1641 #elif defined(_SUNOS_VTOC_16)
1642 if (label->dkl_vtoc.v_part[i].p_size == 0)
1644 #else
1645 #error No VTOC format defined.
1646 #endif /* defined(_SUNOS_VTOC_8) */
1648 continue;
1649 err_print("Partition %d: ", i);
1650 #if defined(_SUNOS_VTOC_8)
1651 scaled = bn2mb(label->dkl_map[i].dkl_nblk);
1653 #elif defined(_SUNOS_VTOC_16)
1655 scaled = bn2mb(label->dkl_vtoc.v_part[i].p_size);
1656 #else
1657 #error No VTOC format defined.
1658 #endif /* defined(_SUNOS_VTOC_8) */
1660 if (scaled > 1024.0) {
1661 err_print("%6.2fGB ", scaled/1024.0);
1662 } else {
1663 err_print("%6.2fMB ", scaled);
1665 #if defined(_SUNOS_VTOC_8)
1666 err_print(" %6d cylinders\n",
1667 label->dkl_map[i].dkl_nblk/blks_per_cyl);
1668 #elif defined(_SUNOS_VTOC_16)
1669 err_print(" %6d cylinders\n",
1670 label->dkl_vtoc.v_part[i].p_size/blks_per_cyl);
1671 #else
1672 #error No VTOC format defined.
1673 #endif /* defined(_SUNOS_VTOC_8) */
1676 err_print("\n");
1679 return (1);
1685 * Find an existing scsi disk definition by this name,
1686 * if possible.
1688 static struct disk_type *
1689 find_scsi_disk_type(
1690 char *disk_name,
1691 struct dk_label *label)
1693 struct ctlr_type *ctlr;
1694 struct disk_type *dp;
1696 ctlr = find_scsi_ctlr_type();
1697 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
1698 if (dp->dtype_asciilabel) {
1699 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0) &&
1700 dp->dtype_pcyl == label->dkl_pcyl &&
1701 dp->dtype_ncyl == label->dkl_ncyl &&
1702 dp->dtype_acyl == label->dkl_acyl &&
1703 dp->dtype_nhead == label->dkl_nhead &&
1704 dp->dtype_nsect == label->dkl_nsect) {
1705 return (dp);
1710 return (NULL);
1715 * Find an existing scsi disk definition by this name,
1716 * if possible.
1718 static struct disk_type *
1719 find_scsi_disk_by_name(
1720 char *disk_name)
1722 struct ctlr_type *ctlr;
1723 struct disk_type *dp;
1725 ctlr = find_scsi_ctlr_type();
1726 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
1727 if (dp->dtype_asciilabel) {
1728 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0)) {
1729 return (dp);
1734 return (NULL);
1739 * Return a pointer to the ctlr_type structure for SCSI
1740 * disks. This list is built into the program, so there's
1741 * no chance of not being able to find it, unless someone
1742 * totally mangles the code.
1744 static struct ctlr_type *
1745 find_scsi_ctlr_type()
1747 struct mctlr_list *mlp;
1749 mlp = controlp;
1751 while (mlp != NULL) {
1752 if (mlp->ctlr_type->ctype_ctype == DKC_SCSI_CCS) {
1753 return (mlp->ctlr_type);
1755 mlp = mlp->next;
1758 impossible("no SCSI controller type");
1760 return (NULL);
1766 * Return a pointer to the scsi ctlr_info structure. This
1767 * structure is allocated the first time format sees a
1768 * disk on this controller, so it must be present.
1770 static struct ctlr_info *
1771 find_scsi_ctlr_info(
1772 struct dk_cinfo *dkinfo)
1774 struct ctlr_info *ctlr;
1776 if (dkinfo->dki_ctype != DKC_SCSI_CCS) {
1777 return (NULL);
1780 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) {
1781 if (ctlr->ctlr_addr == dkinfo->dki_addr &&
1782 ctlr->ctlr_space == dkinfo->dki_space &&
1783 ctlr->ctlr_ctype->ctype_ctype == DKC_SCSI_CCS) {
1784 return (ctlr);
1788 impossible("no SCSI controller info");
1790 return (NULL);
1795 static struct disk_type *
1796 new_scsi_disk_type(
1797 int fd,
1798 char *disk_name,
1799 struct dk_label *label)
1801 struct disk_type *dp;
1802 struct disk_type *disk;
1803 struct ctlr_info *ctlr;
1804 struct dk_cinfo dkinfo;
1805 struct partition_info *part;
1806 struct partition_info *pt;
1807 struct disk_info *disk_info;
1808 int i;
1811 * Get the disk controller info for this disk
1813 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
1814 if (option_msg && diag_msg) {
1815 err_print("DKIOCINFO failed\n");
1817 return (NULL);
1821 * Find the ctlr_info for this disk.
1823 ctlr = find_scsi_ctlr_info(&dkinfo);
1826 * Allocate a new disk type for the SCSI controller.
1828 disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
1831 * Find the disk_info instance for this disk.
1833 disk_info = find_scsi_disk_info(&dkinfo);
1836 * The controller and the disk should match.
1838 assert(disk_info->disk_ctlr == ctlr);
1841 * Link the disk into the list of disks
1843 dp = ctlr->ctlr_ctype->ctype_dlist;
1844 if (dp == NULL) {
1845 ctlr->ctlr_ctype->ctype_dlist = disk;
1846 } else {
1847 while (dp->dtype_next != NULL) {
1848 dp = dp->dtype_next;
1850 dp->dtype_next = disk;
1852 disk->dtype_next = NULL;
1855 * Allocate and initialize the disk name.
1857 disk->dtype_asciilabel = alloc_string(disk_name);
1860 * Initialize disk geometry info
1862 disk->dtype_pcyl = label->dkl_pcyl;
1863 disk->dtype_ncyl = label->dkl_ncyl;
1864 disk->dtype_acyl = label->dkl_acyl;
1865 disk->dtype_nhead = label->dkl_nhead;
1866 disk->dtype_nsect = label->dkl_nsect;
1867 disk->dtype_rpm = label->dkl_rpm;
1870 * Attempt to match the partition map in the label
1871 * with a know partition for this disk type.
1873 for (part = disk->dtype_plist; part; part = part->pinfo_next) {
1874 if (parts_match(label, part)) {
1875 break;
1880 * If no match was made, we need to create a partition
1881 * map for this disk.
1883 if (part == NULL) {
1884 part = (struct partition_info *)
1885 zalloc(sizeof (struct partition_info));
1886 pt = disk->dtype_plist;
1887 if (pt == NULL) {
1888 disk->dtype_plist = part;
1889 } else {
1890 while (pt->pinfo_next != NULL) {
1891 pt = pt->pinfo_next;
1893 pt->pinfo_next = part;
1895 part->pinfo_next = NULL;
1898 * Set up the partition name
1900 part->pinfo_name = alloc_string("default");
1903 * Fill in the partition info from the label
1905 for (i = 0; i < NDKMAP; i++) {
1907 #if defined(_SUNOS_VTOC_8)
1908 part->pinfo_map[i] = label->dkl_map[i];
1910 #elif defined(_SUNOS_VTOC_16)
1911 part->pinfo_map[i].dkl_cylno =
1912 label->dkl_vtoc.v_part[i].p_start /
1913 ((blkaddr32_t)(disk->dtype_nhead *
1914 disk->dtype_nsect - apc));
1915 part->pinfo_map[i].dkl_nblk =
1916 label->dkl_vtoc.v_part[i].p_size;
1917 #else
1918 #error No VTOC format defined.
1919 #endif /* defined(_SUNOS_VTOC_8) */
1926 * Use the VTOC if valid, or install a default
1928 if (label->dkl_vtoc.v_version == V_VERSION) {
1929 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume,
1930 LEN_DKL_VVOL);
1931 part->vtoc = label->dkl_vtoc;
1932 } else {
1933 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL);
1934 set_vtoc_defaults(part);
1938 * Link the disk to the partition map
1940 disk_info->disk_parts = part;
1942 return (disk);
1947 * Delete a disk type from disk type list.
1950 delete_disk_type(struct disk_type *disk_type)
1952 struct ctlr_type *ctlr;
1953 struct disk_type *dp, *disk;
1955 if (cur_ctype->ctype_ctype == DKC_DIRECT ||
1956 cur_ctype->ctype_ctype == DKC_VBD ||
1957 cur_ctype->ctype_ctype == DKC_BLKDEV)
1958 ctlr = find_ctlr_type(cur_ctype->ctype_ctype);
1959 else
1960 ctlr = find_scsi_ctlr_type();
1961 if (ctlr == NULL || ctlr->ctype_dlist == NULL) {
1962 return (-1);
1965 disk = ctlr->ctype_dlist;
1966 if (disk == disk_type) {
1967 ctlr->ctype_dlist = disk->dtype_next;
1968 if (cur_label == L_TYPE_EFI)
1969 free(disk->dtype_plist->etoc);
1970 free(disk->dtype_plist);
1971 free(disk->vendor);
1972 free(disk->product);
1973 free(disk->revision);
1974 free(disk);
1975 return (0);
1976 } else {
1977 for (dp = disk->dtype_next; dp != NULL;
1978 disk = disk->dtype_next, dp = dp->dtype_next) {
1979 if (dp == disk_type) {
1980 disk->dtype_next = dp->dtype_next;
1981 if (cur_label == L_TYPE_EFI)
1982 free(dp->dtype_plist->etoc);
1983 free(dp->dtype_plist);
1984 free(dp->vendor);
1985 free(dp->product);
1986 free(dp->revision);
1987 free(dp);
1988 return (0);
1991 return (-1);
1996 static struct disk_info *
1997 find_scsi_disk_info(
1998 struct dk_cinfo *dkinfo)
2000 struct disk_info *disk;
2001 struct dk_cinfo *dp;
2003 for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
2004 assert(dkinfo->dki_ctype == DKC_SCSI_CCS);
2005 dp = &disk->disk_dkinfo;
2006 if (dp->dki_ctype == dkinfo->dki_ctype &&
2007 dp->dki_cnum == dkinfo->dki_cnum &&
2008 dp->dki_unit == dkinfo->dki_unit &&
2009 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) {
2010 return (disk);
2014 impossible("No SCSI disk info instance\n");
2016 return (NULL);
2020 static char *
2021 get_sun_disk_name(
2022 char *disk_name,
2023 struct scsi_inquiry *inquiry)
2026 * Extract the sun name of the disk
2028 (void) memset(disk_name, 0, DISK_NAME_MAX);
2029 (void) memcpy(disk_name, (char *)&inquiry->inq_pid[9], 7);
2031 return (disk_name);
2035 char *
2036 get_generic_disk_name(
2037 char *disk_name,
2038 struct scsi_inquiry *inquiry)
2040 char *p;
2042 (void) memset(disk_name, 0, DISK_NAME_MAX);
2043 p = strcopy(disk_name, inquiry->inq_vid,
2044 sizeof (inquiry->inq_vid));
2045 *p++ = '-';
2046 p = strcopy(p, inquiry->inq_pid, sizeof (inquiry->inq_pid));
2047 *p++ = '-';
2048 p = strcopy(p, inquiry->inq_revision,
2049 sizeof (inquiry->inq_revision));
2051 return (disk_name);
2055 * Copy a string of characters from src to dst, for at
2056 * most n bytes. Strip all leading and trailing spaces,
2057 * and stop if there are any non-printable characters.
2058 * Return ptr to the next character to be filled.
2060 static char *
2061 strcopy(
2062 char *dst,
2063 char *src,
2064 int n)
2066 int i;
2068 while (*src == ' ' && n > 0) {
2069 src++;
2070 n--;
2073 for (i = 0; n-- > 0 && isascii(*src) && isprint(*src); src++) {
2074 if (*src == ' ') {
2075 i++;
2076 } else {
2077 while (i-- > 0)
2078 *dst++ = ' ';
2079 *dst++ = *src;
2083 *dst = 0;
2084 return (dst);
2088 * adjust disk geometry.
2089 * This is used when disk reports a disk geometry page having
2090 * no of physical cylinders is < 3 which is the minimum required
2091 * by Solaris (2 for storing labels and at least one as a data
2092 * cylinder )
2095 adjust_disk_geometry(diskaddr_t capacity, uint_t *cyl, uint_t *nhead,
2096 uint_t *nsect)
2098 uint_t lcyl = *cyl;
2099 uint_t lnhead = *nhead;
2100 uint_t lnsect = *nsect;
2102 assert(lcyl < SUN_MIN_CYL);
2105 * reduce nsect by 2 for each iteration and re-calculate
2106 * the number of cylinders.
2108 while (lnsect > MINIMUM_NO_SECTORS &&
2109 lcyl < MINIMUM_NO_CYLINDERS) {
2111 * make sure that we do not go below MINIMUM_NO_SECTORS.
2113 lnsect = max(MINIMUM_NO_SECTORS, lnsect / 2);
2114 lcyl = (capacity) / (lnhead * lnsect);
2117 * If the geometry still does not satisfy
2118 * MINIMUM_NO_CYLINDERS then try to reduce the
2119 * no of heads.
2121 while (lnhead > MINIMUM_NO_HEADS &&
2122 lcyl < MINIMUM_NO_CYLINDERS) {
2123 lnhead = max(MINIMUM_NO_HEADS, lnhead / 2);
2124 lcyl = (capacity) / (lnhead * lnsect);
2127 * now we should have atleast SUN_MIN_CYL cylinders.
2128 * If we still do not get SUN_MIN_CYL with MINIMUM_NO_HEADS
2129 * and MINIMUM_NO_HEADS then return error.
2131 if (lcyl < SUN_MIN_CYL)
2132 return (1);
2133 else {
2134 *cyl = lcyl;
2135 *nhead = lnhead;
2136 *nsect = lnsect;
2137 return (0);
2141 #if defined(_SUNOS_VTOC_8)
2143 * Reduce the size of one dimention below a specified
2144 * limit with a minimum loss of volume. Dimenstions are
2145 * assumed to be passed in form the largest value (the one
2146 * that needs to be reduced) to the smallest value. The
2147 * values will be twiddled until they are all less than or
2148 * equal to their limit. Returns the number in the new geometry.
2150 static diskaddr_t
2151 square_box(
2152 diskaddr_t capacity,
2153 uint_t *dim1, uint_t lim1,
2154 uint_t *dim2, uint_t lim2,
2155 uint_t *dim3, uint_t lim3)
2157 uint_t i;
2160 * Although the routine should work with any ordering of
2161 * parameters, it's most efficient if they are passed in
2162 * in decreasing magnitude.
2164 assert(*dim1 >= *dim2);
2165 assert(*dim2 >= *dim3);
2168 * This is done in a very arbitrary manner. We could try to
2169 * find better values but I can't come up with a method that
2170 * would run in a reasonable amount of time. That could take
2171 * approximately 65535 * 65535 iterations of a dozen flops each
2172 * or well over 4G flops.
2174 * First:
2176 * Let's see how far we can go with bitshifts w/o losing
2177 * any blocks.
2180 for (i = 0; (((*dim1)>>i)&1) == 0 && ((*dim1)>>i) > lim1; i++)
2182 if (i) {
2183 *dim1 = ((*dim1)>>i);
2184 *dim3 = ((*dim3)<<i);
2187 if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) {
2188 double d[4];
2191 * Second:
2193 * Set the highest value at its limit then calculate errors,
2194 * adjusting the 2nd highest value (we get better resolution
2195 * that way).
2197 d[1] = lim1;
2198 d[3] = *dim3;
2199 d[2] = (double)capacity/(d[1]*d[3]);
2202 * If we overflowed the middle term, set it to its limit and
2203 * chose a new low term.
2205 if (d[2] > lim2) {
2206 d[2] = lim2;
2207 d[3] = (double)capacity/(d[1]*d[2]);
2210 * Convert to integers.
2212 *dim1 = (int)d[1];
2213 *dim2 = (int)d[2];
2214 *dim3 = (int)d[3];
2217 * Fixup any other possible problems.
2218 * If this happens, we need a new disklabel format.
2220 if (*dim1 > lim1) *dim1 = lim1;
2221 if (*dim2 > lim2) *dim2 = lim2;
2222 if (*dim3 > lim3) *dim3 = lim3;
2223 return (*dim1 * *dim2 * *dim3);
2225 #endif /* defined(_SUNOS_VTOC_8) */
2228 * Calculate CHS values based on the capacity data.
2230 * NOTE: This function is same as cmlb_convert_geomerty() function in
2231 * cmlb kernel module.
2233 static void
2234 compute_chs_values(diskaddr_t total_capacity, diskaddr_t usable_capacity,
2235 uint_t *pcylp, uint_t *nheadp, uint_t *nsectp)
2238 /* Unlabeled SCSI floppy device */
2239 if (total_capacity < 160) {
2240 /* Less than 80K */
2241 *nheadp = 1;
2242 *pcylp = total_capacity;
2243 *nsectp = 1;
2244 return;
2245 } else if (total_capacity <= 0x1000) {
2246 *nheadp = 2;
2247 *pcylp = 80;
2248 *nsectp = total_capacity / (80 * 2);
2249 return;
2253 * For all devices we calculate cylinders using the heads and sectors
2254 * we assign based on capacity of the device. The algorithm is
2255 * designed to be compatible with the way other operating systems
2256 * lay out fdisk tables for X86 and to insure that the cylinders never
2257 * exceed 65535 to prevent problems with X86 ioctls that report
2258 * geometry.
2259 * For some smaller disk sizes we report geometry that matches those
2260 * used by X86 BIOS usage. For larger disks, we use SPT that are
2261 * multiples of 63, since other OSes that are not limited to 16-bits
2262 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
2264 * The following table (in order) illustrates some end result
2265 * calculations:
2267 * Maximum number of blocks nhead nsect
2269 * 2097152 (1GB) 64 32
2270 * 16777216 (8GB) 128 32
2271 * 1052819775 (502.02GB) 255 63
2272 * 2105639550 (0.98TB) 255 126
2273 * 3158459325 (1.47TB) 255 189
2274 * 4211279100 (1.96TB) 255 252
2275 * 5264098875 (2.45TB) 255 315
2276 * ...
2279 if (total_capacity <= 0x200000) {
2280 *nheadp = 64;
2281 *nsectp = 32;
2282 } else if (total_capacity <= 0x01000000) {
2283 *nheadp = 128;
2284 *nsectp = 32;
2285 } else {
2286 *nheadp = 255;
2288 /* make nsect be smallest multiple of 63 */
2289 *nsectp = ((total_capacity +
2290 (UINT16_MAX * 255 * 63) - 1) /
2291 (UINT16_MAX * 255 * 63)) * 63;
2293 if (*nsectp == 0)
2294 *nsectp = (UINT16_MAX / 63) * 63;
2297 if (usable_capacity < total_capacity)
2298 *pcylp = usable_capacity / ((*nheadp) * (*nsectp));
2299 else
2300 *pcylp = total_capacity / ((*nheadp) * (*nsectp));