xref: /titanic_44/usr/src/cmd/format/label.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains the code relating to label manipulation.
31  */
32 
33 #include "global.h"
34 #include "label.h"
35 #include "misc.h"
36 #include "main.h"
37 #include "partition.h"
38 #include "ctlr_scsi.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);
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;
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;
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 	 * If EFI label, then write it out to disk
283 	 */
284 	if (cur_label == L_TYPE_EFI) {
285 	    enter_critical();
286 	    vtoc64 = cur_parts->etoc;
287 	    err_check(vtoc64);
288 	    if (efi_write(cur_file, vtoc64) != 0) {
289 		err_print("Warning: error writing EFI.\n");
290 		error = -1;
291 	    }
292 
293 	    cur_disk->disk_flags |= DSK_LABEL;
294 	    exit_critical();
295 	    return (error);
296 	}
297 
298 	/*
299 	 * Fill in a label structure with the geometry information.
300 	 */
301 	(void) memset((char *)&label, 0, sizeof (struct dk_label));
302 	(void) memset((char *)&new_label, 0, sizeof (struct dk_label));
303 	label.dkl_pcyl = pcyl;
304 	label.dkl_ncyl = ncyl;
305 	label.dkl_acyl = acyl;
306 
307 #if defined(_SUNOS_VTOC_16)
308 	label.dkl_bcyl = bcyl;
309 #endif			/* defined(_SUNOC_VTOC_16) */
310 
311 	label.dkl_nhead = nhead;
312 	label.dkl_nsect = nsect;
313 	label.dkl_apc = apc;
314 	label.dkl_intrlv = 1;
315 	label.dkl_rpm = cur_dtype->dtype_rpm;
316 
317 #if defined(_SUNOS_VTOC_8)
318 	/*
319 	 * Also fill in the current partition information.
320 	 */
321 	for (i = 0; i < NDKMAP; i++) {
322 		label.dkl_map[i] = cur_parts->pinfo_map[i];
323 	}
324 #endif			/* defined(_SUNOS_VTOC_8) */
325 
326 	label.dkl_magic = DKL_MAGIC;
327 
328 	/*
329 	 * Fill in the vtoc information
330 	 */
331 	label.dkl_vtoc = cur_parts->vtoc;
332 
333 	/*
334 	 * Use the current label
335 	 */
336 	bcopy(cur_disk->v_volume, label.dkl_vtoc.v_volume, LEN_DKL_VVOL);
337 
338 	/*
339 	 * Put asciilabel in; on x86 it's in the vtoc, not the label.
340 	 */
341 	(void) snprintf(label.dkl_asciilabel, sizeof (label.dkl_asciilabel),
342 	    "%s cyl %d alt %d hd %d sec %d",
343 	    cur_dtype->dtype_asciilabel, ncyl, acyl, nhead, nsect);
344 
345 #if defined(_SUNOS_VTOC_16)
346 	/*
347 	 * Also add in v_sectorsz, as the driver will.  Everyone
348 	 * else is assuming DEV_BSIZE, so we do the same.
349 	 */
350 	label.dkl_vtoc.v_sectorsz = DEV_BSIZE;
351 #endif			/* defined(_SUNOS_VTOC_16) */
352 
353 	/*
354 	 * Generate the correct checksum.
355 	 */
356 	(void) checksum(&label, CK_MAKESUM);
357 	/*
358 	 * Convert the label into a vtoc
359 	 */
360 	if (label_to_vtoc(&vtoc, &label) == -1) {
361 		return (-1);
362 	}
363 	/*
364 	 * Fill in the geometry info.  This is critical that
365 	 * we do this before writing the vtoc.
366 	 */
367 	bzero((caddr_t)&geom, sizeof (struct dk_geom));
368 	geom.dkg_ncyl = ncyl;
369 	geom.dkg_acyl = acyl;
370 
371 #if defined(_SUNOS_VTOC_16)
372 	geom.dkg_bcyl = bcyl;
373 #endif			/* defined(_SUNOS_VTOC_16) */
374 
375 	geom.dkg_nhead = nhead;
376 	geom.dkg_nsect = nsect;
377 	geom.dkg_intrlv = 1;
378 	geom.dkg_apc = apc;
379 	geom.dkg_rpm = cur_dtype->dtype_rpm;
380 	geom.dkg_pcyl = pcyl;
381 
382 	/*
383 	 * Make a quick check to see that the geometry is being
384 	 * written now is not way off from the actual capacity
385 	 * of the disk. This is only an appoximate check and
386 	 * is only for SCSI disks.
387 	 */
388 	if (SCSI && do_geometry_sanity_check() != 0) {
389 		return (-1);
390 	}
391 
392 	/*
393 	 * Lock out interrupts so we do things in sync.
394 	 */
395 	enter_critical();
396 	/*
397 	 * Do the ioctl to tell the kernel the geometry.
398 	 */
399 	if (ioctl(cur_file, DKIOCSGEOM, &geom) == -1) {
400 		err_print("Warning: error setting drive geometry.\n");
401 		error = -1;
402 	}
403 	/*
404 	 * Write the vtoc.  At the time of this writing, our
405 	 * drivers convert the vtoc back to a label, and
406 	 * then write both the primary and backup labels.
407 	 * This is not a requirement, however, as we
408 	 * always use an ioctl to read the vtoc from the
409 	 * driver, so it can do as it likes.
410 	 */
411 	if (write_vtoc(cur_file, &vtoc) != 0) {
412 		err_print("Warning: error writing VTOC.\n");
413 		error = -1;
414 	}
415 
416 	/*
417 	 * Calculate where the backup labels went.  They are always on
418 	 * the last alternate cylinder, but some older drives put them
419 	 * on head 2 instead of the last head.  They are always on the
420 	 * first 5 odd sectors of the appropriate track.
421 	 */
422 	if (cur_ctype->ctype_flags & CF_BLABEL)
423 		head  = 2;
424 	else
425 		head = nhead - 1;
426 	/*
427 	 * Read and verify the backup labels.
428 	 */
429 	nbackups = 0;
430 	for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect));
431 	    sec += 2) {
432 		if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, (diskaddr_t)
433 			((chs2bn(ncyl + acyl - 1, head, sec)) + solaris_offset),
434 			1, (caddr_t)&new_label, F_NORMAL, NULL)) {
435 			err_print("Warning: error reading backup label.\n");
436 			error = -1;
437 		} else {
438 			if (bcmp((char *)&label, (char *)&new_label,
439 				sizeof (struct dk_label)) == 0) {
440 					nbackups++;
441 			}
442 		}
443 	}
444 	if (nbackups != BAD_LISTCNT) {
445 		err_print("Warning: %s\n", nbackups == 0 ?
446 			"no backup labels" :
447 			"some backup labels incorrect");
448 	}
449 	/*
450 	 * Mark the current disk as labelled and notify the kernel of what
451 	 * has happened.
452 	 */
453 	cur_disk->disk_flags |= DSK_LABEL;
454 
455 	exit_critical();
456 	return (error);
457 }
458 
459 
460 /*
461  * Read the label from the disk.
462  * Do this via the read_vtoc() library routine, then convert it to a label.
463  * We also need a DKIOCGGEOM ioctl to get the disk's geometry.
464  */
465 int
466 read_label(int fd, struct dk_label *label)
467 {
468 	struct vtoc	vtoc;
469 	struct dk_geom	geom;
470 
471 	if (read_vtoc(fd, &vtoc) < 0 || ioctl(fd, DKIOCGGEOM, &geom) == -1) {
472 		return (-1);
473 	}
474 	return (vtoc_to_label(label, &vtoc, &geom));
475 }
476 
477 int
478 get_disk_info_from_devid(int fd, struct efi_info *label)
479 {
480 	ddi_devid_t	devid;
481 	char		*s;
482 	int		n;
483 	char		*vid, *pid;
484 	int		nvid, npid;
485 	struct dk_minfo	minf;
486 	struct dk_cinfo	dkinfo;
487 
488 
489 	if (devid_get(fd, &devid)) {
490 		if (option_msg && diag_msg)
491 			err_print("devid_get failed\n");
492 		return (-1);
493 	}
494 
495 	n = devid_sizeof(devid);
496 	s = (char *)devid;
497 
498 	if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
499 		if (option_msg && diag_msg)
500 			err_print("DKIOCINFO failed\n");
501 		return (-1);
502 	}
503 
504 	if (dkinfo.dki_ctype != DKC_DIRECT)
505 		return (-1);
506 
507 	vid = s+12;
508 	if (!(pid = strchr(vid, '=')))
509 		return (-1);
510 	nvid = pid - vid;
511 	pid += 1;
512 	npid = n - nvid - 13;
513 
514 	if (nvid > 9)
515 		nvid = 9;
516 	if (npid > 17) {
517 		pid = pid + npid - 17;
518 		npid = 17;
519 	}
520 
521 	if (ioctl(fd, DKIOCGMEDIAINFO, &minf) == -1) {
522 		devid_free(devid);
523 		return (-1);
524 	}
525 
526 	(void) strlcpy(label->vendor, vid, nvid);
527 	(void) strlcpy(label->product, pid, npid);
528 	(void) strlcpy(label->revision, "0001", 5);
529 	label->capacity = minf.dki_capacity * minf.dki_lbsize / 512;
530 
531 	devid_free(devid);
532 	return (0);
533 }
534 
535 /*
536  * Issue uscsi_inquiry and read_capacity commands to
537  * retrieve the disk's Vendor, Product, Revision and
538  * Capacity information.
539  */
540 int
541 get_disk_info(int fd, struct efi_info *label)
542 {
543 	struct scsi_inquiry	inquiry;
544 	struct scsi_capacity_16	capacity;
545 
546 	if (!get_disk_info_from_devid(fd, label))
547 		return (0);
548 
549 	if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
550 		err_print("Inquiry failed on %d\n", fd);
551 		return (-1);
552 	}
553 	if (uscsi_read_capacity(fd, &capacity)) {
554 		err_print("Read Capacity failed for %d\n", fd);
555 		return (-1);
556 	}
557 
558 	(void) strlcpy(label->vendor, inquiry.inq_vid, 9);
559 	(void) strlcpy(label->product, inquiry.inq_pid, 17);
560 	(void) strlcpy(label->revision, inquiry.inq_revision, 5);
561 	label->capacity = capacity.sc_capacity;
562 
563 	/* Since we are counting from zero, add 1 to capacity */
564 	label->capacity++;
565 	return (0);
566 }
567 
568 int
569 read_efi_label(int fd, struct efi_info *label)
570 {
571 	struct dk_gpt	*vtoc64;
572 
573 	/* This could fail if there is no label already */
574 	if (efi_alloc_and_read(fd, &vtoc64) < 0) {
575 		return (-1);
576 	}
577 	if (vtoc64_to_label(label, vtoc64) != 0) {
578 		err_print("vtoc64_to_label failed\n");
579 		return (-1);
580 	}
581 	efi_free(vtoc64);
582 	if (get_disk_info(fd, label) != 0) {
583 		return (-1);
584 	}
585 	return (0);
586 }
587 
588 
589 /*
590  * We've read a 64-bit label which has no geometry information.  Use
591  * some heuristics to fake up a geometry that would match the disk in
592  * order to make the rest of format(1M) happy.
593  */
594 static int
595 vtoc64_to_label(struct efi_info *label, struct dk_gpt *vtoc)
596 {
597 	int		i, nparts = 0;
598 	struct dk_gpt	*lmap;
599 
600 	(void) memset((char *)label, 0, sizeof (struct efi_info));
601 
602 	/* XXX do a sanity check here for nparts */
603 	nparts = vtoc->efi_nparts;
604 	lmap = (struct dk_gpt *) calloc(1, (sizeof (struct dk_part) *
605 		    nparts) + sizeof (struct dk_gpt));
606 	if (lmap == NULL) {
607 		err_print("vtoc64_to_label: unable to allocate lmap\n");
608 		fullabort();
609 	}
610 	label->e_parts = lmap;
611 
612 	/*
613 	 * Copy necessary portions
614 	 * XXX Maybe we can use memcpy() ??
615 	 */
616 	lmap->efi_version = vtoc->efi_version;
617 	lmap->efi_nparts = vtoc->efi_nparts;
618 	lmap->efi_part_size = vtoc->efi_part_size;
619 	lmap->efi_lbasize = vtoc->efi_lbasize;
620 	lmap->efi_last_lba = vtoc->efi_last_lba;
621 	lmap->efi_first_u_lba = vtoc->efi_first_u_lba;
622 	lmap->efi_last_u_lba = vtoc->efi_last_u_lba;
623 	lmap->efi_flags = vtoc->efi_flags;
624 	(void) memcpy((uchar_t *)&lmap->efi_disk_uguid,
625 		(uchar_t *)&vtoc->efi_disk_uguid, sizeof (struct uuid));
626 
627 	for (i = 0; i < nparts; i++) {
628 		lmap->efi_parts[i].p_tag = vtoc->efi_parts[i].p_tag;
629 		lmap->efi_parts[i].p_flag = vtoc->efi_parts[i].p_flag;
630 		lmap->efi_parts[i].p_start = vtoc->efi_parts[i].p_start;
631 		lmap->efi_parts[i].p_size = vtoc->efi_parts[i].p_size;
632 		(void) memcpy((uchar_t *)&lmap->efi_parts[i].p_uguid,
633 		    (uchar_t *)&vtoc->efi_parts[i].p_uguid,
634 		    sizeof (struct uuid));
635 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
636 			bcopy(vtoc->efi_parts[i].p_name,
637 			    lmap->efi_parts[i].p_name, LEN_DKL_VVOL);
638 		}
639 	}
640 	return (0);
641 }
642 
643 /*
644  * Convert vtoc/geom to label.
645  */
646 static int
647 vtoc_to_label(struct dk_label *label, struct vtoc *vtoc, struct dk_geom *geom)
648 {
649 #if defined(_SUNOS_VTOC_8)
650 	struct dk_map32		*lmap;
651 #elif defined(_SUNOS_VTOC_16)
652 	struct dkl_partition	*lmap;
653 #else
654 #error No VTOC format defined.
655 #endif			/* defined(_SUNOS_VTOC_8) */
656 
657 	struct partition	*vpart;
658 	long			nblks;
659 	int			i;
660 
661 	(void) memset((char *)label, 0, sizeof (struct dk_label));
662 
663 	/*
664 	 * Sanity-check the vtoc
665 	 */
666 	if (vtoc->v_sanity != VTOC_SANE || vtoc->v_sectorsz != DEV_BSIZE ||
667 			vtoc->v_nparts != V_NUMPAR) {
668 		return (-1);
669 	}
670 
671 	/*
672 	 * Sanity check of geometry
673 	 */
674 	if (geom->dkg_ncyl == 0 || geom->dkg_nhead == 0 ||
675 			geom->dkg_nsect == 0) {
676 		return (-1);
677 	}
678 
679 	label->dkl_magic = DKL_MAGIC;
680 
681 	/*
682 	 * Copy necessary portions of the geometry information
683 	 */
684 	label->dkl_rpm = geom->dkg_rpm;
685 	label->dkl_pcyl = geom->dkg_pcyl;
686 	label->dkl_apc = geom->dkg_apc;
687 	label->dkl_intrlv = geom->dkg_intrlv;
688 	label->dkl_ncyl = geom->dkg_ncyl;
689 	label->dkl_acyl = geom->dkg_acyl;
690 
691 #if defined(_SUNOS_VTOC_16)
692 	label->dkl_bcyl = geom->dkg_bcyl;
693 #endif			/* defined(_SUNOS_VTOC_16) */
694 
695 	label->dkl_nhead = geom->dkg_nhead;
696 	label->dkl_nsect = geom->dkg_nsect;
697 
698 #if defined(_SUNOS_VTOC_8)
699 	label->dkl_obs1 = geom->dkg_obs1;
700 	label->dkl_obs2 = geom->dkg_obs2;
701 	label->dkl_obs3 = geom->dkg_obs3;
702 #endif			/* defined(_SUNOS_VTOC_8) */
703 
704 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
705 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
706 
707 	/*
708 	 * Copy vtoc structure fields into the disk label dk_vtoc
709 	 */
710 	label->dkl_vtoc.v_sanity = vtoc->v_sanity;
711 	label->dkl_vtoc.v_nparts = vtoc->v_nparts;
712 	label->dkl_vtoc.v_version = vtoc->v_version;
713 
714 	(void) memcpy(label->dkl_vtoc.v_volume, vtoc->v_volume,
715 		LEN_DKL_VVOL);
716 	for (i = 0; i < V_NUMPAR; i++) {
717 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
718 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
719 	}
720 	(void) memcpy((char *)label->dkl_vtoc.v_bootinfo,
721 		(char *)vtoc->v_bootinfo, sizeof (vtoc->v_bootinfo));
722 	(void) memcpy((char *)label->dkl_vtoc.v_reserved,
723 		(char *)vtoc->v_reserved, sizeof (vtoc->v_reserved));
724 	(void) memcpy((char *)label->dkl_vtoc.v_timestamp,
725 		(char *)vtoc->timestamp, sizeof (vtoc->timestamp));
726 
727 	(void) memcpy(label->dkl_asciilabel, vtoc->v_asciilabel,
728 		LEN_DKL_ASCII);
729 
730 	/*
731 	 * Note the conversion from starting sector number
732 	 * to starting cylinder number.
733 	 * Return error if division results in a remainder.
734 	 */
735 #if defined(_SUNOS_VTOC_8)
736 	lmap = label->dkl_map;
737 
738 #elif defined(_SUNOS_VTOC_16)
739 	lmap = label->dkl_vtoc.v_part;
740 #else
741 #error No VTOC format defined.
742 #endif			/* defined(_SUNOS_VTOC_8) */
743 
744 	vpart = vtoc->v_part;
745 
746 	nblks = (int)label->dkl_nsect * (int)label->dkl_nhead;
747 
748 	for (i = 0; i < NDKMAP; i++, lmap++, vpart++) {
749 		if ((vpart->p_start % nblks) != 0 ||
750 				(vpart->p_size % nblks) != 0) {
751 			return (-1);
752 		}
753 #if defined(_SUNOS_VTOC_8)
754 		lmap->dkl_cylno = vpart->p_start / nblks;
755 		lmap->dkl_nblk = vpart->p_size;
756 
757 #elif defined(_SUNOS_VTOC_16)
758 		lmap->p_start = vpart->p_start;
759 		lmap->p_size = vpart->p_size;
760 #else
761 #error No VTOC format defined.
762 #endif			/* defined(_SUNOS_VTOC_8) */
763 	}
764 
765 	/*
766 	 * Finally, make a checksum
767 	 */
768 	(void) checksum(label, CK_MAKESUM);
769 
770 	return (0);
771 }
772 
773 
774 
775 /*
776  * Extract a vtoc structure out of a valid label
777  */
778 int
779 label_to_vtoc(struct vtoc *vtoc, struct dk_label *label)
780 {
781 #if defined(_SUNOS_VTOC_8)
782 	struct dk_map2		*lpart;
783 	struct dk_map32		*lmap;
784 	long			nblks;
785 
786 #elif defined(_SUNOS_VTOC_16)
787 	struct dkl_partition	*lpart;
788 #else
789 #error No VTOC format defined.
790 #endif				/* defined(_SUNOS_VTOC_8) */
791 
792 	struct partition	*vpart;
793 	int			i;
794 
795 	(void) memset((char *)vtoc, 0, sizeof (struct vtoc));
796 
797 	switch (label->dkl_vtoc.v_version) {
798 	case 0:
799 		/*
800 		 * No valid vtoc information in the label.
801 		 * Construct default p_flags and p_tags.
802 		 */
803 		vpart = vtoc->v_part;
804 		for (i = 0; i < V_NUMPAR; i++, vpart++) {
805 			vpart->p_tag = default_vtoc_map[i].p_tag;
806 			vpart->p_flag = default_vtoc_map[i].p_flag;
807 		}
808 		break;
809 
810 	case V_VERSION:
811 		vpart = vtoc->v_part;
812 		lpart = label->dkl_vtoc.v_part;
813 		for (i = 0; i < V_NUMPAR; i++, vpart++, lpart++) {
814 			vpart->p_tag = lpart->p_tag;
815 			vpart->p_flag = lpart->p_flag;
816 
817 #if defined(_SUNOS_VTOC_16)
818 			vpart->p_start = lpart->p_start;
819 			vpart->p_size = lpart->p_size;
820 #endif	/* defined(_SUNOS_VTOC_16) */
821 		}
822 		(void) memcpy(vtoc->v_volume, label->dkl_vtoc.v_volume,
823 			LEN_DKL_VVOL);
824 		(void) memcpy((char *)vtoc->v_bootinfo,
825 			(char *)label->dkl_vtoc.v_bootinfo,
826 				sizeof (vtoc->v_bootinfo));
827 		(void) memcpy((char *)vtoc->v_reserved,
828 			(char *)label->dkl_vtoc.v_reserved,
829 				sizeof (vtoc->v_reserved));
830 		(void) memcpy((char *)vtoc->timestamp,
831 			(char *)label->dkl_vtoc.v_timestamp,
832 				sizeof (vtoc->timestamp));
833 		break;
834 
835 	default:
836 		return (-1);
837 	}
838 
839 	/*
840 	 * XXX - this looks wrong to me....
841 	 * why are these values hardwired, rather than returned from
842 	 * the real disk label?
843 	 */
844 	vtoc->v_sanity = VTOC_SANE;
845 	vtoc->v_version = V_VERSION;
846 	vtoc->v_sectorsz = DEV_BSIZE;
847 	vtoc->v_nparts = V_NUMPAR;
848 
849 	(void) memcpy(vtoc->v_asciilabel, label->dkl_asciilabel,
850 		LEN_DKL_ASCII);
851 
852 #if defined(_SUNOS_VTOC_8)
853 	/*
854 	 * Convert partitioning information.
855 	 * Note the conversion from starting cylinder number
856 	 * to starting sector number.
857 	 */
858 	lmap = label->dkl_map;
859 	vpart = vtoc->v_part;
860 	nblks = label->dkl_nsect * label->dkl_nhead;
861 	for (i = 0; i < V_NUMPAR; i++, vpart++, lmap++) {
862 		vpart->p_start = lmap->dkl_cylno * nblks;
863 		vpart->p_size = lmap->dkl_nblk;
864 	}
865 #endif			/* defined(_SUNOS_VTOC_8) */
866 
867 	return (0);
868 }
869 
870 /*
871  * Input: File descriptor
872  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
873  */
874 
875 int
876 is_efi_type(int fd)
877 {
878 	struct vtoc vtoc;
879 
880 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
881 		if (errno == ENOTSUP) {
882 			return (1);
883 		}
884 	}
885 	return (0);
886 }
887 
888 /* make sure the user specified something reasonable */
889 void
890 err_check(struct dk_gpt *vtoc)
891 {
892 	int			resv_part = -1;
893 	int			i, j;
894 	diskaddr_t		istart, jstart, isize, jsize, endsect;
895 	int			overlap = 0;
896 
897 	/*
898 	 * make sure no partitions overlap
899 	 */
900 	for (i = 0; i < vtoc->efi_nparts; i++) {
901 		/* It can't be unassigned and have an actual size */
902 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
903 		    (vtoc->efi_parts[i].p_size != 0)) {
904 			(void) fprintf(stderr,
905 "partition %d is \"unassigned\" but has a size of %llu\n", i,
906 			vtoc->efi_parts[i].p_size);
907 		}
908 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
909 			continue;
910 		}
911 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
912 			if (resv_part != -1) {
913 			    (void) fprintf(stderr,
914 "found duplicate reserved partition at %d\n",
915 				i);
916 			}
917 			resv_part = i;
918 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
919 				(void) fprintf(stderr,
920 "Warning: reserved partition size must be %d sectors\n",
921 				EFI_MIN_RESV_SIZE);
922 		}
923 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
924 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
925 			(void) fprintf(stderr,
926 			    "Partition %d starts at %llu\n",
927 			    i,
928 			    vtoc->efi_parts[i].p_start);
929 			(void) fprintf(stderr,
930 			    "It must be between %llu and %llu.\n",
931 			    vtoc->efi_first_u_lba,
932 			    vtoc->efi_last_u_lba);
933 		}
934 		if ((vtoc->efi_parts[i].p_start +
935 		    vtoc->efi_parts[i].p_size <
936 		    vtoc->efi_first_u_lba) ||
937 		    (vtoc->efi_parts[i].p_start +
938 		    vtoc->efi_parts[i].p_size >
939 		    vtoc->efi_last_u_lba + 1)) {
940 			(void) fprintf(stderr,
941 			    "Partition %d ends at %llu\n",
942 			    i,
943 			    vtoc->efi_parts[i].p_start +
944 			    vtoc->efi_parts[i].p_size);
945 			(void) fprintf(stderr,
946 			    "It must be between %llu and %llu.\n",
947 			    vtoc->efi_first_u_lba,
948 			    vtoc->efi_last_u_lba);
949 		}
950 
951 		for (j = 0; j < vtoc->efi_nparts; j++) {
952 			isize = vtoc->efi_parts[i].p_size;
953 			jsize = vtoc->efi_parts[j].p_size;
954 			istart = vtoc->efi_parts[i].p_start;
955 			jstart = vtoc->efi_parts[j].p_start;
956 			if ((i != j) && (isize != 0) && (jsize != 0)) {
957 				endsect = jstart + jsize -1;
958 				if ((jstart <= istart) &&
959 				    (istart <= endsect)) {
960 					if (!overlap) {
961 					(void) fprintf(stderr,
962 "label error: EFI Labels do not support overlapping partitions\n");
963 					}
964 					(void) fprintf(stderr,
965 "Partition %d overlaps partition %d.\n", i, j);
966 					overlap = 1;
967 				}
968 			}
969 		}
970 	}
971 	/* make sure there is a reserved partition */
972 	if (resv_part == -1) {
973 		(void) fprintf(stderr,
974 			"no reserved partition found\n");
975 	}
976 }
977 
978 #ifdef	FOR_DEBUGGING_ONLY
979 int
980 dump_label(label)
981 	struct dk_label	*label;
982 {
983 	int		i;
984 
985 	fmt_print("%s\n", label->dkl_asciilabel);
986 
987 	fmt_print("version:  %d\n", label->dkl_vtoc.v_version);
988 	fmt_print("volume:   ");
989 	for (i = 0; i < LEN_DKL_VVOL; i++) {
990 		if (label->dkl_vtoc.v_volume[i] == 0)
991 			break;
992 		fmt_print("%c", label->dkl_vtoc.v_volume[i]);
993 	}
994 	fmt_print("\n");
995 	fmt_print("v_nparts: %d\n", label->dkl_vtoc.v_nparts);
996 	fmt_print("v_sanity: %lx\n", label->dkl_vtoc.v_sanity);
997 
998 #if defined(_SUNOS_VTOC_8)
999 	fmt_print("rpm:      %d\n", label->dkl_rpm);
1000 	fmt_print("pcyl:     %d\n", label->dkl_pcyl);
1001 	fmt_print("apc:      %d\n", label->dkl_apc);
1002 	fmt_print("obs1:     %d\n", label->dkl_obs1);
1003 	fmt_print("obs2:     %d\n", label->dkl_obs2);
1004 	fmt_print("intrlv:   %d\n", label->dkl_intrlv);
1005 	fmt_print("ncyl:     %d\n", label->dkl_ncyl);
1006 	fmt_print("acyl:     %d\n", label->dkl_acyl);
1007 	fmt_print("nhead:    %d\n", label->dkl_nhead);
1008 	fmt_print("nsect:    %d\n", label->dkl_nsect);
1009 	fmt_print("obs3:     %d\n", label->dkl_obs3);
1010 	fmt_print("obs4:     %d\n", label->dkl_obs4);
1011 
1012 #elif defined(_SUNOS_VTOC_16)
1013 	fmt_print("rpm:      %d\n", label->dkl_rpm);
1014 	fmt_print("pcyl:     %d\n", label->dkl_pcyl);
1015 	fmt_print("apc:      %d\n", label->dkl_apc);
1016 	fmt_print("intrlv:   %d\n", label->dkl_intrlv);
1017 	fmt_print("ncyl:     %d\n", label->dkl_ncyl);
1018 	fmt_print("acyl:     %d\n", label->dkl_acyl);
1019 	fmt_print("nhead:    %d\n", label->dkl_nhead);
1020 	fmt_print("nsect:    %d\n", label->dkl_nsect);
1021 	fmt_print("bcyl:     %d\n", label->dkl_bcyl);
1022 	fmt_print("skew:     %d\n", label->dkl_skew);
1023 #else
1024 #error No VTOC format defined.
1025 #endif				/* defined(_SUNOS_VTOC_8) */
1026 	fmt_print("magic:    %0x\n", label->dkl_magic);
1027 	fmt_print("cksum:    %0x\n", label->dkl_cksum);
1028 
1029 	for (i = 0; i < NDKMAP; i++) {
1030 
1031 #if defined(_SUNOS_VTOC_8)
1032 		fmt_print("%c:        cyl=%d, blocks=%d", i+'a',
1033 			label->dkl_map[i].dkl_cylno,
1034 			label->dkl_map[i].dkl_nblk);
1035 
1036 #elif defined(_SUNOS_VTOC_16)
1037 		fmt_print("%c:        start=%d, blocks=%d", i+'a',
1038 		    label->dkl_vtoc.v_part[i].p_start,
1039 		    label->dkl_vtoc.v_part[i].p_size);
1040 #else
1041 #error No VTOC format defined.
1042 #endif				/* defined(_SUNOS_VTOC_8) */
1043 
1044 		fmt_print(",  tag=%d,  flag=%d",
1045 			label->dkl_vtoc.v_part[i].p_tag,
1046 			label->dkl_vtoc.v_part[i].p_flag);
1047 		fmt_print("\n");
1048 	}
1049 
1050 	fmt_print("read_reinstruct:  %d\n", label->dkl_read_reinstruct);
1051 	fmt_print("write_reinstruct: %d\n", label->dkl_write_reinstruct);
1052 
1053 	fmt_print("bootinfo: ");
1054 	for (i = 0; i < 3; i++) {
1055 		fmt_print("0x%x ", label->dkl_vtoc.v_bootinfo[i]);
1056 	}
1057 	fmt_print("\n");
1058 
1059 	fmt_print("reserved: ");
1060 	for (i = 0; i < 10; i++) {
1061 		if ((i % 4) == 3)
1062 			fmt_print("\n");
1063 		fmt_print("0x%x ", label->dkl_vtoc.v_reserved[i]);
1064 	}
1065 	fmt_print("\n");
1066 
1067 	fmt_print("timestamp:\n");
1068 	for (i = 0; i < NDKMAP; i++) {
1069 		if ((i % 4) == 3)
1070 			fmt_print("\n");
1071 		fmt_print("0x%x ", label->dkl_vtoc.v_timestamp[i]);
1072 	}
1073 	fmt_print("\n");
1074 
1075 	fmt_print("pad:\n");
1076 	dump("", label->dkl_pad, LEN_DKL_PAD, HEX_ONLY);
1077 
1078 	fmt_print("\n\n");
1079 }
1080 #endif	/* FOR_DEBUGGING_ONLY */
1081