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