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