installboot: fix stage2 size check for MBR
[unleashed.git] / usr / src / cmd / format / auto_sense.c
blob05d88dc131b10a617805073fd2cc2a042e9817d7
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 ((struct disk_type *)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 ((struct disk_type *)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 ((struct ctlr_type *)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 ((struct ctlr_info *)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 ((struct disk_info *)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 ((struct disk_type *)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 ((struct disk_type *)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 ((struct disk_type *)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 ((struct disk_type *)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;
1459 #ifdef lint
1460 ctrl_type = ctrl_type;
1461 #endif
1464 * Install a default vtoc
1466 vtoc = &label->dkl_vtoc;
1467 vtoc->v_version = V_VERSION;
1468 vtoc->v_nparts = NDKMAP;
1469 vtoc->v_sanity = VTOC_SANE;
1471 for (i = 0; i < NDKMAP; i++) {
1472 vtoc->v_part[i].p_tag = default_vtoc_map[i].p_tag;
1473 vtoc->v_part[i].p_flag = default_vtoc_map[i].p_flag;
1477 * Find a partition that matches this disk. Capacity
1478 * is in integral number of megabytes.
1480 capacity = ((diskaddr_t)(label->dkl_ncyl) * label->dkl_nhead *
1481 label->dkl_nsect) / (diskaddr_t)((1024 * 1024) / cur_blksz);
1482 dpt = default_partitions;
1483 for (i = 0; i < DEFAULT_PARTITION_TABLE_SIZE; i++, dpt++) {
1484 if (capacity >= dpt->min_capacity &&
1485 capacity < dpt->max_capacity) {
1486 break;
1489 if (i == DEFAULT_PARTITION_TABLE_SIZE) {
1490 if (option_msg && diag_msg) {
1491 err_print("No matching default partition (%llu)\n",
1492 capacity);
1494 return (0);
1496 pt = dpt->part_table;
1499 * Go through default partition table, finding fixed
1500 * sized entries.
1502 freecyls = label->dkl_ncyl;
1503 blks_per_cyl = label->dkl_nhead * label->dkl_nsect;
1504 for (i = 0; i < NDKMAP; i++) {
1505 if (pt->partitions[i] == HOG || pt->partitions[i] == 0) {
1506 ncyls[i] = 0;
1507 } else {
1509 * Calculate number of cylinders necessary
1510 * for specified size, rounding up to
1511 * the next greatest integral number of
1512 * cylinders. Always give what they
1513 * asked or more, never less.
1515 nblks = pt->partitions[i] * ((1024*1024)/cur_blksz);
1516 nblks += (blks_per_cyl - 1);
1517 ncyls[i] = nblks / blks_per_cyl;
1518 freecyls -= ncyls[i];
1522 if (freecyls < 0) {
1523 if (option_msg && diag_msg) {
1524 for (i = 0; i < NDKMAP; i++) {
1525 if (ncyls[i] == 0)
1526 continue;
1527 err_print("Partition %d: %u cyls\n",
1528 i, ncyls[i]);
1530 err_print("Free cylinders exhausted (%d)\n",
1531 freecyls);
1533 return (0);
1535 #if defined(i386)
1537 * Set the default boot partition to 1 cylinder
1539 ncyls[8] = 1;
1540 freecyls -= 1;
1543 * If current disk type is not a SCSI disk,
1544 * set the default alternates partition to 2 cylinders
1546 if (ctrl_type != DKC_SCSI_CCS) {
1547 ncyls[9] = 2;
1548 freecyls -= 2;
1550 #endif /* defined(i386) */
1553 * Set the free hog partition to whatever space remains.
1554 * It's an error to have more than one HOG partition,
1555 * but we don't verify that here.
1557 for (i = 0; i < NDKMAP; i++) {
1558 if (pt->partitions[i] == HOG) {
1559 assert(ncyls[i] == 0);
1560 ncyls[i] = freecyls;
1561 break;
1566 * Error checking
1568 ncyl = 0;
1569 for (i = 0; i < NDKMAP; i++) {
1570 ncyl += ncyls[i];
1572 assert(ncyl == (label->dkl_ncyl));
1575 * Finally, install the partition in the label.
1577 cyl = 0;
1579 #if defined(_SUNOS_VTOC_16)
1580 for (i = NDKMAP/2; i < NDKMAP; i++) {
1581 if (i == 2 || ncyls[i] == 0)
1582 continue;
1583 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
1584 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
1585 cyl += ncyls[i];
1587 for (i = 0; i < NDKMAP/2; i++) {
1589 #elif defined(_SUNOS_VTOC_8)
1590 for (i = 0; i < NDKMAP; i++) {
1592 #else
1593 #error No VTOC format defined.
1594 #endif /* defined(_SUNOS_VTOC_16) */
1596 if (i == 2 || ncyls[i] == 0) {
1597 #if defined(_SUNOS_VTOC_8)
1598 if (i != 2) {
1599 label->dkl_map[i].dkl_cylno = 0;
1600 label->dkl_map[i].dkl_nblk = 0;
1602 #endif
1603 continue;
1605 #if defined(_SUNOS_VTOC_8)
1606 label->dkl_map[i].dkl_cylno = cyl;
1607 label->dkl_map[i].dkl_nblk = ncyls[i] * blks_per_cyl;
1608 #elif defined(_SUNOS_VTOC_16)
1609 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
1610 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
1612 #else
1613 #error No VTOC format defined.
1614 #endif /* defined(_SUNOS_VTOC_8) */
1616 cyl += ncyls[i];
1620 * Set the whole disk partition
1622 #if defined(_SUNOS_VTOC_8)
1623 label->dkl_map[2].dkl_cylno = 0;
1624 label->dkl_map[2].dkl_nblk =
1625 label->dkl_ncyl * label->dkl_nhead * label->dkl_nsect;
1627 #elif defined(_SUNOS_VTOC_16)
1628 label->dkl_vtoc.v_part[2].p_start = 0;
1629 label->dkl_vtoc.v_part[2].p_size =
1630 (label->dkl_ncyl + label->dkl_acyl) * label->dkl_nhead *
1631 label->dkl_nsect;
1632 #else
1633 #error No VTOC format defined.
1634 #endif /* defined(_SUNOS_VTOC_8) */
1637 if (option_msg && diag_msg) {
1638 float scaled;
1639 err_print("\n");
1640 for (i = 0; i < NDKMAP; i++) {
1641 #if defined(_SUNOS_VTOC_8)
1642 if (label->dkl_map[i].dkl_nblk == 0)
1644 #elif defined(_SUNOS_VTOC_16)
1645 if (label->dkl_vtoc.v_part[i].p_size == 0)
1647 #else
1648 #error No VTOC format defined.
1649 #endif /* defined(_SUNOS_VTOC_8) */
1651 continue;
1652 err_print("Partition %d: ", i);
1653 #if defined(_SUNOS_VTOC_8)
1654 scaled = bn2mb(label->dkl_map[i].dkl_nblk);
1656 #elif defined(_SUNOS_VTOC_16)
1658 scaled = bn2mb(label->dkl_vtoc.v_part[i].p_size);
1659 #else
1660 #error No VTOC format defined.
1661 #endif /* defined(_SUNOS_VTOC_8) */
1663 if (scaled > 1024.0) {
1664 err_print("%6.2fGB ", scaled/1024.0);
1665 } else {
1666 err_print("%6.2fMB ", scaled);
1668 #if defined(_SUNOS_VTOC_8)
1669 err_print(" %6d cylinders\n",
1670 label->dkl_map[i].dkl_nblk/blks_per_cyl);
1671 #elif defined(_SUNOS_VTOC_16)
1672 err_print(" %6d cylinders\n",
1673 label->dkl_vtoc.v_part[i].p_size/blks_per_cyl);
1674 #else
1675 #error No VTOC format defined.
1676 #endif /* defined(_SUNOS_VTOC_8) */
1679 err_print("\n");
1682 return (1);
1688 * Find an existing scsi disk definition by this name,
1689 * if possible.
1691 static struct disk_type *
1692 find_scsi_disk_type(
1693 char *disk_name,
1694 struct dk_label *label)
1696 struct ctlr_type *ctlr;
1697 struct disk_type *dp;
1699 ctlr = find_scsi_ctlr_type();
1700 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
1701 if (dp->dtype_asciilabel) {
1702 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0) &&
1703 dp->dtype_pcyl == label->dkl_pcyl &&
1704 dp->dtype_ncyl == label->dkl_ncyl &&
1705 dp->dtype_acyl == label->dkl_acyl &&
1706 dp->dtype_nhead == label->dkl_nhead &&
1707 dp->dtype_nsect == label->dkl_nsect) {
1708 return (dp);
1713 return ((struct disk_type *)NULL);
1718 * Find an existing scsi disk definition by this name,
1719 * if possible.
1721 static struct disk_type *
1722 find_scsi_disk_by_name(
1723 char *disk_name)
1725 struct ctlr_type *ctlr;
1726 struct disk_type *dp;
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 return (dp);
1737 return ((struct disk_type *)NULL);
1742 * Return a pointer to the ctlr_type structure for SCSI
1743 * disks. This list is built into the program, so there's
1744 * no chance of not being able to find it, unless someone
1745 * totally mangles the code.
1747 static struct ctlr_type *
1748 find_scsi_ctlr_type()
1750 struct mctlr_list *mlp;
1752 mlp = controlp;
1754 while (mlp != NULL) {
1755 if (mlp->ctlr_type->ctype_ctype == DKC_SCSI_CCS) {
1756 return (mlp->ctlr_type);
1758 mlp = mlp->next;
1761 impossible("no SCSI controller type");
1763 return ((struct ctlr_type *)NULL);
1769 * Return a pointer to the scsi ctlr_info structure. This
1770 * structure is allocated the first time format sees a
1771 * disk on this controller, so it must be present.
1773 static struct ctlr_info *
1774 find_scsi_ctlr_info(
1775 struct dk_cinfo *dkinfo)
1777 struct ctlr_info *ctlr;
1779 if (dkinfo->dki_ctype != DKC_SCSI_CCS) {
1780 return (NULL);
1783 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) {
1784 if (ctlr->ctlr_addr == dkinfo->dki_addr &&
1785 ctlr->ctlr_space == dkinfo->dki_space &&
1786 ctlr->ctlr_ctype->ctype_ctype == DKC_SCSI_CCS) {
1787 return (ctlr);
1791 impossible("no SCSI controller info");
1793 return ((struct ctlr_info *)NULL);
1798 static struct disk_type *
1799 new_scsi_disk_type(
1800 int fd,
1801 char *disk_name,
1802 struct dk_label *label)
1804 struct disk_type *dp;
1805 struct disk_type *disk;
1806 struct ctlr_info *ctlr;
1807 struct dk_cinfo dkinfo;
1808 struct partition_info *part;
1809 struct partition_info *pt;
1810 struct disk_info *disk_info;
1811 int i;
1814 * Get the disk controller info for this disk
1816 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
1817 if (option_msg && diag_msg) {
1818 err_print("DKIOCINFO failed\n");
1820 return (NULL);
1824 * Find the ctlr_info for this disk.
1826 ctlr = find_scsi_ctlr_info(&dkinfo);
1829 * Allocate a new disk type for the SCSI controller.
1831 disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
1834 * Find the disk_info instance for this disk.
1836 disk_info = find_scsi_disk_info(&dkinfo);
1839 * The controller and the disk should match.
1841 assert(disk_info->disk_ctlr == ctlr);
1844 * Link the disk into the list of disks
1846 dp = ctlr->ctlr_ctype->ctype_dlist;
1847 if (dp == NULL) {
1848 ctlr->ctlr_ctype->ctype_dlist = disk;
1849 } else {
1850 while (dp->dtype_next != NULL) {
1851 dp = dp->dtype_next;
1853 dp->dtype_next = disk;
1855 disk->dtype_next = NULL;
1858 * Allocate and initialize the disk name.
1860 disk->dtype_asciilabel = alloc_string(disk_name);
1863 * Initialize disk geometry info
1865 disk->dtype_pcyl = label->dkl_pcyl;
1866 disk->dtype_ncyl = label->dkl_ncyl;
1867 disk->dtype_acyl = label->dkl_acyl;
1868 disk->dtype_nhead = label->dkl_nhead;
1869 disk->dtype_nsect = label->dkl_nsect;
1870 disk->dtype_rpm = label->dkl_rpm;
1873 * Attempt to match the partition map in the label
1874 * with a know partition for this disk type.
1876 for (part = disk->dtype_plist; part; part = part->pinfo_next) {
1877 if (parts_match(label, part)) {
1878 break;
1883 * If no match was made, we need to create a partition
1884 * map for this disk.
1886 if (part == NULL) {
1887 part = (struct partition_info *)
1888 zalloc(sizeof (struct partition_info));
1889 pt = disk->dtype_plist;
1890 if (pt == NULL) {
1891 disk->dtype_plist = part;
1892 } else {
1893 while (pt->pinfo_next != NULL) {
1894 pt = pt->pinfo_next;
1896 pt->pinfo_next = part;
1898 part->pinfo_next = NULL;
1901 * Set up the partition name
1903 part->pinfo_name = alloc_string("default");
1906 * Fill in the partition info from the label
1908 for (i = 0; i < NDKMAP; i++) {
1910 #if defined(_SUNOS_VTOC_8)
1911 part->pinfo_map[i] = label->dkl_map[i];
1913 #elif defined(_SUNOS_VTOC_16)
1914 part->pinfo_map[i].dkl_cylno =
1915 label->dkl_vtoc.v_part[i].p_start /
1916 ((blkaddr32_t)(disk->dtype_nhead *
1917 disk->dtype_nsect - apc));
1918 part->pinfo_map[i].dkl_nblk =
1919 label->dkl_vtoc.v_part[i].p_size;
1920 #else
1921 #error No VTOC format defined.
1922 #endif /* defined(_SUNOS_VTOC_8) */
1929 * Use the VTOC if valid, or install a default
1931 if (label->dkl_vtoc.v_version == V_VERSION) {
1932 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume,
1933 LEN_DKL_VVOL);
1934 part->vtoc = label->dkl_vtoc;
1935 } else {
1936 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL);
1937 set_vtoc_defaults(part);
1941 * Link the disk to the partition map
1943 disk_info->disk_parts = part;
1945 return (disk);
1950 * Delete a disk type from disk type list.
1953 delete_disk_type(struct disk_type *disk_type)
1955 struct ctlr_type *ctlr;
1956 struct disk_type *dp, *disk;
1958 if (cur_ctype->ctype_ctype == DKC_DIRECT ||
1959 cur_ctype->ctype_ctype == DKC_VBD ||
1960 cur_ctype->ctype_ctype == DKC_BLKDEV)
1961 ctlr = find_ctlr_type(cur_ctype->ctype_ctype);
1962 else
1963 ctlr = find_scsi_ctlr_type();
1964 if (ctlr == NULL || ctlr->ctype_dlist == NULL) {
1965 return (-1);
1968 disk = ctlr->ctype_dlist;
1969 if (disk == disk_type) {
1970 ctlr->ctype_dlist = disk->dtype_next;
1971 if (cur_label == L_TYPE_EFI)
1972 free(disk->dtype_plist->etoc);
1973 free(disk->dtype_plist);
1974 free(disk->vendor);
1975 free(disk->product);
1976 free(disk->revision);
1977 free(disk);
1978 return (0);
1979 } else {
1980 for (dp = disk->dtype_next; dp != NULL;
1981 disk = disk->dtype_next, dp = dp->dtype_next) {
1982 if (dp == disk_type) {
1983 disk->dtype_next = dp->dtype_next;
1984 if (cur_label == L_TYPE_EFI)
1985 free(dp->dtype_plist->etoc);
1986 free(dp->dtype_plist);
1987 free(dp->vendor);
1988 free(dp->product);
1989 free(dp->revision);
1990 free(dp);
1991 return (0);
1994 return (-1);
1999 static struct disk_info *
2000 find_scsi_disk_info(
2001 struct dk_cinfo *dkinfo)
2003 struct disk_info *disk;
2004 struct dk_cinfo *dp;
2006 for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
2007 assert(dkinfo->dki_ctype == DKC_SCSI_CCS);
2008 dp = &disk->disk_dkinfo;
2009 if (dp->dki_ctype == dkinfo->dki_ctype &&
2010 dp->dki_cnum == dkinfo->dki_cnum &&
2011 dp->dki_unit == dkinfo->dki_unit &&
2012 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) {
2013 return (disk);
2017 impossible("No SCSI disk info instance\n");
2019 return ((struct disk_info *)NULL);
2023 static char *
2024 get_sun_disk_name(
2025 char *disk_name,
2026 struct scsi_inquiry *inquiry)
2029 * Extract the sun name of the disk
2031 (void) memset(disk_name, 0, DISK_NAME_MAX);
2032 (void) memcpy(disk_name, (char *)&inquiry->inq_pid[9], 7);
2034 return (disk_name);
2038 char *
2039 get_generic_disk_name(
2040 char *disk_name,
2041 struct scsi_inquiry *inquiry)
2043 char *p;
2045 (void) memset(disk_name, 0, DISK_NAME_MAX);
2046 p = strcopy(disk_name, inquiry->inq_vid,
2047 sizeof (inquiry->inq_vid));
2048 *p++ = '-';
2049 p = strcopy(p, inquiry->inq_pid, sizeof (inquiry->inq_pid));
2050 *p++ = '-';
2051 p = strcopy(p, inquiry->inq_revision,
2052 sizeof (inquiry->inq_revision));
2054 return (disk_name);
2058 * Copy a string of characters from src to dst, for at
2059 * most n bytes. Strip all leading and trailing spaces,
2060 * and stop if there are any non-printable characters.
2061 * Return ptr to the next character to be filled.
2063 static char *
2064 strcopy(
2065 char *dst,
2066 char *src,
2067 int n)
2069 int i;
2071 while (*src == ' ' && n > 0) {
2072 src++;
2073 n--;
2076 for (i = 0; n-- > 0 && isascii(*src) && isprint(*src); src++) {
2077 if (*src == ' ') {
2078 i++;
2079 } else {
2080 while (i-- > 0)
2081 *dst++ = ' ';
2082 *dst++ = *src;
2086 *dst = 0;
2087 return (dst);
2091 * adjust disk geometry.
2092 * This is used when disk reports a disk geometry page having
2093 * no of physical cylinders is < 3 which is the minimum required
2094 * by Solaris (2 for storing labels and at least one as a data
2095 * cylinder )
2098 adjust_disk_geometry(diskaddr_t capacity, uint_t *cyl, uint_t *nhead,
2099 uint_t *nsect)
2101 uint_t lcyl = *cyl;
2102 uint_t lnhead = *nhead;
2103 uint_t lnsect = *nsect;
2105 assert(lcyl < SUN_MIN_CYL);
2108 * reduce nsect by 2 for each iteration and re-calculate
2109 * the number of cylinders.
2111 while (lnsect > MINIMUM_NO_SECTORS &&
2112 lcyl < MINIMUM_NO_CYLINDERS) {
2114 * make sure that we do not go below MINIMUM_NO_SECTORS.
2116 lnsect = max(MINIMUM_NO_SECTORS, lnsect / 2);
2117 lcyl = (capacity) / (lnhead * lnsect);
2120 * If the geometry still does not satisfy
2121 * MINIMUM_NO_CYLINDERS then try to reduce the
2122 * no of heads.
2124 while (lnhead > MINIMUM_NO_HEADS &&
2125 lcyl < MINIMUM_NO_CYLINDERS) {
2126 lnhead = max(MINIMUM_NO_HEADS, lnhead / 2);
2127 lcyl = (capacity) / (lnhead * lnsect);
2130 * now we should have atleast SUN_MIN_CYL cylinders.
2131 * If we still do not get SUN_MIN_CYL with MINIMUM_NO_HEADS
2132 * and MINIMUM_NO_HEADS then return error.
2134 if (lcyl < SUN_MIN_CYL)
2135 return (1);
2136 else {
2137 *cyl = lcyl;
2138 *nhead = lnhead;
2139 *nsect = lnsect;
2140 return (0);
2144 #if defined(_SUNOS_VTOC_8)
2146 * Reduce the size of one dimention below a specified
2147 * limit with a minimum loss of volume. Dimenstions are
2148 * assumed to be passed in form the largest value (the one
2149 * that needs to be reduced) to the smallest value. The
2150 * values will be twiddled until they are all less than or
2151 * equal to their limit. Returns the number in the new geometry.
2153 static diskaddr_t
2154 square_box(
2155 diskaddr_t capacity,
2156 uint_t *dim1, uint_t lim1,
2157 uint_t *dim2, uint_t lim2,
2158 uint_t *dim3, uint_t lim3)
2160 uint_t i;
2163 * Although the routine should work with any ordering of
2164 * parameters, it's most efficient if they are passed in
2165 * in decreasing magnitude.
2167 assert(*dim1 >= *dim2);
2168 assert(*dim2 >= *dim3);
2171 * This is done in a very arbitrary manner. We could try to
2172 * find better values but I can't come up with a method that
2173 * would run in a reasonable amount of time. That could take
2174 * approximately 65535 * 65535 iterations of a dozen flops each
2175 * or well over 4G flops.
2177 * First:
2179 * Let's see how far we can go with bitshifts w/o losing
2180 * any blocks.
2183 for (i = 0; (((*dim1)>>i)&1) == 0 && ((*dim1)>>i) > lim1; i++)
2185 if (i) {
2186 *dim1 = ((*dim1)>>i);
2187 *dim3 = ((*dim3)<<i);
2190 if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) {
2191 double d[4];
2194 * Second:
2196 * Set the highest value at its limit then calculate errors,
2197 * adjusting the 2nd highest value (we get better resolution
2198 * that way).
2200 d[1] = lim1;
2201 d[3] = *dim3;
2202 d[2] = (double)capacity/(d[1]*d[3]);
2205 * If we overflowed the middle term, set it to its limit and
2206 * chose a new low term.
2208 if (d[2] > lim2) {
2209 d[2] = lim2;
2210 d[3] = (double)capacity/(d[1]*d[2]);
2213 * Convert to integers.
2215 *dim1 = (int)d[1];
2216 *dim2 = (int)d[2];
2217 *dim3 = (int)d[3];
2220 * Fixup any other possible problems.
2221 * If this happens, we need a new disklabel format.
2223 if (*dim1 > lim1) *dim1 = lim1;
2224 if (*dim2 > lim2) *dim2 = lim2;
2225 if (*dim3 > lim3) *dim3 = lim3;
2226 return (*dim1 * *dim2 * *dim3);
2228 #endif /* defined(_SUNOS_VTOC_8) */
2231 * Calculate CHS values based on the capacity data.
2233 * NOTE: This function is same as cmlb_convert_geomerty() function in
2234 * cmlb kernel module.
2236 static void
2237 compute_chs_values(diskaddr_t total_capacity, diskaddr_t usable_capacity,
2238 uint_t *pcylp, uint_t *nheadp, uint_t *nsectp)
2241 /* Unlabeled SCSI floppy device */
2242 if (total_capacity < 160) {
2243 /* Less than 80K */
2244 *nheadp = 1;
2245 *pcylp = total_capacity;
2246 *nsectp = 1;
2247 return;
2248 } else if (total_capacity <= 0x1000) {
2249 *nheadp = 2;
2250 *pcylp = 80;
2251 *nsectp = total_capacity / (80 * 2);
2252 return;
2256 * For all devices we calculate cylinders using the heads and sectors
2257 * we assign based on capacity of the device. The algorithm is
2258 * designed to be compatible with the way other operating systems
2259 * lay out fdisk tables for X86 and to insure that the cylinders never
2260 * exceed 65535 to prevent problems with X86 ioctls that report
2261 * geometry.
2262 * For some smaller disk sizes we report geometry that matches those
2263 * used by X86 BIOS usage. For larger disks, we use SPT that are
2264 * multiples of 63, since other OSes that are not limited to 16-bits
2265 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
2267 * The following table (in order) illustrates some end result
2268 * calculations:
2270 * Maximum number of blocks nhead nsect
2272 * 2097152 (1GB) 64 32
2273 * 16777216 (8GB) 128 32
2274 * 1052819775 (502.02GB) 255 63
2275 * 2105639550 (0.98TB) 255 126
2276 * 3158459325 (1.47TB) 255 189
2277 * 4211279100 (1.96TB) 255 252
2278 * 5264098875 (2.45TB) 255 315
2279 * ...
2282 if (total_capacity <= 0x200000) {
2283 *nheadp = 64;
2284 *nsectp = 32;
2285 } else if (total_capacity <= 0x01000000) {
2286 *nheadp = 128;
2287 *nsectp = 32;
2288 } else {
2289 *nheadp = 255;
2291 /* make nsect be smallest multiple of 63 */
2292 *nsectp = ((total_capacity +
2293 (UINT16_MAX * 255 * 63) - 1) /
2294 (UINT16_MAX * 255 * 63)) * 63;
2296 if (*nsectp == 0)
2297 *nsectp = (UINT16_MAX / 63) * 63;
2300 if (usable_capacity < total_capacity)
2301 *pcylp = usable_capacity / ((*nheadp) * (*nsectp));
2302 else
2303 *pcylp = total_capacity / ((*nheadp) * (*nsectp));