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