xref: /titanic_44/usr/src/lib/libefi/common/rdwr_efi.c (revision b81bb599d2182818b41e3bbe6d4d1771b9d484d4)
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 = disk_info.dki_lbsize;
376 	user_length = vtoc->efi_nparts;
377 	efi = dk_ioc.dki_data;
378 	if (md_flag) {
379 		dk_ioc.dki_length = label_len;
380 		if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
381 			switch (errno) {
382 			case EIO:
383 				return (VT_EIO);
384 			default:
385 				return (VT_ERROR);
386 			}
387 		}
388 	} else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
389 		/* no valid label here; try the alternate */
390 		dk_ioc.dki_lba = disk_info.dki_capacity - 1;
391 		dk_ioc.dki_length = disk_info.dki_lbsize;
392 		rval = check_label(fd, &dk_ioc);
393 		if (rval != 0) {
394 			/*
395 			 * This is a workaround for legacy systems.
396 			 *
397 			 * In the past, the last sector of SCSI disk was
398 			 * invisible on x86 platform. At that time, backup
399 			 * label was saved on the next to the last sector.
400 			 * It is possible for users to move a disk from
401 			 * previous solaris system to present system.
402 			 */
403 			dk_ioc.dki_lba = disk_info.dki_capacity - 2;
404 			dk_ioc.dki_length = disk_info.dki_lbsize;
405 			rval = check_label(fd, &dk_ioc);
406 			if (efi_debug && (rval == 0)) {
407 				(void) fprintf(stderr,
408 				    "efi_read: primary label corrupt; "
409 				    "using legacy EFI backup label\n");
410 			}
411 		}
412 
413 		if (rval == 0) {
414 			if (efi_debug) {
415 				(void) fprintf(stderr,
416 				    "efi_read: primary label corrupt; "
417 				    "using backup\n");
418 			}
419 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
420 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
421 			vtoc->efi_nparts =
422 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
423 			/*
424 			 * partitions are between last usable LBA and
425 			 * backup partition header
426 			 */
427 			dk_ioc.dki_data++;
428 			dk_ioc.dki_length = disk_info.dki_capacity -
429 						    dk_ioc.dki_lba - 1;
430 			dk_ioc.dki_length *= disk_info.dki_lbsize;
431 			if (dk_ioc.dki_length > (len_t)label_len) {
432 				rval = VT_EINVAL;
433 			} else {
434 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
435 			}
436 		}
437 	} else {
438 		dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
439 		dk_ioc.dki_data++;
440 		dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
441 		rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
442 	}
443 	if (rval < 0) {
444 		free(efi);
445 		return (rval);
446 	}
447 
448 	/* LINTED -- always longlong aligned */
449 	efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
450 
451 	/*
452 	 * Assemble this into a "dk_gpt" struct for easier
453 	 * digestibility by applications.
454 	 */
455 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
456 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
457 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
458 	vtoc->efi_lbasize = disk_info.dki_lbsize;
459 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
460 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
461 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
462 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
463 
464 	/*
465 	 * If the array the user passed in is too small, set the length
466 	 * to what it needs to be and return
467 	 */
468 	if (user_length < vtoc->efi_nparts) {
469 		return (VT_EINVAL);
470 	}
471 
472 	for (i = 0; i < vtoc->efi_nparts; i++) {
473 
474 	    UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
475 		efi_parts[i].efi_gpe_PartitionTypeGUID);
476 
477 	    for (j = 0;
478 		j < sizeof (conversion_array) / sizeof (struct uuid_to_ptag);
479 		j++) {
480 
481 		    if (bcmp(&vtoc->efi_parts[i].p_guid,
482 			&conversion_array[j].uuid,
483 			sizeof (struct uuid)) == 0) {
484 			    vtoc->efi_parts[i].p_tag = j;
485 			    break;
486 		    }
487 	    }
488 	    if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
489 		    continue;
490 	    vtoc->efi_parts[i].p_flag =
491 		LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
492 	    vtoc->efi_parts[i].p_start =
493 		LE_64(efi_parts[i].efi_gpe_StartingLBA);
494 	    vtoc->efi_parts[i].p_size =
495 		LE_64(efi_parts[i].efi_gpe_EndingLBA) -
496 		    vtoc->efi_parts[i].p_start + 1;
497 	    for (j = 0; j < EFI_PART_NAME_LEN; j++) {
498 		vtoc->efi_parts[i].p_name[j] =
499 		    (uchar_t)LE_16(efi_parts[i].efi_gpe_PartitionName[j]);
500 	    }
501 
502 	    UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
503 		efi_parts[i].efi_gpe_UniquePartitionGUID);
504 	}
505 	free(efi);
506 
507 	return (dki_info.dki_partition);
508 }
509 
510 /* writes a "protective" MBR */
511 static int
512 write_pmbr(int fd, struct dk_gpt *vtoc)
513 {
514 	dk_efi_t	dk_ioc;
515 	struct mboot	mb;
516 	uchar_t		*cp;
517 	diskaddr_t	size_in_lba;
518 
519 	mb.signature = LE_16(MBB_MAGIC);
520 	bzero(&mb.parts, sizeof (mb.parts));
521 	cp = (uchar_t *)&mb.parts[0];
522 	/* bootable or not */
523 	*cp++ = 0;
524 	/* beginning CHS; 0xffffff if not representable */
525 	*cp++ = 0xff;
526 	*cp++ = 0xff;
527 	*cp++ = 0xff;
528 	/* OS type */
529 	*cp++ = EFI_PMBR;
530 	/* ending CHS; 0xffffff if not representable */
531 	*cp++ = 0xff;
532 	*cp++ = 0xff;
533 	*cp++ = 0xff;
534 	/* starting LBA: 1 (little endian format) by EFI definition */
535 	*cp++ = 0x01;
536 	*cp++ = 0x00;
537 	*cp++ = 0x00;
538 	*cp++ = 0x00;
539 	/* ending LBA: last block on the disk (little endian format) */
540 	size_in_lba = vtoc->efi_last_lba;
541 	if (size_in_lba < 0xffffffff) {
542 		*cp++ = (size_in_lba & 0x000000ff);
543 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
544 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
545 		*cp++ = (size_in_lba & 0xff000000) >> 24;
546 	} else {
547 		*cp++ = 0xff;
548 		*cp++ = 0xff;
549 		*cp++ = 0xff;
550 		*cp++ = 0xff;
551 	}
552 	/* LINTED -- always longlong aligned */
553 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
554 	dk_ioc.dki_lba = 0;
555 	dk_ioc.dki_length = sizeof (mb);
556 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
557 		switch (errno) {
558 		case EIO:
559 			return (VT_EIO);
560 		case EINVAL:
561 			return (VT_EINVAL);
562 		default:
563 			return (VT_ERROR);
564 		}
565 	}
566 	return (0);
567 }
568 
569 /* make sure the user specified something reasonable */
570 static int
571 check_input(struct dk_gpt *vtoc)
572 {
573 	int			resv_part = -1;
574 	int			i, j;
575 	diskaddr_t		istart, jstart, isize, jsize, endsect;
576 
577 	/*
578 	 * Sanity-check the input (make sure no partitions overlap)
579 	 */
580 	for (i = 0; i < vtoc->efi_nparts; i++) {
581 		/* It can't be unassigned and have an actual size */
582 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
583 		    (vtoc->efi_parts[i].p_size != 0)) {
584 			if (efi_debug) {
585 				(void) fprintf(stderr,
586 "partition %d is \"unassigned\" but has a size of %llu",
587 				    i,
588 				    vtoc->efi_parts[i].p_size);
589 			}
590 			return (VT_EINVAL);
591 		}
592 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
593 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
594 				continue;
595 			/* we have encountered an unknown uuid */
596 			vtoc->efi_parts[i].p_tag = 0xff;
597 		}
598 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
599 			if (resv_part != -1) {
600 				if (efi_debug) {
601 				    (void) fprintf(stderr,
602 "found duplicate reserved partition at %d\n",
603 					i);
604 				}
605 				return (VT_EINVAL);
606 			}
607 			resv_part = i;
608 		}
609 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
610 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
611 			if (efi_debug) {
612 				(void) fprintf(stderr,
613 				    "Partition %d starts at %llu.  ",
614 				    i,
615 				    vtoc->efi_parts[i].p_start);
616 				(void) fprintf(stderr,
617 				    "It must be between %llu and %llu.\n",
618 				    vtoc->efi_first_u_lba,
619 				    vtoc->efi_last_u_lba);
620 			}
621 			return (VT_EINVAL);
622 		}
623 		if ((vtoc->efi_parts[i].p_start +
624 		    vtoc->efi_parts[i].p_size <
625 		    vtoc->efi_first_u_lba) ||
626 		    (vtoc->efi_parts[i].p_start +
627 		    vtoc->efi_parts[i].p_size >
628 		    vtoc->efi_last_u_lba + 1)) {
629 			if (efi_debug) {
630 				(void) fprintf(stderr,
631 				    "Partition %d ends at %llu.  ",
632 				    i,
633 				    vtoc->efi_parts[i].p_start +
634 				    vtoc->efi_parts[i].p_size);
635 				(void) fprintf(stderr,
636 				    "It must be between %llu and %llu.\n",
637 				    vtoc->efi_first_u_lba,
638 				    vtoc->efi_last_u_lba);
639 			}
640 			return (VT_EINVAL);
641 		}
642 
643 		for (j = 0; j < vtoc->efi_nparts; j++) {
644 			isize = vtoc->efi_parts[i].p_size;
645 			jsize = vtoc->efi_parts[j].p_size;
646 			istart = vtoc->efi_parts[i].p_start;
647 			jstart = vtoc->efi_parts[j].p_start;
648 			if ((i != j) && (isize != 0) && (jsize != 0)) {
649 				endsect = jstart + jsize -1;
650 				if ((jstart <= istart) &&
651 				    (istart <= endsect)) {
652 					if (efi_debug) {
653 						(void) fprintf(stderr,
654 "Partition %d overlaps partition %d.",
655 						    i, j);
656 					    }
657 					    return (VT_EINVAL);
658 				}
659 			}
660 		}
661 	}
662 	/* just a warning for now */
663 	if ((resv_part == -1) && efi_debug) {
664 		(void) fprintf(stderr,
665 				"no reserved partition found\n");
666 	}
667 	return (0);
668 }
669 
670 /*
671  * write EFI label and backup label
672  */
673 int
674 efi_write(int fd, struct dk_gpt *vtoc)
675 {
676 	dk_efi_t		dk_ioc;
677 	efi_gpt_t		*efi;
678 	efi_gpe_t		*efi_parts;
679 	int			i, j;
680 	struct dk_cinfo		dki_info;
681 	int			md_flag = 0;
682 
683 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
684 		if (efi_debug)
685 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
686 		switch (errno) {
687 		case EIO:
688 			return (VT_EIO);
689 		case EINVAL:
690 			return (VT_EINVAL);
691 		default:
692 			return (VT_ERROR);
693 		}
694 	}
695 
696 	/* check if we are dealing wih a metadevice */
697 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
698 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
699 		md_flag = 1;
700 	}
701 
702 	if (check_input(vtoc)) {
703 		/*
704 		 * not valid; if it's a metadevice just pass it down
705 		 * because SVM will do its own checking
706 		 */
707 		if (md_flag == 0) {
708 			return (VT_EINVAL);
709 		}
710 	}
711 
712 	dk_ioc.dki_lba = 1;
713 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
714 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
715 	} else {
716 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
717 				    vtoc->efi_lbasize) *
718 				    vtoc->efi_lbasize;
719 	}
720 
721 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
722 		return (VT_ERROR);
723 
724 	efi = dk_ioc.dki_data;
725 
726 	/* stuff user's input into EFI struct */
727 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
728 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
729 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
730 	efi->efi_gpt_Reserved1 = 0;
731 	efi->efi_gpt_MyLBA = LE_64(1ULL);
732 	efi->efi_gpt_AlternateLBA = LE_64(vtoc->efi_last_lba);
733 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
734 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
735 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
736 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
737 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
738 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
739 
740 	/* LINTED -- always longlong aligned */
741 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + sizeof (efi_gpt_t));
742 
743 	for (i = 0; i < vtoc->efi_nparts; i++) {
744 	    for (j = 0;
745 		j < sizeof (conversion_array) / sizeof (struct uuid_to_ptag);
746 		j++) {
747 
748 		    if (vtoc->efi_parts[i].p_tag == j) {
749 			    UUID_LE_CONVERT(
750 				efi_parts[i].efi_gpe_PartitionTypeGUID,
751 				conversion_array[j].uuid);
752 			    break;
753 		    }
754 	    }
755 
756 	    if (j == sizeof (conversion_array) / sizeof (struct uuid_to_ptag)) {
757 		/*
758 		 * If we didn't have a matching uuid match, bail here.
759 		 * Don't write a label with unknown uuid.
760 		 */
761 		if (efi_debug)
762 		    (void) fprintf(stderr, "Unknown uuid for p_tag %d\n",
763 			vtoc->efi_parts[i].p_tag);
764 		return (VT_EINVAL);
765 	    }
766 
767 	    efi_parts[i].efi_gpe_StartingLBA =
768 		LE_64(vtoc->efi_parts[i].p_start);
769 	    efi_parts[i].efi_gpe_EndingLBA =
770 		LE_64(vtoc->efi_parts[i].p_start +
771 		vtoc->efi_parts[i].p_size - 1);
772 	    efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
773 		    LE_16(vtoc->efi_parts[i].p_flag);
774 	    for (j = 0; j < EFI_PART_NAME_LEN; j++) {
775 		    efi_parts[i].efi_gpe_PartitionName[j] =
776 			LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
777 	    }
778 	    if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
779 		uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
780 		    (void) uuid_generate((uchar_t *)
781 			&vtoc->efi_parts[i].p_uguid);
782 	    }
783 	    bcopy(&vtoc->efi_parts[i].p_uguid,
784 		&efi_parts[i].efi_gpe_UniquePartitionGUID,
785 		sizeof (uuid_t));
786 	}
787 	efi->efi_gpt_PartitionEntryArrayCRC32 =
788 	    LE_32(efi_crc32((unsigned char *)efi_parts,
789 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
790 	efi->efi_gpt_HeaderCRC32 =
791 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
792 
793 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
794 		free(dk_ioc.dki_data);
795 		switch (errno) {
796 		case EIO:
797 			return (VT_EIO);
798 		case EINVAL:
799 			return (VT_EINVAL);
800 		default:
801 			return (VT_ERROR);
802 		}
803 	}
804 	/* if it's a metadevice we're done */
805 	if (md_flag) {
806 		free(dk_ioc.dki_data);
807 		return (0);
808 	}
809 	/* write backup partition array */
810 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
811 	dk_ioc.dki_length -= vtoc->efi_lbasize;
812 	dk_ioc.dki_data++;
813 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
814 		/*
815 		 * we wrote the primary label okay, so don't fail
816 		 */
817 		if (efi_debug) {
818 			(void) fprintf(stderr,
819 			    "write of backup partitions to block %llu "
820 			    "failed, errno %d\n",
821 			    vtoc->efi_last_u_lba + 1,
822 			    errno);
823 		}
824 	}
825 	/*
826 	 * now swap MyLBA and AlternateLBA fields and write backup
827 	 * partition table header
828 	 */
829 	dk_ioc.dki_lba = vtoc->efi_last_lba;
830 	dk_ioc.dki_length = vtoc->efi_lbasize;
831 	dk_ioc.dki_data--;
832 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
833 	efi->efi_gpt_MyLBA = LE_64(vtoc->efi_last_lba);
834 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
835 	efi->efi_gpt_HeaderCRC32 = 0;
836 	efi->efi_gpt_HeaderCRC32 =
837 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
838 	    sizeof (struct efi_gpt)));
839 
840 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
841 		if (efi_debug) {
842 			(void) fprintf(stderr,
843 			    "write of backup header to block %llu failed, "
844 			    "errno %d\n",
845 			    vtoc->efi_last_lba,
846 			    errno);
847 		}
848 	}
849 	/* write the PMBR */
850 	(void) write_pmbr(fd, vtoc);
851 	free(dk_ioc.dki_data);
852 	return (0);
853 }
854 
855 void
856 efi_free(struct dk_gpt *ptr)
857 {
858 	free(ptr);
859 }
860 
861 /*
862  * Input: File descriptor
863  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
864  */
865 int
866 efi_type(int fd)
867 {
868 	struct vtoc vtoc;
869 
870 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
871 		if (errno == ENOTSUP) {
872 			return (1);
873 		}
874 	}
875 	return (0);
876 }
877 
878 void
879 efi_err_check(struct dk_gpt *vtoc)
880 {
881 	int			resv_part = -1;
882 	int			i, j;
883 	diskaddr_t		istart, jstart, isize, jsize, endsect;
884 	int			overlap = 0;
885 
886 	/*
887 	 * make sure no partitions overlap
888 	 */
889 	for (i = 0; i < vtoc->efi_nparts; i++) {
890 		/* It can't be unassigned and have an actual size */
891 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
892 		    (vtoc->efi_parts[i].p_size != 0)) {
893 			(void) fprintf(stderr,
894 			    "partition %d is \"unassigned\" but has a size "
895 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
896 		}
897 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
898 			continue;
899 		}
900 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
901 			if (resv_part != -1) {
902 				(void) fprintf(stderr,
903 				    "found duplicate reserved partition at "
904 				    "%d\n", i);
905 			}
906 			resv_part = i;
907 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
908 				(void) fprintf(stderr,
909 				    "Warning: reserved partition size must "
910 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
911 		}
912 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
913 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
914 			(void) fprintf(stderr,
915 			    "Partition %d starts at %llu\n",
916 			    i,
917 			    vtoc->efi_parts[i].p_start);
918 			(void) fprintf(stderr,
919 			    "It must be between %llu and %llu.\n",
920 			    vtoc->efi_first_u_lba,
921 			    vtoc->efi_last_u_lba);
922 		}
923 		if ((vtoc->efi_parts[i].p_start +
924 		    vtoc->efi_parts[i].p_size <
925 		    vtoc->efi_first_u_lba) ||
926 		    (vtoc->efi_parts[i].p_start +
927 		    vtoc->efi_parts[i].p_size >
928 		    vtoc->efi_last_u_lba + 1)) {
929 			(void) fprintf(stderr,
930 			    "Partition %d ends at %llu\n",
931 			    i,
932 			    vtoc->efi_parts[i].p_start +
933 			    vtoc->efi_parts[i].p_size);
934 			(void) fprintf(stderr,
935 			    "It must be between %llu and %llu.\n",
936 			    vtoc->efi_first_u_lba,
937 			    vtoc->efi_last_u_lba);
938 		}
939 
940 		for (j = 0; j < vtoc->efi_nparts; j++) {
941 			isize = vtoc->efi_parts[i].p_size;
942 			jsize = vtoc->efi_parts[j].p_size;
943 			istart = vtoc->efi_parts[i].p_start;
944 			jstart = vtoc->efi_parts[j].p_start;
945 			if ((i != j) && (isize != 0) && (jsize != 0)) {
946 				endsect = jstart + jsize -1;
947 				if ((jstart <= istart) &&
948 				    (istart <= endsect)) {
949 					if (!overlap) {
950 					(void) fprintf(stderr,
951 					    "label error: EFI Labels do not "
952 					    "support overlapping partitions\n");
953 					}
954 					(void) fprintf(stderr,
955 					    "Partition %d overlaps partition "
956 					    "%d.\n", i, j);
957 					overlap = 1;
958 				}
959 			}
960 		}
961 	}
962 	/* make sure there is a reserved partition */
963 	if (resv_part == -1) {
964 		(void) fprintf(stderr,
965 			"no reserved partition found\n");
966 	}
967 }
968 
969 /*
970  * We need to get information necessary to construct a *new* efi
971  * label type
972  */
973 int
974 efi_auto_sense(int fd, struct dk_gpt **vtoc)
975 {
976 
977 	int	i;
978 
979 	/*
980 	 * Now build the default partition table
981 	 */
982 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
983 		if (efi_debug) {
984 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
985 		}
986 		return (-1);
987 	}
988 
989 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
990 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
991 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
992 		(*vtoc)->efi_parts[i].p_start = 0;
993 		(*vtoc)->efi_parts[i].p_size = 0;
994 	}
995 	/*
996 	 * Make constants first
997 	 * and variable partitions later
998 	 */
999 
1000 	/* root partition - s0 128 MB */
1001 	(*vtoc)->efi_parts[0].p_start = 34;
1002 	(*vtoc)->efi_parts[0].p_size = 262144;
1003 
1004 	/* partition - s1  128 MB */
1005 	(*vtoc)->efi_parts[1].p_start = 262178;
1006 	(*vtoc)->efi_parts[1].p_size = 262144;
1007 
1008 	/* partition -s2 is NOT the Backup disk */
1009 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1010 
1011 	/* partition -s6 /usr partition - HOG */
1012 	(*vtoc)->efi_parts[6].p_start = 524322;
1013 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1014 	    - (1024 * 16);
1015 
1016 	/* efi reserved partition - s9 16K */
1017 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1018 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
1019 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
1020 	return (0);
1021 }
1022