xref: /titanic_44/usr/src/lib/libefi/common/rdwr_efi.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <uuid/uuid.h>
34 #include <libintl.h>
35 #include <sys/types.h>
36 #include <sys/dkio.h>
37 #include <sys/vtoc.h>
38 #include <sys/mhd.h>
39 #include <sys/param.h>
40 #include <sys/dktp/fdisk.h>
41 #include <sys/efi_partition.h>
42 #include <sys/byteorder.h>
43 #include <sys/ddi.h>
44 
45 static struct uuid_to_ptag {
46 	struct uuid	uuid;
47 } conversion_array[] = {
48 	{ EFI_UNUSED },
49 	{ EFI_BOOT },
50 	{ EFI_ROOT },
51 	{ EFI_SWAP },
52 	{ EFI_USR },
53 	{ EFI_BACKUP },
54 	{ 0 },			/* STAND is never used */
55 	{ EFI_VAR },
56 	{ EFI_HOME },
57 	{ EFI_ALTSCTR },
58 	{ 0 },			/* CACHE (cachefs) is never used */
59 	{ EFI_RESERVED },
60 	{ EFI_SYSTEM },
61 	{ EFI_LEGACY_MBR },
62 	{ EFI_RESV3 },
63 	{ EFI_RESV4 },
64 	{ EFI_MSFT_RESV },
65 	{ EFI_DELL_BASIC },
66 	{ EFI_DELL_RAID },
67 	{ EFI_DELL_SWAP },
68 	{ EFI_DELL_LVM },
69 	{ EFI_DELL_RESV },
70 	{ EFI_AAPL_HFS },
71 	{ EFI_AAPL_UFS }
72 };
73 
74 /*
75  * Default vtoc information for non-SVr4 partitions
76  */
77 struct dk_map2  default_vtoc_map[NDKMAP] = {
78 	{	V_ROOT,		0	},		/* a - 0 */
79 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
80 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
81 	{	V_UNASSIGNED,	0	},		/* d - 3 */
82 	{	V_UNASSIGNED,	0	},		/* e - 4 */
83 	{	V_UNASSIGNED,	0	},		/* f - 5 */
84 	{	V_USR,		0	},		/* g - 6 */
85 	{	V_UNASSIGNED,	0	},		/* h - 7 */
86 
87 #if defined(_SUNOS_VTOC_16)
88 
89 #if defined(i386) || defined(__amd64)
90 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
91 	{	V_ALTSCTR,	0	},		/* j - 9 */
92 
93 #else
94 #error No VTOC format defined.
95 #endif			/* defined(i386) */
96 
97 	{	V_UNASSIGNED,	0	},		/* k - 10 */
98 	{	V_UNASSIGNED,	0	},		/* l - 11 */
99 	{	V_UNASSIGNED,	0	},		/* m - 12 */
100 	{	V_UNASSIGNED,	0	},		/* n - 13 */
101 	{	V_UNASSIGNED,	0	},		/* o - 14 */
102 	{	V_UNASSIGNED,	0	},		/* p - 15 */
103 #endif			/* defined(_SUNOS_VTOC_16) */
104 };
105 
106 /*
107  * This is the size of the reserved partition.
108  * Valid in case of EFI labels.
109  */
110 #define	EFI_MIN_RESV_SIZE	(16 * 1024)
111 
112 #ifdef DEBUG
113 int efi_debug = 1;
114 #else
115 int efi_debug = 0;
116 #endif
117 
118 extern unsigned int	efi_crc32(const unsigned char *, unsigned int);
119 static int		efi_read(int, struct dk_gpt *);
120 
121 static int
122 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
123 {
124 	struct dk_minfo		disk_info;
125 
126 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
127 		return (errno);
128 	*capacity = disk_info.dki_capacity;
129 	*lbsize = disk_info.dki_lbsize;
130 	return (0);
131 }
132 
133 /*
134  * the number of blocks the EFI label takes up (round up to nearest
135  * block)
136  */
137 #define	NBLOCKS(p, l)	(1 + ((((p) * (int)sizeof (efi_gpe_t))  + \
138 				((l) - 1)) / (l)))
139 /* number of partitions -- limited by what we can malloc */
140 #define	MAX_PARTS	((4294967295UL - sizeof (struct dk_gpt)) / \
141 			    sizeof (struct dk_part))
142 
143 int
144 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
145 {
146 	diskaddr_t	capacity;
147 	uint_t		lbsize;
148 	uint_t		nblocks;
149 	size_t		length;
150 	struct dk_gpt	*vptr;
151 	struct uuid	uuid;
152 
153 	if (read_disk_info(fd, &capacity, &lbsize) != 0) {
154 		if (efi_debug)
155 			(void) fprintf(stderr,
156 			    "couldn't read disk information\n");
157 		return (-1);
158 	}
159 
160 	nblocks = NBLOCKS(nparts, lbsize);
161 	if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
162 		/* 16K plus one block for the GPT */
163 		nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
164 	}
165 
166 	if (nparts > MAX_PARTS) {
167 		if (efi_debug) {
168 			(void) fprintf(stderr,
169 			"the maximum number of partitions supported is %lu\n",
170 			    MAX_PARTS);
171 		}
172 		return (-1);
173 	}
174 
175 	length = sizeof (struct dk_gpt) +
176 	    sizeof (struct dk_part) * (nparts - 1);
177 
178 	if ((*vtoc = calloc(length, 1)) == NULL)
179 		return (-1);
180 
181 	vptr = *vtoc;
182 
183 	vptr->efi_version = EFI_VERSION_CURRENT;
184 	vptr->efi_lbasize = lbsize;
185 	vptr->efi_nparts = nparts;
186 	/*
187 	 * add one block here for the PMBR; on disks with a 512 byte
188 	 * block size and 128 or fewer partitions, efi_first_u_lba
189 	 * should work out to "34"
190 	 */
191 	vptr->efi_first_u_lba = nblocks + 1;
192 	vptr->efi_last_lba = capacity - 1;
193 	vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
194 	(void) uuid_generate((uchar_t *)&uuid);
195 	UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
196 	return (0);
197 }
198 
199 /*
200  * Read EFI - return partition number upon success.
201  */
202 int
203 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
204 {
205 	int			rval;
206 	uint32_t		nparts;
207 	int			length;
208 
209 	/* figure out the number of entries that would fit into 16K */
210 	nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
211 	length = (int) sizeof (struct dk_gpt) +
212 			    (int) sizeof (struct dk_part) * (nparts - 1);
213 	if ((*vtoc = calloc(length, 1)) == NULL)
214 		return (VT_ERROR);
215 
216 	(*vtoc)->efi_nparts = nparts;
217 	rval = efi_read(fd, *vtoc);
218 
219 	if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
220 		void *tmp;
221 		length = (int) sizeof (struct dk_gpt) +
222 				(int) sizeof (struct dk_part) *
223 				((*vtoc)->efi_nparts - 1);
224 		nparts = (*vtoc)->efi_nparts;
225 		if ((tmp = realloc(*vtoc, length)) == NULL) {
226 			free (*vtoc);
227 			*vtoc = NULL;
228 			return (VT_ERROR);
229 		} else {
230 			*vtoc = tmp;
231 			rval = efi_read(fd, *vtoc);
232 		}
233 	}
234 
235 	if (rval < 0) {
236 		if (efi_debug) {
237 			(void) fprintf(stderr,
238 			    "read of EFI table failed, rval=%d\n", rval);
239 		}
240 		free (*vtoc);
241 		*vtoc = NULL;
242 	}
243 
244 	return (rval);
245 }
246 
247 static int
248 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
249 {
250 	void *data = dk_ioc->dki_data;
251 	int error;
252 
253 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
254 	error = ioctl(fd, cmd, (void *)dk_ioc);
255 	dk_ioc->dki_data = data;
256 
257 	return (error);
258 }
259 
260 static int
261 check_label(int fd, dk_efi_t *dk_ioc)
262 {
263 	efi_gpt_t		*efi;
264 	uint_t			crc;
265 
266 	if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
267 		switch (errno) {
268 		case EIO:
269 			return (VT_EIO);
270 		default:
271 			return (VT_ERROR);
272 		}
273 	}
274 	efi = dk_ioc->dki_data;
275 	if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
276 		if (efi_debug)
277 			(void) fprintf(stderr,
278 			    "Bad EFI signature: 0x%llx != 0x%llx\n",
279 			    (long long)efi->efi_gpt_Signature,
280 			    (long long)LE_64(EFI_SIGNATURE));
281 		return (VT_EINVAL);
282 	}
283 
284 	/*
285 	 * check CRC of the header; the size of the header should
286 	 * never be larger than one block
287 	 */
288 	crc = efi->efi_gpt_HeaderCRC32;
289 	efi->efi_gpt_HeaderCRC32 = 0;
290 
291 	if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
292 	    crc != LE_32(efi_crc32((unsigned char *)efi,
293 	    LE_32(efi->efi_gpt_HeaderSize)))) {
294 		if (efi_debug)
295 			(void) fprintf(stderr,
296 				"Bad EFI CRC: 0x%x != 0x%x\n",
297 				crc,
298 				LE_32(efi_crc32((unsigned char *)efi,
299 				    sizeof (struct efi_gpt))));
300 		return (VT_EINVAL);
301 	}
302 
303 	return (0);
304 }
305 
306 static int
307 efi_read(int fd, struct dk_gpt *vtoc)
308 {
309 	int			i, j;
310 	int			label_len;
311 	int			rval = 0;
312 	int			md_flag = 0;
313 	struct dk_minfo		disk_info;
314 	dk_efi_t		dk_ioc;
315 	efi_gpt_t		*efi;
316 	efi_gpe_t		*efi_parts;
317 	struct dk_cinfo		dki_info;
318 	uint32_t		user_length;
319 
320 	/*
321 	 * get the partition number for this file descriptor.
322 	 */
323 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
324 		if (efi_debug)
325 		    (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
326 		switch (errno) {
327 		case EIO:
328 			return (VT_EIO);
329 		case EINVAL:
330 			return (VT_EINVAL);
331 		default:
332 			return (VT_ERROR);
333 		}
334 	}
335 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
336 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
337 		md_flag++;
338 	}
339 	/* get the LBA size */
340 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
341 		if (efi_debug) {
342 			(void) fprintf(stderr,
343 			    "assuming LBA 512 bytes %d\n",
344 			    errno);
345 		}
346 		disk_info.dki_lbsize = DEV_BSIZE;
347 	}
348 	if (disk_info.dki_lbsize == 0) {
349 		if (efi_debug) {
350 			(void) fprintf(stderr,
351 			    "efi_read: assuming LBA 512 bytes\n");
352 		}
353 		disk_info.dki_lbsize = DEV_BSIZE;
354 	}
355 	/*
356 	 * Read the EFI GPT to figure out how many partitions we need
357 	 * to deal with.
358 	 */
359 	dk_ioc.dki_lba = 1;
360 	if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
361 		label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
362 	} else {
363 		label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
364 				    disk_info.dki_lbsize;
365 		if (label_len % disk_info.dki_lbsize) {
366 			/* pad to physical sector size */
367 			label_len += disk_info.dki_lbsize;
368 			label_len &= ~(disk_info.dki_lbsize - 1);
369 		}
370 	}
371 
372 	if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
373 		return (VT_ERROR);
374 
375 	dk_ioc.dki_length = label_len;
376 	user_length = vtoc->efi_nparts;
377 	efi = dk_ioc.dki_data;
378 	if (md_flag) {
379 		if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
380 			switch (errno) {
381 			case EIO:
382 				return (VT_EIO);
383 			default:
384 				return (VT_ERROR);
385 			}
386 		}
387 	} else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
388 		/* no valid label here; try the alternate */
389 		dk_ioc.dki_lba = disk_info.dki_capacity - 1;
390 		dk_ioc.dki_length = disk_info.dki_lbsize;
391 		rval = check_label(fd, &dk_ioc);
392 		if (rval != 0) {
393 			/*
394 			 * This is a workaround for legacy systems.
395 			 *
396 			 * In the past, the last sector of SCSI disk was
397 			 * invisible on x86 platform. At that time, backup
398 			 * label was saved on the next to the last sector.
399 			 * It is possible for users to move a disk from
400 			 * previous solaris system to present system.
401 			 */
402 			dk_ioc.dki_lba = disk_info.dki_capacity - 2;
403 			dk_ioc.dki_length = disk_info.dki_lbsize;
404 			rval = check_label(fd, &dk_ioc);
405 			if (efi_debug && (rval == 0)) {
406 				(void) fprintf(stderr,
407 				    "efi_read: primary label corrupt; "
408 				    "using legacy EFI backup label\n");
409 			}
410 		}
411 
412 		if (rval == 0) {
413 			if (efi_debug) {
414 				(void) fprintf(stderr,
415 				    "efi_read: primary label corrupt; "
416 				    "using backup\n");
417 			}
418 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
419 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
420 			vtoc->efi_nparts =
421 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
422 			/*
423 			 * partitions are between last usable LBA and
424 			 * backup partition header
425 			 */
426 			dk_ioc.dki_data++;
427 			dk_ioc.dki_length = disk_info.dki_capacity -
428 						    dk_ioc.dki_lba - 1;
429 			dk_ioc.dki_length *= disk_info.dki_lbsize;
430 			if (dk_ioc.dki_length > (len_t)label_len) {
431 				rval = VT_EINVAL;
432 			} else {
433 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
434 			}
435 		}
436 	}
437 	if (rval < 0) {
438 		free(efi);
439 		return (rval);
440 	}
441 
442 	/* LINTED -- always longlong aligned */
443 	efi_parts = (efi_gpe_t *)(((char *)efi) +
444 	    LE_64(efi->efi_gpt_PartitionEntryLBA) * disk_info.dki_lbsize);
445 
446 	/*
447 	 * Assemble this into a "dk_gpt" struct for easier
448 	 * digestibility by applications.
449 	 */
450 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
451 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
452 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
453 	vtoc->efi_lbasize = disk_info.dki_lbsize;
454 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
455 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
456 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
457 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
458 
459 	/*
460 	 * If the array the user passed in is too small, set the length
461 	 * to what it needs to be and return
462 	 */
463 	if (user_length < vtoc->efi_nparts) {
464 		return (VT_EINVAL);
465 	}
466 
467 	for (i = 0; i < vtoc->efi_nparts; i++) {
468 
469 	    UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
470 		efi_parts[i].efi_gpe_PartitionTypeGUID);
471 
472 	    for (j = 0;
473 		j < sizeof (conversion_array) / sizeof (struct uuid_to_ptag);
474 		j++) {
475 
476 		    if (bcmp(&vtoc->efi_parts[i].p_guid,
477 			&conversion_array[j].uuid,
478 			sizeof (struct uuid)) == 0) {
479 			    vtoc->efi_parts[i].p_tag = j;
480 			    break;
481 		    }
482 	    }
483 	    if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
484 		    continue;
485 	    vtoc->efi_parts[i].p_flag =
486 		LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
487 	    vtoc->efi_parts[i].p_start =
488 		LE_64(efi_parts[i].efi_gpe_StartingLBA);
489 	    vtoc->efi_parts[i].p_size =
490 		LE_64(efi_parts[i].efi_gpe_EndingLBA) -
491 		    vtoc->efi_parts[i].p_start + 1;
492 	    for (j = 0; j < EFI_PART_NAME_LEN; j++) {
493 		vtoc->efi_parts[i].p_name[j] =
494 		    (uchar_t)LE_16(efi_parts[i].efi_gpe_PartitionName[j]);
495 	    }
496 
497 	    UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
498 		efi_parts[i].efi_gpe_UniquePartitionGUID);
499 	}
500 	free(efi);
501 
502 	return (dki_info.dki_partition);
503 }
504 
505 /* writes a "protective" MBR */
506 static int
507 write_pmbr(int fd, struct dk_gpt *vtoc)
508 {
509 	dk_efi_t	dk_ioc;
510 	struct mboot	mb;
511 	uchar_t		*cp;
512 	diskaddr_t	size_in_lba;
513 
514 	mb.signature = LE_16(MBB_MAGIC);
515 	bzero(&mb.parts, sizeof (mb.parts));
516 	cp = (uchar_t *)&mb.parts[0];
517 	/* bootable or not */
518 	*cp++ = 0;
519 	/* beginning CHS; 0xffffff if not representable */
520 	*cp++ = 0xff;
521 	*cp++ = 0xff;
522 	*cp++ = 0xff;
523 	/* OS type */
524 	*cp++ = EFI_PMBR;
525 	/* ending CHS; 0xffffff if not representable */
526 	*cp++ = 0xff;
527 	*cp++ = 0xff;
528 	*cp++ = 0xff;
529 	/* starting LBA: 1 (little endian format) by EFI definition */
530 	*cp++ = 0x01;
531 	*cp++ = 0x00;
532 	*cp++ = 0x00;
533 	*cp++ = 0x00;
534 	/* ending LBA: last block on the disk (little endian format) */
535 	size_in_lba = vtoc->efi_last_lba;
536 	if (size_in_lba < 0xffffffff) {
537 		*cp++ = (size_in_lba & 0x000000ff);
538 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
539 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
540 		*cp++ = (size_in_lba & 0xff000000) >> 24;
541 	} else {
542 		*cp++ = 0xff;
543 		*cp++ = 0xff;
544 		*cp++ = 0xff;
545 		*cp++ = 0xff;
546 	}
547 	/* LINTED -- always longlong aligned */
548 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
549 	dk_ioc.dki_lba = 0;
550 	dk_ioc.dki_length = sizeof (mb);
551 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
552 		switch (errno) {
553 		case EIO:
554 			return (VT_EIO);
555 		case EINVAL:
556 			return (VT_EINVAL);
557 		default:
558 			return (VT_ERROR);
559 		}
560 	}
561 	return (0);
562 }
563 
564 /* make sure the user specified something reasonable */
565 static int
566 check_input(struct dk_gpt *vtoc)
567 {
568 	int			resv_part = -1;
569 	int			i, j;
570 	diskaddr_t		istart, jstart, isize, jsize, endsect;
571 
572 	/*
573 	 * Sanity-check the input (make sure no partitions overlap)
574 	 */
575 	for (i = 0; i < vtoc->efi_nparts; i++) {
576 		/* It can't be unassigned and have an actual size */
577 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
578 		    (vtoc->efi_parts[i].p_size != 0)) {
579 			if (efi_debug) {
580 				(void) fprintf(stderr,
581 "partition %d is \"unassigned\" but has a size of %llu",
582 				    i,
583 				    vtoc->efi_parts[i].p_size);
584 			}
585 			return (VT_EINVAL);
586 		}
587 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
588 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
589 				continue;
590 			/* we have encountered an unknown uuid */
591 			vtoc->efi_parts[i].p_tag = 0xff;
592 		}
593 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
594 			if (resv_part != -1) {
595 				if (efi_debug) {
596 				    (void) fprintf(stderr,
597 "found duplicate reserved partition at %d\n",
598 					i);
599 				}
600 				return (VT_EINVAL);
601 			}
602 			resv_part = i;
603 		}
604 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
605 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
606 			if (efi_debug) {
607 				(void) fprintf(stderr,
608 				    "Partition %d starts at %llu.  ",
609 				    i,
610 				    vtoc->efi_parts[i].p_start);
611 				(void) fprintf(stderr,
612 				    "It must be between %llu and %llu.\n",
613 				    vtoc->efi_first_u_lba,
614 				    vtoc->efi_last_u_lba);
615 			}
616 			return (VT_EINVAL);
617 		}
618 		if ((vtoc->efi_parts[i].p_start +
619 		    vtoc->efi_parts[i].p_size <
620 		    vtoc->efi_first_u_lba) ||
621 		    (vtoc->efi_parts[i].p_start +
622 		    vtoc->efi_parts[i].p_size >
623 		    vtoc->efi_last_u_lba + 1)) {
624 			if (efi_debug) {
625 				(void) fprintf(stderr,
626 				    "Partition %d ends at %llu.  ",
627 				    i,
628 				    vtoc->efi_parts[i].p_start +
629 				    vtoc->efi_parts[i].p_size);
630 				(void) fprintf(stderr,
631 				    "It must be between %llu and %llu.\n",
632 				    vtoc->efi_first_u_lba,
633 				    vtoc->efi_last_u_lba);
634 			}
635 			return (VT_EINVAL);
636 		}
637 
638 		for (j = 0; j < vtoc->efi_nparts; j++) {
639 			isize = vtoc->efi_parts[i].p_size;
640 			jsize = vtoc->efi_parts[j].p_size;
641 			istart = vtoc->efi_parts[i].p_start;
642 			jstart = vtoc->efi_parts[j].p_start;
643 			if ((i != j) && (isize != 0) && (jsize != 0)) {
644 				endsect = jstart + jsize -1;
645 				if ((jstart <= istart) &&
646 				    (istart <= endsect)) {
647 					if (efi_debug) {
648 						(void) fprintf(stderr,
649 "Partition %d overlaps partition %d.",
650 						    i, j);
651 					    }
652 					    return (VT_EINVAL);
653 				}
654 			}
655 		}
656 	}
657 	/* just a warning for now */
658 	if ((resv_part == -1) && efi_debug) {
659 		(void) fprintf(stderr,
660 				"no reserved partition found\n");
661 	}
662 	return (0);
663 }
664 
665 /*
666  * write EFI label and backup label
667  */
668 int
669 efi_write(int fd, struct dk_gpt *vtoc)
670 {
671 	dk_efi_t		dk_ioc;
672 	efi_gpt_t		*efi;
673 	efi_gpe_t		*efi_parts;
674 	int			i, j;
675 	struct dk_cinfo		dki_info;
676 	int			md_flag = 0;
677 
678 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
679 		if (efi_debug)
680 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
681 		switch (errno) {
682 		case EIO:
683 			return (VT_EIO);
684 		case EINVAL:
685 			return (VT_EINVAL);
686 		default:
687 			return (VT_ERROR);
688 		}
689 	}
690 
691 	/* check if we are dealing wih a metadevice */
692 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
693 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
694 		md_flag = 1;
695 	}
696 
697 	if (check_input(vtoc)) {
698 		/*
699 		 * not valid; if it's a metadevice just pass it down
700 		 * because SVM will do its own checking
701 		 */
702 		if (md_flag == 0) {
703 			return (VT_EINVAL);
704 		}
705 	}
706 
707 	dk_ioc.dki_lba = 1;
708 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
709 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
710 	} else {
711 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
712 				    vtoc->efi_lbasize) *
713 				    vtoc->efi_lbasize;
714 	}
715 
716 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
717 		return (VT_ERROR);
718 
719 	efi = dk_ioc.dki_data;
720 
721 	/* stuff user's input into EFI struct */
722 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
723 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
724 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
725 	efi->efi_gpt_Reserved1 = 0;
726 	efi->efi_gpt_MyLBA = LE_64(1ULL);
727 	efi->efi_gpt_AlternateLBA = LE_64(vtoc->efi_last_lba);
728 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
729 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
730 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
731 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
732 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
733 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
734 
735 	/* LINTED -- always longlong aligned */
736 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + sizeof (efi_gpt_t));
737 
738 	for (i = 0; i < vtoc->efi_nparts; i++) {
739 	    for (j = 0;
740 		j < sizeof (conversion_array) / sizeof (struct uuid_to_ptag);
741 		j++) {
742 
743 		    if (vtoc->efi_parts[i].p_tag == j) {
744 			    UUID_LE_CONVERT(
745 				efi_parts[i].efi_gpe_PartitionTypeGUID,
746 				conversion_array[j].uuid);
747 			    break;
748 		    }
749 	    }
750 
751 	    if (j == sizeof (conversion_array) / sizeof (struct uuid_to_ptag)) {
752 		/*
753 		 * If we didn't have a matching uuid match, bail here.
754 		 * Don't write a label with unknown uuid.
755 		 */
756 		if (efi_debug)
757 		    (void) fprintf(stderr, "Unknown uuid for p_tag %d\n",
758 			vtoc->efi_parts[i].p_tag);
759 		return (VT_EINVAL);
760 	    }
761 
762 	    efi_parts[i].efi_gpe_StartingLBA =
763 		LE_64(vtoc->efi_parts[i].p_start);
764 	    efi_parts[i].efi_gpe_EndingLBA =
765 		LE_64(vtoc->efi_parts[i].p_start +
766 		vtoc->efi_parts[i].p_size - 1);
767 	    efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
768 		    LE_16(vtoc->efi_parts[i].p_flag);
769 	    for (j = 0; j < EFI_PART_NAME_LEN; j++) {
770 		    efi_parts[i].efi_gpe_PartitionName[j] =
771 			LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
772 	    }
773 	    if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
774 		uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
775 		    (void) uuid_generate((uchar_t *)
776 			&vtoc->efi_parts[i].p_uguid);
777 	    }
778 	    bcopy(&vtoc->efi_parts[i].p_uguid,
779 		&efi_parts[i].efi_gpe_UniquePartitionGUID,
780 		sizeof (uuid_t));
781 	}
782 	efi->efi_gpt_PartitionEntryArrayCRC32 =
783 	    LE_32(efi_crc32((unsigned char *)efi_parts,
784 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
785 	efi->efi_gpt_HeaderCRC32 =
786 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
787 
788 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
789 		free(dk_ioc.dki_data);
790 		switch (errno) {
791 		case EIO:
792 			return (VT_EIO);
793 		case EINVAL:
794 			return (VT_EINVAL);
795 		default:
796 			return (VT_ERROR);
797 		}
798 	}
799 	/* if it's a metadevice we're done */
800 	if (md_flag) {
801 		free(dk_ioc.dki_data);
802 		return (0);
803 	}
804 	/* write backup partition array */
805 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
806 	dk_ioc.dki_length -= vtoc->efi_lbasize;
807 	dk_ioc.dki_data++;
808 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
809 		/*
810 		 * we wrote the primary label okay, so don't fail
811 		 */
812 		if (efi_debug) {
813 			(void) fprintf(stderr,
814 			    "write of backup partitions to block %llu "
815 			    "failed, errno %d\n",
816 			    vtoc->efi_last_u_lba + 1,
817 			    errno);
818 		}
819 	}
820 	/*
821 	 * now swap MyLBA and AlternateLBA fields and write backup
822 	 * partition table header
823 	 */
824 	dk_ioc.dki_lba = vtoc->efi_last_lba;
825 	dk_ioc.dki_length = vtoc->efi_lbasize;
826 	dk_ioc.dki_data--;
827 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
828 	efi->efi_gpt_MyLBA = LE_64(vtoc->efi_last_lba);
829 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
830 	efi->efi_gpt_HeaderCRC32 = 0;
831 	efi->efi_gpt_HeaderCRC32 =
832 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
833 	    sizeof (struct efi_gpt)));
834 
835 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
836 		if (efi_debug) {
837 			(void) fprintf(stderr,
838 			    "write of backup header to block %llu failed, "
839 			    "errno %d\n",
840 			    vtoc->efi_last_lba,
841 			    errno);
842 		}
843 	}
844 	/* write the PMBR */
845 	(void) write_pmbr(fd, vtoc);
846 	free(dk_ioc.dki_data);
847 	return (0);
848 }
849 
850 void
851 efi_free(struct dk_gpt *ptr)
852 {
853 	free(ptr);
854 }
855 
856 /*
857  * Input: File descriptor
858  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
859  */
860 int
861 efi_type(int fd)
862 {
863 	struct vtoc vtoc;
864 
865 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
866 		if (errno == ENOTSUP) {
867 			return (1);
868 		}
869 	}
870 	return (0);
871 }
872 
873 void
874 efi_err_check(struct dk_gpt *vtoc)
875 {
876 	int			resv_part = -1;
877 	int			i, j;
878 	diskaddr_t		istart, jstart, isize, jsize, endsect;
879 	int			overlap = 0;
880 
881 	/*
882 	 * make sure no partitions overlap
883 	 */
884 	for (i = 0; i < vtoc->efi_nparts; i++) {
885 		/* It can't be unassigned and have an actual size */
886 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
887 		    (vtoc->efi_parts[i].p_size != 0)) {
888 			(void) fprintf(stderr,
889 			    "partition %d is \"unassigned\" but has a size "
890 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
891 		}
892 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
893 			continue;
894 		}
895 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
896 			if (resv_part != -1) {
897 				(void) fprintf(stderr,
898 				    "found duplicate reserved partition at "
899 				    "%d\n", i);
900 			}
901 			resv_part = i;
902 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
903 				(void) fprintf(stderr,
904 				    "Warning: reserved partition size must "
905 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
906 		}
907 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
908 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
909 			(void) fprintf(stderr,
910 			    "Partition %d starts at %llu\n",
911 			    i,
912 			    vtoc->efi_parts[i].p_start);
913 			(void) fprintf(stderr,
914 			    "It must be between %llu and %llu.\n",
915 			    vtoc->efi_first_u_lba,
916 			    vtoc->efi_last_u_lba);
917 		}
918 		if ((vtoc->efi_parts[i].p_start +
919 		    vtoc->efi_parts[i].p_size <
920 		    vtoc->efi_first_u_lba) ||
921 		    (vtoc->efi_parts[i].p_start +
922 		    vtoc->efi_parts[i].p_size >
923 		    vtoc->efi_last_u_lba + 1)) {
924 			(void) fprintf(stderr,
925 			    "Partition %d ends at %llu\n",
926 			    i,
927 			    vtoc->efi_parts[i].p_start +
928 			    vtoc->efi_parts[i].p_size);
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 
935 		for (j = 0; j < vtoc->efi_nparts; j++) {
936 			isize = vtoc->efi_parts[i].p_size;
937 			jsize = vtoc->efi_parts[j].p_size;
938 			istart = vtoc->efi_parts[i].p_start;
939 			jstart = vtoc->efi_parts[j].p_start;
940 			if ((i != j) && (isize != 0) && (jsize != 0)) {
941 				endsect = jstart + jsize -1;
942 				if ((jstart <= istart) &&
943 				    (istart <= endsect)) {
944 					if (!overlap) {
945 					(void) fprintf(stderr,
946 					    "label error: EFI Labels do not "
947 					    "support overlapping partitions\n");
948 					}
949 					(void) fprintf(stderr,
950 					    "Partition %d overlaps partition "
951 					    "%d.\n", i, j);
952 					overlap = 1;
953 				}
954 			}
955 		}
956 	}
957 	/* make sure there is a reserved partition */
958 	if (resv_part == -1) {
959 		(void) fprintf(stderr,
960 			"no reserved partition found\n");
961 	}
962 }
963 
964 /*
965  * We need to get information necessary to construct a *new* efi
966  * label type
967  */
968 int
969 efi_auto_sense(int fd, struct dk_gpt **vtoc)
970 {
971 
972 	int	i;
973 
974 	/*
975 	 * Now build the default partition table
976 	 */
977 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
978 		if (efi_debug) {
979 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
980 		}
981 		return (-1);
982 	}
983 
984 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
985 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
986 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
987 		(*vtoc)->efi_parts[i].p_start = 0;
988 		(*vtoc)->efi_parts[i].p_size = 0;
989 	}
990 	/*
991 	 * Make constants first
992 	 * and variable partitions later
993 	 */
994 
995 	/* root partition - s0 128 MB */
996 	(*vtoc)->efi_parts[0].p_start = 34;
997 	(*vtoc)->efi_parts[0].p_size = 262144;
998 
999 	/* partition - s1  128 MB */
1000 	(*vtoc)->efi_parts[1].p_start = 262178;
1001 	(*vtoc)->efi_parts[1].p_size = 262144;
1002 
1003 	/* partition -s2 is NOT the Backup disk */
1004 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1005 
1006 	/* partition -s6 /usr partition - HOG */
1007 	(*vtoc)->efi_parts[6].p_start = 524322;
1008 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1009 	    - (1024 * 16);
1010 
1011 	/* efi reserved partition - s9 16K */
1012 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1013 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
1014 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
1015 	return (0);
1016 }
1017