xref: /titanic_50/usr/src/cmd/format/label.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This file contains the code relating to label manipulation.
30  */
31 
32 #include "global.h"
33 #include "label.h"
34 #include "misc.h"
35 #include "main.h"
36 #include "partition.h"
37 #include "ctlr_scsi.h"
38 #include <string.h>
39 #include <stdlib.h>
40 #include <memory.h>
41 #include <sys/isa_defs.h>
42 #include <sys/efi_partition.h>
43 #include <sys/vtoc.h>
44 #include <sys/uuid.h>
45 #include <errno.h>
46 #include <devid.h>
47 
48 #if defined(_FIRMWARE_NEEDS_FDISK)
49 #include <sys/dktp/fdisk.h>
50 #include "menu_fdisk.h"
51 #endif		/* defined(_FIRMWARE_NEEDS_FDISK) */
52 
53 #ifndef	WD_NODE
54 #define	WD_NODE		7
55 #endif
56 
57 #ifdef	__STDC__
58 /*
59  * Prototypes for ANSI C compilers
60  */
61 static int	do_geometry_sanity_check(void);
62 static int	vtoc_to_label(struct dk_label *label, struct vtoc *vtoc,
63 		struct dk_geom *geom);
64 extern int	read_vtoc(int, struct vtoc *);
65 extern int	write_vtoc(int, struct vtoc *);
66 static int	vtoc64_to_label(struct efi_info *, struct dk_gpt *);
67 
68 #else	/* __STDC__ */
69 
70 /*
71  * Prototypes for non-ANSI C compilers
72  */
73 static int	do_geometry_sanity_check();
74 static int	vtoc_to_label();
75 extern int	read_vtoc();
76 extern int	write_vtoc();
77 static int	vtoc64_to_label();
78 
79 #endif	/* __STDC__ */
80 
81 /*
82  * This routine checks the given label to see if it is valid.
83  */
84 int
85 checklabel(label)
86 	register struct dk_label *label;
87 {
88 
89 	/*
90 	 * Check the magic number.
91 	 */
92 	if (label->dkl_magic != DKL_MAGIC)
93 		return (0);
94 	/*
95 	 * Check the checksum.
96 	 */
97 	if (checksum(label, CK_CHECKSUM) != 0)
98 		return (0);
99 	return (1);
100 }
101 
102 /*
103  * This routine checks or calculates the label checksum, depending on
104  * the mode it is called in.
105  */
106 int
107 checksum(label, mode)
108 	struct	dk_label *label;
109 	int	mode;
110 {
111 	register short *sp, sum = 0;
112 	register short count = (sizeof (struct dk_label)) / (sizeof (short));
113 
114 	/*
115 	 * If we are generating a checksum, don't include the checksum
116 	 * in the rolling xor.
117 	 */
118 	if (mode == CK_MAKESUM)
119 		count -= 1;
120 	sp = (short *)label;
121 	/*
122 	 * Take the xor of all the half-words in the label.
123 	 */
124 	while (count--) {
125 		sum ^= *sp++;
126 	}
127 	/*
128 	 * If we are checking the checksum, the total will be zero for
129 	 * a correct checksum, so we can just return the sum.
130 	 */
131 	if (mode == CK_CHECKSUM)
132 		return (sum);
133 	/*
134 	 * If we are generating the checksum, fill it in.
135 	 */
136 	else {
137 		label->dkl_cksum = sum;
138 		return (0);
139 	}
140 }
141 
142 /*
143  * This routine is used to extract the id string from the string stored
144  * in a disk label.  The problem is that the string in the label has
145  * the physical characteristics of the drive appended to it.  The approach
146  * is to find the beginning of the physical attributes portion of the string
147  * and truncate it there.
148  */
149 int
150 trim_id(id)
151 	char	*id;
152 {
153 	register char *c;
154 
155 	/*
156 	 * Start at the end of the string.  When we match the word ' cyl',
157 	 * we are at the beginning of the attributes.
158 	 */
159 	for (c = id + strlen(id); c >= id; c--) {
160 		if (strncmp(c, " cyl", strlen(" cyl")) == 0) {
161 			/*
162 			 * Remove any white space.
163 			 */
164 			for (; (((*(c - 1) == ' ') || (*(c - 1) == '\t')) &&
165 				(c >= id)); c--);
166 			break;
167 		}
168 	}
169 	/*
170 	 * If we ran off the beginning of the string, something is wrong.
171 	 */
172 	if (c < id)
173 		return (-1);
174 	/*
175 	 * Truncate the string.
176 	 */
177 	*c = '\0';
178 	return (0);
179 }
180 
181 /*
182  * This routine is used by write_label() to do a quick sanity check on the
183  * supplied geometry. This is not a thorough check.
184  *
185  * The SCSI READ_CAPACITY command is used here to get the capacity of the
186  * disk. But, the available area to store data on a disk is usually less
187  * than this. So, if the specified geometry evaluates to a value which falls
188  * in this margin, then such illegal geometries can slip through the cracks.
189  */
190 static int
191 do_geometry_sanity_check()
192 {
193 	struct scsi_capacity_16	 capacity;
194 
195 	if (uscsi_read_capacity(cur_file, &capacity)) {
196 		err_print("Warning: Unable to get capacity."
197 				" Cannot check geometry\n");
198 		return (0);	/* Just ignore this problem */
199 	}
200 
201 	if (capacity.sc_capacity < ncyl * nhead * nsect) {
202 		err_print("\nWarning: Current geometry overshoots "
203 				"actual geometry of disk\n\n");
204 		if (check("Continue labelling disk") != 0)
205 			return (-1);
206 		return (0);	/* Just ignore this problem */
207 	}
208 
209 	return (0);
210 }
211 
212 /*
213  * create a clear EFI partition table when format is used
214  * to convert a SMI label to an EFI lable
215  */
216 int
217 SMI_vtoc_to_EFI(int fd, struct dk_gpt **new_vtoc)
218 {
219 	int i;
220 	struct dk_gpt	*efi;
221 
222 	if (efi_alloc_and_init(fd, EFI_NUMPAR, new_vtoc) != 0) {
223 	    err_print("SMI vtoc to EFI failed\n");
224 	    return (-1);
225 	}
226 	efi = *new_vtoc;
227 
228 	/*
229 	 * create a clear EFI partition table:
230 	 * s0 takes the whole disk except the primary EFI lable,
231 	 * backup EFI labels, and the reserved partition.
232 	 * s1-s6 are unassigned slices.
233 	 */
234 	efi->efi_parts[0].p_tag = V_USR;
235 	efi->efi_parts[0].p_start = efi->efi_first_u_lba;
236 	efi->efi_parts[0].p_size = efi->efi_last_u_lba - efi->efi_first_u_lba
237 		- EFI_MIN_RESV_SIZE;
238 
239 	/*
240 	 * s1-s6 are unassigned slices
241 	 */
242 	for (i = 1; i < efi->efi_nparts - 2; i++) {
243 		efi->efi_parts[i].p_tag = V_UNASSIGNED;
244 		efi->efi_parts[i].p_start = 0;
245 		efi->efi_parts[i].p_size = 0;
246 	}
247 
248 	/*
249 	 * the reserved slice
250 	 */
251 	efi->efi_parts[efi->efi_nparts - 1].p_tag = V_RESERVED;
252 	efi->efi_parts[efi->efi_nparts - 1].p_start =
253 		efi->efi_last_u_lba - EFI_MIN_RESV_SIZE;
254 	efi->efi_parts[efi->efi_nparts - 1].p_size = EFI_MIN_RESV_SIZE;
255 
256 	return (0);
257 }
258 
259 /*
260  * This routine constructs and writes a label on the disk.  It writes both
261  * the primary and backup labels.  It assumes that there is a current
262  * partition map already defined.  It also notifies the SunOS kernel of
263  * the label and partition information it has written on the disk.
264  */
265 int
266 write_label()
267 {
268 	int	error = 0, head, sec;
269 	struct dk_label label;
270 	struct dk_label new_label;
271 	struct vtoc	vtoc;
272 	struct dk_geom	geom;
273 	struct dk_gpt	*vtoc64;
274 	int		nbackups;
275 
276 #if defined(_SUNOS_VTOC_8)
277 	int i;
278 #endif		/* defined(_SUNOS_VTOC_8) */
279 
280 	/*
281 	 * If EFI label, then write it out to disk
282 	 */
283 	if (cur_label == L_TYPE_EFI) {
284 	    enter_critical();
285 	    vtoc64 = cur_parts->etoc;
286 	    err_check(vtoc64);
287 	    if (efi_write(cur_file, vtoc64) != 0) {
288 		err_print("Warning: error writing EFI.\n");
289 		error = -1;
290 	    }
291 
292 	    cur_disk->disk_flags |= DSK_LABEL;
293 	    exit_critical();
294 	    return (error);
295 	}
296 
297 	/*
298 	 * Fill in a label structure with the geometry information.
299 	 */
300 	(void) memset((char *)&label, 0, sizeof (struct dk_label));
301 	(void) memset((char *)&new_label, 0, sizeof (struct dk_label));
302 	label.dkl_pcyl = pcyl;
303 	label.dkl_ncyl = ncyl;
304 	label.dkl_acyl = acyl;
305 
306 #if defined(_SUNOS_VTOC_16)
307 	label.dkl_bcyl = bcyl;
308 #endif			/* defined(_SUNOC_VTOC_16) */
309 
310 	label.dkl_nhead = nhead;
311 	label.dkl_nsect = nsect;
312 	label.dkl_apc = apc;
313 	label.dkl_intrlv = 1;
314 	label.dkl_rpm = cur_dtype->dtype_rpm;
315 
316 #if defined(_SUNOS_VTOC_8)
317 	/*
318 	 * Also fill in the current partition information.
319 	 */
320 	for (i = 0; i < NDKMAP; i++) {
321 		label.dkl_map[i] = cur_parts->pinfo_map[i];
322 	}
323 #endif			/* defined(_SUNOS_VTOC_8) */
324 
325 	label.dkl_magic = DKL_MAGIC;
326 
327 	/*
328 	 * Fill in the vtoc information
329 	 */
330 	label.dkl_vtoc = cur_parts->vtoc;
331 
332 	/*
333 	 * Use the current label
334 	 */
335 	bcopy(cur_disk->v_volume, label.dkl_vtoc.v_volume, LEN_DKL_VVOL);
336 
337 	/*
338 	 * Put asciilabel in; on x86 it's in the vtoc, not the label.
339 	 */
340 	(void) snprintf(label.dkl_asciilabel, sizeof (label.dkl_asciilabel),
341 	    "%s cyl %d alt %d hd %d sec %d",
342 	    cur_dtype->dtype_asciilabel, ncyl, acyl, nhead, nsect);
343 
344 #if defined(_SUNOS_VTOC_16)
345 	/*
346 	 * Also add in v_sectorsz, as the driver will.  Everyone
347 	 * else is assuming DEV_BSIZE, so we do the same.
348 	 */
349 	label.dkl_vtoc.v_sectorsz = DEV_BSIZE;
350 #endif			/* defined(_SUNOS_VTOC_16) */
351 
352 	/*
353 	 * Generate the correct checksum.
354 	 */
355 	(void) checksum(&label, CK_MAKESUM);
356 	/*
357 	 * Convert the label into a vtoc
358 	 */
359 	if (label_to_vtoc(&vtoc, &label) == -1) {
360 		return (-1);
361 	}
362 	/*
363 	 * Fill in the geometry info.  This is critical that
364 	 * we do this before writing the vtoc.
365 	 */
366 	bzero((caddr_t)&geom, sizeof (struct dk_geom));
367 	geom.dkg_ncyl = ncyl;
368 	geom.dkg_acyl = acyl;
369 
370 #if defined(_SUNOS_VTOC_16)
371 	geom.dkg_bcyl = bcyl;
372 #endif			/* defined(_SUNOS_VTOC_16) */
373 
374 	geom.dkg_nhead = nhead;
375 	geom.dkg_nsect = nsect;
376 	geom.dkg_intrlv = 1;
377 	geom.dkg_apc = apc;
378 	geom.dkg_rpm = cur_dtype->dtype_rpm;
379 	geom.dkg_pcyl = pcyl;
380 
381 	/*
382 	 * Make a quick check to see that the geometry is being
383 	 * written now is not way off from the actual capacity
384 	 * of the disk. This is only an appoximate check and
385 	 * is only for SCSI disks.
386 	 */
387 	if (SCSI && do_geometry_sanity_check() != 0) {
388 		return (-1);
389 	}
390 
391 	/*
392 	 * Lock out interrupts so we do things in sync.
393 	 */
394 	enter_critical();
395 	/*
396 	 * Do the ioctl to tell the kernel the geometry.
397 	 */
398 	if (ioctl(cur_file, DKIOCSGEOM, &geom) == -1) {
399 		err_print("Warning: error setting drive geometry.\n");
400 		error = -1;
401 	}
402 	/*
403 	 * Write the vtoc.  At the time of this writing, our
404 	 * drivers convert the vtoc back to a label, and
405 	 * then write both the primary and backup labels.
406 	 * This is not a requirement, however, as we
407 	 * always use an ioctl to read the vtoc from the
408 	 * driver, so it can do as it likes.
409 	 */
410 	if (write_vtoc(cur_file, &vtoc) != 0) {
411 		err_print("Warning: error writing VTOC.\n");
412 		error = -1;
413 	}
414 
415 	/*
416 	 * Calculate where the backup labels went.  They are always on
417 	 * the last alternate cylinder, but some older drives put them
418 	 * on head 2 instead of the last head.  They are always on the
419 	 * first 5 odd sectors of the appropriate track.
420 	 */
421 	if (cur_ctype->ctype_flags & CF_BLABEL)
422 		head  = 2;
423 	else
424 		head = nhead - 1;
425 	/*
426 	 * Read and verify the backup labels.
427 	 */
428 	nbackups = 0;
429 	for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect));
430 	    sec += 2) {
431 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, (diskaddr_t)
432 			((chs2bn(ncyl + acyl - 1, head, sec)) + solaris_offset),
433 			1, (caddr_t)&new_label, F_NORMAL, NULL)) {
434 			err_print("Warning: error reading backup label.\n");
435 			error = -1;
436 		} else {
437 			if (bcmp((char *)&label, (char *)&new_label,
438 				sizeof (struct dk_label)) == 0) {
439 					nbackups++;
440 			}
441 		}
442 	}
443 	if (nbackups != BAD_LISTCNT) {
444 		err_print("Warning: %s\n", nbackups == 0 ?
445 			"no backup labels" :
446 			"some backup labels incorrect");
447 	}
448 	/*
449 	 * Mark the current disk as labelled and notify the kernel of what
450 	 * has happened.
451 	 */
452 	cur_disk->disk_flags |= DSK_LABEL;
453 
454 	exit_critical();
455 	return (error);
456 }
457 
458 
459 /*
460  * Read the label from the disk.
461  * Do this via the read_vtoc() library routine, then convert it to a label.
462  * We also need a DKIOCGGEOM ioctl to get the disk's geometry.
463  */
464 int
465 read_label(int fd, struct dk_label *label)
466 {
467 	struct vtoc	vtoc;
468 	struct dk_geom	geom;
469 
470 	if (read_vtoc(fd, &vtoc) < 0 || ioctl(fd, DKIOCGGEOM, &geom) == -1) {
471 		return (-1);
472 	}
473 	return (vtoc_to_label(label, &vtoc, &geom));
474 }
475 
476 int
477 get_disk_info_from_devid(int fd, struct efi_info *label)
478 {
479 	ddi_devid_t	devid;
480 	char		*s;
481 	int		n;
482 	char		*vid, *pid;
483 	int		nvid, npid;
484 	struct dk_minfo	minf;
485 	struct dk_cinfo	dkinfo;
486 
487 	if (devid_get(fd, &devid)) {
488 		if (option_msg && diag_msg)
489 			err_print("devid_get failed\n");
490 		return (-1);
491 	}
492 
493 	n = devid_sizeof(devid);
494 	s = (char *)devid;
495 
496 	if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
497 		if (option_msg && diag_msg)
498 			err_print("DKIOCINFO failed\n");
499 		return (-1);
500 	}
501 
502 	if (dkinfo.dki_ctype != DKC_DIRECT)
503 		return (-1);
504 
505 	vid = s+12;
506 	if (!(pid = strchr(vid, '=')))
507 		return (-1);
508 	nvid = pid - vid;
509 	pid += 1;
510 	npid = n - nvid - 13;
511 
512 	if (nvid > 9)
513 		nvid = 9;
514 	if (npid > 17) {
515 		pid = pid + npid - 17;
516 		npid = 17;
517 	}
518 
519 	if (ioctl(fd, DKIOCGMEDIAINFO, &minf) == -1) {
520 		devid_free(devid);
521 		return (-1);
522 	}
523 
524 	(void) strlcpy(label->vendor, vid, nvid);
525 	(void) strlcpy(label->product, pid, npid);
526 	(void) strlcpy(label->revision, "0001", 5);
527 	label->capacity = minf.dki_capacity * minf.dki_lbsize / 512;
528 
529 	devid_free(devid);
530 	return (0);
531 }
532 
533 /*
534  * Issue uscsi_inquiry and read_capacity commands to
535  * retrieve the disk's Vendor, Product, Revision and
536  * Capacity information.
537  */
538 int
539 get_disk_info(int fd, struct efi_info *label)
540 {
541 	struct scsi_inquiry	inquiry;
542 	struct scsi_capacity_16	capacity;
543 	struct dk_minfo		minf;
544 
545 	if (!get_disk_info_from_devid(fd, label))
546 		return (0);
547 
548 	if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
549 		(void) strlcpy(label->vendor, "Unknown", 8);
550 		(void) strlcpy(label->product, "Unknown", 8);
551 		(void) strlcpy(label->revision, "0001", 5);
552 	} else {
553 		(void) strlcpy(label->vendor, inquiry.inq_vid, 9);
554 		(void) strlcpy(label->product, inquiry.inq_pid, 17);
555 		(void) strlcpy(label->revision, inquiry.inq_revision, 5);
556 	}
557 
558 	if (uscsi_read_capacity(fd, &capacity)) {
559 		if (ioctl(fd, DKIOCGMEDIAINFO, &minf) == -1) {
560 			err_print("Fetch Capacity failed\n");
561 			return (-1);
562 		}
563 		label->capacity = minf.dki_capacity * minf.dki_lbsize / 512;
564 	} else {
565 		label->capacity = capacity.sc_capacity;
566 
567 		/* Since we are counting from zero, add 1 to capacity */
568 		label->capacity++;
569 	}
570 
571 	return (0);
572 }
573 
574 int
575 read_efi_label(int fd, struct efi_info *label)
576 {
577 	struct dk_gpt	*vtoc64;
578 
579 	/* This could fail if there is no label already */
580 	if (efi_alloc_and_read(fd, &vtoc64) < 0) {
581 		return (-1);
582 	}
583 	if (vtoc64_to_label(label, vtoc64) != 0) {
584 		err_print("vtoc64_to_label failed\n");
585 		return (-1);
586 	}
587 	efi_free(vtoc64);
588 	if (get_disk_info(fd, label) != 0) {
589 		return (-1);
590 	}
591 	return (0);
592 }
593 
594 
595 /*
596  * We've read a 64-bit label which has no geometry information.  Use
597  * some heuristics to fake up a geometry that would match the disk in
598  * order to make the rest of format(1M) happy.
599  */
600 static int
601 vtoc64_to_label(struct efi_info *label, struct dk_gpt *vtoc)
602 {
603 	int		i, nparts = 0;
604 	struct dk_gpt	*lmap;
605 
606 	(void) memset((char *)label, 0, sizeof (struct efi_info));
607 
608 	/* XXX do a sanity check here for nparts */
609 	nparts = vtoc->efi_nparts;
610 	lmap = (struct dk_gpt *) calloc(1, (sizeof (struct dk_part) *
611 		    nparts) + sizeof (struct dk_gpt));
612 	if (lmap == NULL) {
613 		err_print("vtoc64_to_label: unable to allocate lmap\n");
614 		fullabort();
615 	}
616 	label->e_parts = lmap;
617 
618 	/*
619 	 * Copy necessary portions
620 	 * XXX Maybe we can use memcpy() ??
621 	 */
622 	lmap->efi_version = vtoc->efi_version;
623 	lmap->efi_nparts = vtoc->efi_nparts;
624 	lmap->efi_part_size = vtoc->efi_part_size;
625 	lmap->efi_lbasize = vtoc->efi_lbasize;
626 	lmap->efi_last_lba = vtoc->efi_last_lba;
627 	lmap->efi_first_u_lba = vtoc->efi_first_u_lba;
628 	lmap->efi_last_u_lba = vtoc->efi_last_u_lba;
629 	lmap->efi_flags = vtoc->efi_flags;
630 	(void) memcpy((uchar_t *)&lmap->efi_disk_uguid,
631 		(uchar_t *)&vtoc->efi_disk_uguid, sizeof (struct uuid));
632 
633 	for (i = 0; i < nparts; i++) {
634 		lmap->efi_parts[i].p_tag = vtoc->efi_parts[i].p_tag;
635 		lmap->efi_parts[i].p_flag = vtoc->efi_parts[i].p_flag;
636 		lmap->efi_parts[i].p_start = vtoc->efi_parts[i].p_start;
637 		lmap->efi_parts[i].p_size = vtoc->efi_parts[i].p_size;
638 		(void) memcpy((uchar_t *)&lmap->efi_parts[i].p_uguid,
639 		    (uchar_t *)&vtoc->efi_parts[i].p_uguid,
640 		    sizeof (struct uuid));
641 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
642 			bcopy(vtoc->efi_parts[i].p_name,
643 			    lmap->efi_parts[i].p_name, LEN_DKL_VVOL);
644 		}
645 	}
646 	return (0);
647 }
648 
649 /*
650  * Convert vtoc/geom to label.
651  */
652 static int
653 vtoc_to_label(struct dk_label *label, struct vtoc *vtoc, struct dk_geom *geom)
654 {
655 #if defined(_SUNOS_VTOC_8)
656 	struct dk_map32		*lmap;
657 #elif defined(_SUNOS_VTOC_16)
658 	struct dkl_partition	*lmap;
659 #else
660 #error No VTOC format defined.
661 #endif			/* defined(_SUNOS_VTOC_8) */
662 
663 	struct partition	*vpart;
664 	long			nblks;
665 	int			i;
666 
667 	(void) memset((char *)label, 0, sizeof (struct dk_label));
668 
669 	/*
670 	 * Sanity-check the vtoc
671 	 */
672 	if (vtoc->v_sanity != VTOC_SANE || vtoc->v_sectorsz != DEV_BSIZE ||
673 			vtoc->v_nparts != V_NUMPAR) {
674 		return (-1);
675 	}
676 
677 	/*
678 	 * Sanity check of geometry
679 	 */
680 	if (geom->dkg_ncyl == 0 || geom->dkg_nhead == 0 ||
681 			geom->dkg_nsect == 0) {
682 		return (-1);
683 	}
684 
685 	label->dkl_magic = DKL_MAGIC;
686 
687 	/*
688 	 * Copy necessary portions of the geometry information
689 	 */
690 	label->dkl_rpm = geom->dkg_rpm;
691 	label->dkl_pcyl = geom->dkg_pcyl;
692 	label->dkl_apc = geom->dkg_apc;
693 	label->dkl_intrlv = geom->dkg_intrlv;
694 	label->dkl_ncyl = geom->dkg_ncyl;
695 	label->dkl_acyl = geom->dkg_acyl;
696 
697 #if defined(_SUNOS_VTOC_16)
698 	label->dkl_bcyl = geom->dkg_bcyl;
699 #endif			/* defined(_SUNOS_VTOC_16) */
700 
701 	label->dkl_nhead = geom->dkg_nhead;
702 	label->dkl_nsect = geom->dkg_nsect;
703 
704 #if defined(_SUNOS_VTOC_8)
705 	label->dkl_obs1 = geom->dkg_obs1;
706 	label->dkl_obs2 = geom->dkg_obs2;
707 	label->dkl_obs3 = geom->dkg_obs3;
708 #endif			/* defined(_SUNOS_VTOC_8) */
709 
710 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
711 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
712 
713 	/*
714 	 * Copy vtoc structure fields into the disk label dk_vtoc
715 	 */
716 	label->dkl_vtoc.v_sanity = vtoc->v_sanity;
717 	label->dkl_vtoc.v_nparts = vtoc->v_nparts;
718 	label->dkl_vtoc.v_version = vtoc->v_version;
719 
720 	(void) memcpy(label->dkl_vtoc.v_volume, vtoc->v_volume,
721 		LEN_DKL_VVOL);
722 	for (i = 0; i < V_NUMPAR; i++) {
723 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
724 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
725 	}
726 	(void) memcpy((char *)label->dkl_vtoc.v_bootinfo,
727 		(char *)vtoc->v_bootinfo, sizeof (vtoc->v_bootinfo));
728 	(void) memcpy((char *)label->dkl_vtoc.v_reserved,
729 		(char *)vtoc->v_reserved, sizeof (vtoc->v_reserved));
730 	(void) memcpy((char *)label->dkl_vtoc.v_timestamp,
731 		(char *)vtoc->timestamp, sizeof (vtoc->timestamp));
732 
733 	(void) memcpy(label->dkl_asciilabel, vtoc->v_asciilabel,
734 		LEN_DKL_ASCII);
735 
736 	/*
737 	 * Note the conversion from starting sector number
738 	 * to starting cylinder number.
739 	 * Return error if division results in a remainder.
740 	 */
741 #if defined(_SUNOS_VTOC_8)
742 	lmap = label->dkl_map;
743 
744 #elif defined(_SUNOS_VTOC_16)
745 	lmap = label->dkl_vtoc.v_part;
746 #else
747 #error No VTOC format defined.
748 #endif			/* defined(_SUNOS_VTOC_8) */
749 
750 	vpart = vtoc->v_part;
751 
752 	nblks = (int)label->dkl_nsect * (int)label->dkl_nhead;
753 
754 	for (i = 0; i < NDKMAP; i++, lmap++, vpart++) {
755 		if ((vpart->p_start % nblks) != 0 ||
756 				(vpart->p_size % nblks) != 0) {
757 			return (-1);
758 		}
759 #if defined(_SUNOS_VTOC_8)
760 		lmap->dkl_cylno = vpart->p_start / nblks;
761 		lmap->dkl_nblk = vpart->p_size;
762 
763 #elif defined(_SUNOS_VTOC_16)
764 		lmap->p_start = vpart->p_start;
765 		lmap->p_size = vpart->p_size;
766 #else
767 #error No VTOC format defined.
768 #endif			/* defined(_SUNOS_VTOC_8) */
769 	}
770 
771 	/*
772 	 * Finally, make a checksum
773 	 */
774 	(void) checksum(label, CK_MAKESUM);
775 
776 	return (0);
777 }
778 
779 
780 
781 /*
782  * Extract a vtoc structure out of a valid label
783  */
784 int
785 label_to_vtoc(struct vtoc *vtoc, struct dk_label *label)
786 {
787 #if defined(_SUNOS_VTOC_8)
788 	struct dk_map2		*lpart;
789 	struct dk_map32		*lmap;
790 	long			nblks;
791 
792 #elif defined(_SUNOS_VTOC_16)
793 	struct dkl_partition	*lpart;
794 #else
795 #error No VTOC format defined.
796 #endif				/* defined(_SUNOS_VTOC_8) */
797 
798 	struct partition	*vpart;
799 	int			i;
800 
801 	(void) memset((char *)vtoc, 0, sizeof (struct vtoc));
802 
803 	switch (label->dkl_vtoc.v_version) {
804 	case 0:
805 		/*
806 		 * No valid vtoc information in the label.
807 		 * Construct default p_flags and p_tags.
808 		 */
809 		vpart = vtoc->v_part;
810 		for (i = 0; i < V_NUMPAR; i++, vpart++) {
811 			vpart->p_tag = default_vtoc_map[i].p_tag;
812 			vpart->p_flag = default_vtoc_map[i].p_flag;
813 		}
814 		break;
815 
816 	case V_VERSION:
817 		vpart = vtoc->v_part;
818 		lpart = label->dkl_vtoc.v_part;
819 		for (i = 0; i < V_NUMPAR; i++, vpart++, lpart++) {
820 			vpart->p_tag = lpart->p_tag;
821 			vpart->p_flag = lpart->p_flag;
822 
823 #if defined(_SUNOS_VTOC_16)
824 			vpart->p_start = lpart->p_start;
825 			vpart->p_size = lpart->p_size;
826 #endif	/* defined(_SUNOS_VTOC_16) */
827 		}
828 		(void) memcpy(vtoc->v_volume, label->dkl_vtoc.v_volume,
829 			LEN_DKL_VVOL);
830 		(void) memcpy((char *)vtoc->v_bootinfo,
831 			(char *)label->dkl_vtoc.v_bootinfo,
832 				sizeof (vtoc->v_bootinfo));
833 		(void) memcpy((char *)vtoc->v_reserved,
834 			(char *)label->dkl_vtoc.v_reserved,
835 				sizeof (vtoc->v_reserved));
836 		(void) memcpy((char *)vtoc->timestamp,
837 			(char *)label->dkl_vtoc.v_timestamp,
838 				sizeof (vtoc->timestamp));
839 		break;
840 
841 	default:
842 		return (-1);
843 	}
844 
845 	/*
846 	 * XXX - this looks wrong to me....
847 	 * why are these values hardwired, rather than returned from
848 	 * the real disk label?
849 	 */
850 	vtoc->v_sanity = VTOC_SANE;
851 	vtoc->v_version = V_VERSION;
852 	vtoc->v_sectorsz = DEV_BSIZE;
853 	vtoc->v_nparts = V_NUMPAR;
854 
855 	(void) memcpy(vtoc->v_asciilabel, label->dkl_asciilabel,
856 		LEN_DKL_ASCII);
857 
858 #if defined(_SUNOS_VTOC_8)
859 	/*
860 	 * Convert partitioning information.
861 	 * Note the conversion from starting cylinder number
862 	 * to starting sector number.
863 	 */
864 	lmap = label->dkl_map;
865 	vpart = vtoc->v_part;
866 	nblks = label->dkl_nsect * label->dkl_nhead;
867 	for (i = 0; i < V_NUMPAR; i++, vpart++, lmap++) {
868 		vpart->p_start = lmap->dkl_cylno * nblks;
869 		vpart->p_size = lmap->dkl_nblk;
870 	}
871 #endif			/* defined(_SUNOS_VTOC_8) */
872 
873 	return (0);
874 }
875 
876 /*
877  * Input: File descriptor
878  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
879  */
880 
881 int
882 is_efi_type(int fd)
883 {
884 	struct vtoc vtoc;
885 
886 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
887 		if (errno == ENOTSUP) {
888 			return (1);
889 		}
890 	}
891 	return (0);
892 }
893 
894 /* make sure the user specified something reasonable */
895 void
896 err_check(struct dk_gpt *vtoc)
897 {
898 	int			resv_part = -1;
899 	int			i, j;
900 	diskaddr_t		istart, jstart, isize, jsize, endsect;
901 	int			overlap = 0;
902 
903 	/*
904 	 * make sure no partitions overlap
905 	 */
906 	for (i = 0; i < vtoc->efi_nparts; i++) {
907 		/* It can't be unassigned and have an actual size */
908 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
909 		    (vtoc->efi_parts[i].p_size != 0)) {
910 			(void) fprintf(stderr,
911 "partition %d is \"unassigned\" but has a size of %llu\n", i,
912 			vtoc->efi_parts[i].p_size);
913 		}
914 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
915 			continue;
916 		}
917 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
918 			if (resv_part != -1) {
919 			    (void) fprintf(stderr,
920 "found duplicate reserved partition at %d\n",
921 				i);
922 			}
923 			resv_part = i;
924 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
925 				(void) fprintf(stderr,
926 "Warning: reserved partition size must be %d sectors\n",
927 				EFI_MIN_RESV_SIZE);
928 		}
929 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
930 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
931 			(void) fprintf(stderr,
932 			    "Partition %d starts at %llu\n",
933 			    i,
934 			    vtoc->efi_parts[i].p_start);
935 			(void) fprintf(stderr,
936 			    "It must be between %llu and %llu.\n",
937 			    vtoc->efi_first_u_lba,
938 			    vtoc->efi_last_u_lba);
939 		}
940 		if ((vtoc->efi_parts[i].p_start +
941 		    vtoc->efi_parts[i].p_size <
942 		    vtoc->efi_first_u_lba) ||
943 		    (vtoc->efi_parts[i].p_start +
944 		    vtoc->efi_parts[i].p_size >
945 		    vtoc->efi_last_u_lba + 1)) {
946 			(void) fprintf(stderr,
947 			    "Partition %d ends at %llu\n",
948 			    i,
949 			    vtoc->efi_parts[i].p_start +
950 			    vtoc->efi_parts[i].p_size);
951 			(void) fprintf(stderr,
952 			    "It must be between %llu and %llu.\n",
953 			    vtoc->efi_first_u_lba,
954 			    vtoc->efi_last_u_lba);
955 		}
956 
957 		for (j = 0; j < vtoc->efi_nparts; j++) {
958 			isize = vtoc->efi_parts[i].p_size;
959 			jsize = vtoc->efi_parts[j].p_size;
960 			istart = vtoc->efi_parts[i].p_start;
961 			jstart = vtoc->efi_parts[j].p_start;
962 			if ((i != j) && (isize != 0) && (jsize != 0)) {
963 				endsect = jstart + jsize -1;
964 				if ((jstart <= istart) &&
965 				    (istart <= endsect)) {
966 					if (!overlap) {
967 					(void) fprintf(stderr,
968 "label error: EFI Labels do not support overlapping partitions\n");
969 					}
970 					(void) fprintf(stderr,
971 "Partition %d overlaps partition %d.\n", i, j);
972 					overlap = 1;
973 				}
974 			}
975 		}
976 	}
977 	/* make sure there is a reserved partition */
978 	if (resv_part == -1) {
979 		(void) fprintf(stderr,
980 			"no reserved partition found\n");
981 	}
982 }
983 
984 #ifdef	FOR_DEBUGGING_ONLY
985 int
986 dump_label(label)
987 	struct dk_label	*label;
988 {
989 	int		i;
990 
991 	fmt_print("%s\n", label->dkl_asciilabel);
992 
993 	fmt_print("version:  %d\n", label->dkl_vtoc.v_version);
994 	fmt_print("volume:   ");
995 	for (i = 0; i < LEN_DKL_VVOL; i++) {
996 		if (label->dkl_vtoc.v_volume[i] == 0)
997 			break;
998 		fmt_print("%c", label->dkl_vtoc.v_volume[i]);
999 	}
1000 	fmt_print("\n");
1001 	fmt_print("v_nparts: %d\n", label->dkl_vtoc.v_nparts);
1002 	fmt_print("v_sanity: %lx\n", label->dkl_vtoc.v_sanity);
1003 
1004 #if defined(_SUNOS_VTOC_8)
1005 	fmt_print("rpm:      %d\n", label->dkl_rpm);
1006 	fmt_print("pcyl:     %d\n", label->dkl_pcyl);
1007 	fmt_print("apc:      %d\n", label->dkl_apc);
1008 	fmt_print("obs1:     %d\n", label->dkl_obs1);
1009 	fmt_print("obs2:     %d\n", label->dkl_obs2);
1010 	fmt_print("intrlv:   %d\n", label->dkl_intrlv);
1011 	fmt_print("ncyl:     %d\n", label->dkl_ncyl);
1012 	fmt_print("acyl:     %d\n", label->dkl_acyl);
1013 	fmt_print("nhead:    %d\n", label->dkl_nhead);
1014 	fmt_print("nsect:    %d\n", label->dkl_nsect);
1015 	fmt_print("obs3:     %d\n", label->dkl_obs3);
1016 	fmt_print("obs4:     %d\n", label->dkl_obs4);
1017 
1018 #elif defined(_SUNOS_VTOC_16)
1019 	fmt_print("rpm:      %d\n", label->dkl_rpm);
1020 	fmt_print("pcyl:     %d\n", label->dkl_pcyl);
1021 	fmt_print("apc:      %d\n", label->dkl_apc);
1022 	fmt_print("intrlv:   %d\n", label->dkl_intrlv);
1023 	fmt_print("ncyl:     %d\n", label->dkl_ncyl);
1024 	fmt_print("acyl:     %d\n", label->dkl_acyl);
1025 	fmt_print("nhead:    %d\n", label->dkl_nhead);
1026 	fmt_print("nsect:    %d\n", label->dkl_nsect);
1027 	fmt_print("bcyl:     %d\n", label->dkl_bcyl);
1028 	fmt_print("skew:     %d\n", label->dkl_skew);
1029 #else
1030 #error No VTOC format defined.
1031 #endif				/* defined(_SUNOS_VTOC_8) */
1032 	fmt_print("magic:    %0x\n", label->dkl_magic);
1033 	fmt_print("cksum:    %0x\n", label->dkl_cksum);
1034 
1035 	for (i = 0; i < NDKMAP; i++) {
1036 
1037 #if defined(_SUNOS_VTOC_8)
1038 		fmt_print("%c:        cyl=%d, blocks=%d", i+'a',
1039 			label->dkl_map[i].dkl_cylno,
1040 			label->dkl_map[i].dkl_nblk);
1041 
1042 #elif defined(_SUNOS_VTOC_16)
1043 		fmt_print("%c:        start=%d, blocks=%d", i+'a',
1044 		    label->dkl_vtoc.v_part[i].p_start,
1045 		    label->dkl_vtoc.v_part[i].p_size);
1046 #else
1047 #error No VTOC format defined.
1048 #endif				/* defined(_SUNOS_VTOC_8) */
1049 
1050 		fmt_print(",  tag=%d,  flag=%d",
1051 			label->dkl_vtoc.v_part[i].p_tag,
1052 			label->dkl_vtoc.v_part[i].p_flag);
1053 		fmt_print("\n");
1054 	}
1055 
1056 	fmt_print("read_reinstruct:  %d\n", label->dkl_read_reinstruct);
1057 	fmt_print("write_reinstruct: %d\n", label->dkl_write_reinstruct);
1058 
1059 	fmt_print("bootinfo: ");
1060 	for (i = 0; i < 3; i++) {
1061 		fmt_print("0x%x ", label->dkl_vtoc.v_bootinfo[i]);
1062 	}
1063 	fmt_print("\n");
1064 
1065 	fmt_print("reserved: ");
1066 	for (i = 0; i < 10; i++) {
1067 		if ((i % 4) == 3)
1068 			fmt_print("\n");
1069 		fmt_print("0x%x ", label->dkl_vtoc.v_reserved[i]);
1070 	}
1071 	fmt_print("\n");
1072 
1073 	fmt_print("timestamp:\n");
1074 	for (i = 0; i < NDKMAP; i++) {
1075 		if ((i % 4) == 3)
1076 			fmt_print("\n");
1077 		fmt_print("0x%x ", label->dkl_vtoc.v_timestamp[i]);
1078 	}
1079 	fmt_print("\n");
1080 
1081 	fmt_print("pad:\n");
1082 	dump("", label->dkl_pad, LEN_DKL_PAD, HEX_ONLY);
1083 
1084 	fmt_print("\n\n");
1085 }
1086 #endif	/* FOR_DEBUGGING_ONLY */
1087