xref: /titanic_52/usr/src/lib/libefi/common/rdwr_efi.c (revision 6c10f5d00ef774bc0c97c0ef38374f86a4ee2007)
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 	/*
544 	 * Preserve any boot code and disk signature if the first block is
545 	 * already an MBR.
546 	 */
547 	dk_ioc.dki_lba = 0;
548 	dk_ioc.dki_length = sizeof (mb);
549 	/* LINTED -- always longlong aligned */
550 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
551 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1 ||
552 	    mb.signature != LE_16(MBB_MAGIC)) {
553 		bzero(&mb, sizeof (mb));
554 		mb.signature = LE_16(MBB_MAGIC);
555 	}
556 	bzero(&mb.parts, sizeof (mb.parts));
557 	cp = (uchar_t *)&mb.parts[0];
558 	/* bootable or not */
559 	*cp++ = 0;
560 	/* beginning CHS; 0xffffff if not representable */
561 	*cp++ = 0xff;
562 	*cp++ = 0xff;
563 	*cp++ = 0xff;
564 	/* OS type */
565 	*cp++ = EFI_PMBR;
566 	/* ending CHS; 0xffffff if not representable */
567 	*cp++ = 0xff;
568 	*cp++ = 0xff;
569 	*cp++ = 0xff;
570 	/* starting LBA: 1 (little endian format) by EFI definition */
571 	*cp++ = 0x01;
572 	*cp++ = 0x00;
573 	*cp++ = 0x00;
574 	*cp++ = 0x00;
575 	/* ending LBA: last block on the disk (little endian format) */
576 	size_in_lba = vtoc->efi_last_lba;
577 	if (size_in_lba < 0xffffffff) {
578 		*cp++ = (size_in_lba & 0x000000ff);
579 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
580 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
581 		*cp++ = (size_in_lba & 0xff000000) >> 24;
582 	} else {
583 		*cp++ = 0xff;
584 		*cp++ = 0xff;
585 		*cp++ = 0xff;
586 		*cp++ = 0xff;
587 	}
588 	/* LINTED -- always longlong aligned */
589 	dk_ioc.dki_data = (efi_gpt_t *)&mb;
590 	dk_ioc.dki_lba = 0;
591 	dk_ioc.dki_length = sizeof (mb);
592 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
593 		switch (errno) {
594 		case EIO:
595 			return (VT_EIO);
596 		case EINVAL:
597 			return (VT_EINVAL);
598 		default:
599 			return (VT_ERROR);
600 		}
601 	}
602 	return (0);
603 }
604 
605 /* make sure the user specified something reasonable */
606 static int
607 check_input(struct dk_gpt *vtoc)
608 {
609 	int			resv_part = -1;
610 	int			i, j;
611 	diskaddr_t		istart, jstart, isize, jsize, endsect;
612 
613 	/*
614 	 * Sanity-check the input (make sure no partitions overlap)
615 	 */
616 	for (i = 0; i < vtoc->efi_nparts; i++) {
617 		/* It can't be unassigned and have an actual size */
618 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
619 		    (vtoc->efi_parts[i].p_size != 0)) {
620 			if (efi_debug) {
621 				(void) fprintf(stderr,
622 "partition %d is \"unassigned\" but has a size of %llu",
623 				    i,
624 				    vtoc->efi_parts[i].p_size);
625 			}
626 			return (VT_EINVAL);
627 		}
628 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
629 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
630 				continue;
631 			/* we have encountered an unknown uuid */
632 			vtoc->efi_parts[i].p_tag = 0xff;
633 		}
634 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
635 			if (resv_part != -1) {
636 				if (efi_debug) {
637 					(void) fprintf(stderr,
638 "found duplicate reserved partition at %d\n",
639 					    i);
640 				}
641 				return (VT_EINVAL);
642 			}
643 			resv_part = i;
644 		}
645 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
646 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
647 			if (efi_debug) {
648 				(void) fprintf(stderr,
649 				    "Partition %d starts at %llu.  ",
650 				    i,
651 				    vtoc->efi_parts[i].p_start);
652 				(void) fprintf(stderr,
653 				    "It must be between %llu and %llu.\n",
654 				    vtoc->efi_first_u_lba,
655 				    vtoc->efi_last_u_lba);
656 			}
657 			return (VT_EINVAL);
658 		}
659 		if ((vtoc->efi_parts[i].p_start +
660 		    vtoc->efi_parts[i].p_size <
661 		    vtoc->efi_first_u_lba) ||
662 		    (vtoc->efi_parts[i].p_start +
663 		    vtoc->efi_parts[i].p_size >
664 		    vtoc->efi_last_u_lba + 1)) {
665 			if (efi_debug) {
666 				(void) fprintf(stderr,
667 				    "Partition %d ends at %llu.  ",
668 				    i,
669 				    vtoc->efi_parts[i].p_start +
670 				    vtoc->efi_parts[i].p_size);
671 				(void) fprintf(stderr,
672 				    "It must be between %llu and %llu.\n",
673 				    vtoc->efi_first_u_lba,
674 				    vtoc->efi_last_u_lba);
675 			}
676 			return (VT_EINVAL);
677 		}
678 
679 		for (j = 0; j < vtoc->efi_nparts; j++) {
680 			isize = vtoc->efi_parts[i].p_size;
681 			jsize = vtoc->efi_parts[j].p_size;
682 			istart = vtoc->efi_parts[i].p_start;
683 			jstart = vtoc->efi_parts[j].p_start;
684 			if ((i != j) && (isize != 0) && (jsize != 0)) {
685 				endsect = jstart + jsize -1;
686 				if ((jstart <= istart) &&
687 				    (istart <= endsect)) {
688 					if (efi_debug) {
689 						(void) fprintf(stderr,
690 "Partition %d overlaps partition %d.",
691 						    i, j);
692 					}
693 					return (VT_EINVAL);
694 				}
695 			}
696 		}
697 	}
698 	/* just a warning for now */
699 	if ((resv_part == -1) && efi_debug) {
700 		(void) fprintf(stderr,
701 		    "no reserved partition found\n");
702 	}
703 	return (0);
704 }
705 
706 /*
707  * add all the unallocated space to the current label
708  */
709 int
710 efi_use_whole_disk(int fd)
711 {
712 	struct dk_gpt		*efi_label;
713 	int			rval;
714 	int			i;
715 	uint_t			phy_last_slice = 0;
716 	diskaddr_t		pl_start = 0;
717 	diskaddr_t		pl_size;
718 
719 	rval = efi_alloc_and_read(fd, &efi_label);
720 	if (rval < 0) {
721 		return (rval);
722 	}
723 
724 	/* find the last physically non-zero partition */
725 	for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
726 		if (pl_start < efi_label->efi_parts[i].p_start) {
727 			pl_start = efi_label->efi_parts[i].p_start;
728 			phy_last_slice = i;
729 		}
730 	}
731 	pl_size = efi_label->efi_parts[phy_last_slice].p_size;
732 
733 	/*
734 	 * If alter_lba is 1, we are using the backup label.
735 	 * Since we can locate the backup label by disk capacity,
736 	 * there must be no unallocated space.
737 	 */
738 	if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
739 	    >= efi_label->efi_last_lba)) {
740 		if (efi_debug) {
741 			(void) fprintf(stderr,
742 			    "efi_use_whole_disk: requested space not found\n");
743 		}
744 		efi_free(efi_label);
745 		return (VT_ENOSPC);
746 	}
747 
748 	/*
749 	 * If there is space between the last physically non-zero partition
750 	 * and the reserved partition, just add the unallocated space to this
751 	 * area. Otherwise, the unallocated space is added to the last
752 	 * physically non-zero partition.
753 	 */
754 	if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
755 	    EFI_MIN_RESV_SIZE) {
756 		efi_label->efi_parts[phy_last_slice].p_size +=
757 		    efi_label->efi_last_lba - efi_label->efi_altern_lba;
758 	}
759 
760 	/*
761 	 * Move the reserved partition. There is currently no data in
762 	 * here except fabricated devids (which get generated via
763 	 * efi_write()). So there is no need to copy data.
764 	 */
765 	efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
766 	    efi_label->efi_last_lba - efi_label->efi_altern_lba;
767 	efi_label->efi_last_u_lba += efi_label->efi_last_lba
768 	    - efi_label->efi_altern_lba;
769 
770 	rval = efi_write(fd, efi_label);
771 	if (rval < 0) {
772 		if (efi_debug) {
773 			(void) fprintf(stderr,
774 			    "efi_use_whole_disk:fail to write label, rval=%d\n",
775 			    rval);
776 		}
777 		efi_free(efi_label);
778 		return (rval);
779 	}
780 
781 	efi_free(efi_label);
782 	return (0);
783 }
784 
785 
786 /*
787  * write EFI label and backup label
788  */
789 int
790 efi_write(int fd, struct dk_gpt *vtoc)
791 {
792 	dk_efi_t		dk_ioc;
793 	efi_gpt_t		*efi;
794 	efi_gpe_t		*efi_parts;
795 	int			i, j;
796 	struct dk_cinfo		dki_info;
797 	int			md_flag = 0;
798 	int			nblocks;
799 	diskaddr_t		lba_backup_gpt_hdr;
800 
801 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
802 		if (efi_debug)
803 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
804 		switch (errno) {
805 		case EIO:
806 			return (VT_EIO);
807 		case EINVAL:
808 			return (VT_EINVAL);
809 		default:
810 			return (VT_ERROR);
811 		}
812 	}
813 
814 	/* check if we are dealing wih a metadevice */
815 	if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
816 	    (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
817 		md_flag = 1;
818 	}
819 
820 	if (check_input(vtoc)) {
821 		/*
822 		 * not valid; if it's a metadevice just pass it down
823 		 * because SVM will do its own checking
824 		 */
825 		if (md_flag == 0) {
826 			return (VT_EINVAL);
827 		}
828 	}
829 
830 	dk_ioc.dki_lba = 1;
831 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
832 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
833 	} else {
834 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
835 		    vtoc->efi_lbasize) *
836 		    vtoc->efi_lbasize;
837 	}
838 
839 	/*
840 	 * the number of blocks occupied by GUID partition entry array
841 	 */
842 	nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
843 
844 	/*
845 	 * Backup GPT header is located on the block after GUID
846 	 * partition entry array. Here, we calculate the address
847 	 * for backup GPT header.
848 	 */
849 	lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
850 
851 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
852 		return (VT_ERROR);
853 
854 	efi = dk_ioc.dki_data;
855 
856 	/* stuff user's input into EFI struct */
857 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
858 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
859 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
860 	efi->efi_gpt_Reserved1 = 0;
861 	efi->efi_gpt_MyLBA = LE_64(1ULL);
862 	efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
863 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
864 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
865 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
866 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
867 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
868 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
869 
870 	/* LINTED -- always longlong aligned */
871 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + sizeof (efi_gpt_t));
872 
873 	for (i = 0; i < vtoc->efi_nparts; i++) {
874 		for (j = 0;
875 		    j < sizeof (conversion_array) /
876 		    sizeof (struct uuid_to_ptag); j++) {
877 
878 			if (vtoc->efi_parts[i].p_tag == j) {
879 				UUID_LE_CONVERT(
880 				    efi_parts[i].efi_gpe_PartitionTypeGUID,
881 				    conversion_array[j].uuid);
882 				break;
883 			}
884 		}
885 
886 		if (j == sizeof (conversion_array) /
887 		    sizeof (struct uuid_to_ptag)) {
888 			/*
889 			 * If we didn't have a matching uuid match, bail here.
890 			 * Don't write a label with unknown uuid.
891 			 */
892 			if (efi_debug) {
893 				(void) fprintf(stderr,
894 				    "Unknown uuid for p_tag %d\n",
895 				    vtoc->efi_parts[i].p_tag);
896 			}
897 			return (VT_EINVAL);
898 		}
899 
900 		efi_parts[i].efi_gpe_StartingLBA =
901 		    LE_64(vtoc->efi_parts[i].p_start);
902 		efi_parts[i].efi_gpe_EndingLBA =
903 		    LE_64(vtoc->efi_parts[i].p_start +
904 		    vtoc->efi_parts[i].p_size - 1);
905 		efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
906 		    LE_16(vtoc->efi_parts[i].p_flag);
907 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
908 			efi_parts[i].efi_gpe_PartitionName[j] =
909 			    LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
910 		}
911 		if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
912 		    uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
913 			(void) uuid_generate((uchar_t *)
914 			    &vtoc->efi_parts[i].p_uguid);
915 		}
916 		bcopy(&vtoc->efi_parts[i].p_uguid,
917 		    &efi_parts[i].efi_gpe_UniquePartitionGUID,
918 		    sizeof (uuid_t));
919 	}
920 	efi->efi_gpt_PartitionEntryArrayCRC32 =
921 	    LE_32(efi_crc32((unsigned char *)efi_parts,
922 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
923 	efi->efi_gpt_HeaderCRC32 =
924 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
925 
926 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
927 		free(dk_ioc.dki_data);
928 		switch (errno) {
929 		case EIO:
930 			return (VT_EIO);
931 		case EINVAL:
932 			return (VT_EINVAL);
933 		default:
934 			return (VT_ERROR);
935 		}
936 	}
937 	/* if it's a metadevice we're done */
938 	if (md_flag) {
939 		free(dk_ioc.dki_data);
940 		return (0);
941 	}
942 	/* write backup partition array */
943 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
944 	dk_ioc.dki_length -= vtoc->efi_lbasize;
945 	dk_ioc.dki_data++;
946 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
947 		/*
948 		 * we wrote the primary label okay, so don't fail
949 		 */
950 		if (efi_debug) {
951 			(void) fprintf(stderr,
952 			    "write of backup partitions to block %llu "
953 			    "failed, errno %d\n",
954 			    vtoc->efi_last_u_lba + 1,
955 			    errno);
956 		}
957 	}
958 	/*
959 	 * now swap MyLBA and AlternateLBA fields and write backup
960 	 * partition table header
961 	 */
962 	dk_ioc.dki_lba = lba_backup_gpt_hdr;
963 	dk_ioc.dki_length = vtoc->efi_lbasize;
964 	dk_ioc.dki_data--;
965 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
966 	efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
967 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
968 	efi->efi_gpt_HeaderCRC32 = 0;
969 	efi->efi_gpt_HeaderCRC32 =
970 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
971 	    sizeof (struct efi_gpt)));
972 
973 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
974 		if (efi_debug) {
975 			(void) fprintf(stderr,
976 			    "write of backup header to block %llu failed, "
977 			    "errno %d\n",
978 			    lba_backup_gpt_hdr,
979 			    errno);
980 		}
981 	}
982 	/* write the PMBR */
983 	(void) write_pmbr(fd, vtoc);
984 	free(dk_ioc.dki_data);
985 	return (0);
986 }
987 
988 void
989 efi_free(struct dk_gpt *ptr)
990 {
991 	free(ptr);
992 }
993 
994 /*
995  * Input: File descriptor
996  * Output: 1 if disk is >1TB OR has an EFI label, 0 otherwise.
997  */
998 int
999 efi_type(int fd)
1000 {
1001 	struct vtoc vtoc;
1002 
1003 	if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) {
1004 		if (errno == ENOTSUP) {
1005 			return (1);
1006 		}
1007 	}
1008 	return (0);
1009 }
1010 
1011 void
1012 efi_err_check(struct dk_gpt *vtoc)
1013 {
1014 	int			resv_part = -1;
1015 	int			i, j;
1016 	diskaddr_t		istart, jstart, isize, jsize, endsect;
1017 	int			overlap = 0;
1018 
1019 	/*
1020 	 * make sure no partitions overlap
1021 	 */
1022 	for (i = 0; i < vtoc->efi_nparts; i++) {
1023 		/* It can't be unassigned and have an actual size */
1024 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1025 		    (vtoc->efi_parts[i].p_size != 0)) {
1026 			(void) fprintf(stderr,
1027 			    "partition %d is \"unassigned\" but has a size "
1028 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
1029 		}
1030 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1031 			continue;
1032 		}
1033 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1034 			if (resv_part != -1) {
1035 				(void) fprintf(stderr,
1036 				    "found duplicate reserved partition at "
1037 				    "%d\n", i);
1038 			}
1039 			resv_part = i;
1040 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1041 				(void) fprintf(stderr,
1042 				    "Warning: reserved partition size must "
1043 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
1044 		}
1045 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1046 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1047 			(void) fprintf(stderr,
1048 			    "Partition %d starts at %llu\n",
1049 			    i,
1050 			    vtoc->efi_parts[i].p_start);
1051 			(void) fprintf(stderr,
1052 			    "It must be between %llu and %llu.\n",
1053 			    vtoc->efi_first_u_lba,
1054 			    vtoc->efi_last_u_lba);
1055 		}
1056 		if ((vtoc->efi_parts[i].p_start +
1057 		    vtoc->efi_parts[i].p_size <
1058 		    vtoc->efi_first_u_lba) ||
1059 		    (vtoc->efi_parts[i].p_start +
1060 		    vtoc->efi_parts[i].p_size >
1061 		    vtoc->efi_last_u_lba + 1)) {
1062 			(void) fprintf(stderr,
1063 			    "Partition %d ends at %llu\n",
1064 			    i,
1065 			    vtoc->efi_parts[i].p_start +
1066 			    vtoc->efi_parts[i].p_size);
1067 			(void) fprintf(stderr,
1068 			    "It must be between %llu and %llu.\n",
1069 			    vtoc->efi_first_u_lba,
1070 			    vtoc->efi_last_u_lba);
1071 		}
1072 
1073 		for (j = 0; j < vtoc->efi_nparts; j++) {
1074 			isize = vtoc->efi_parts[i].p_size;
1075 			jsize = vtoc->efi_parts[j].p_size;
1076 			istart = vtoc->efi_parts[i].p_start;
1077 			jstart = vtoc->efi_parts[j].p_start;
1078 			if ((i != j) && (isize != 0) && (jsize != 0)) {
1079 				endsect = jstart + jsize -1;
1080 				if ((jstart <= istart) &&
1081 				    (istart <= endsect)) {
1082 					if (!overlap) {
1083 					(void) fprintf(stderr,
1084 					    "label error: EFI Labels do not "
1085 					    "support overlapping partitions\n");
1086 					}
1087 					(void) fprintf(stderr,
1088 					    "Partition %d overlaps partition "
1089 					    "%d.\n", i, j);
1090 					overlap = 1;
1091 				}
1092 			}
1093 		}
1094 	}
1095 	/* make sure there is a reserved partition */
1096 	if (resv_part == -1) {
1097 		(void) fprintf(stderr,
1098 		    "no reserved partition found\n");
1099 	}
1100 }
1101 
1102 /*
1103  * We need to get information necessary to construct a *new* efi
1104  * label type
1105  */
1106 int
1107 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1108 {
1109 
1110 	int	i;
1111 
1112 	/*
1113 	 * Now build the default partition table
1114 	 */
1115 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1116 		if (efi_debug) {
1117 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1118 		}
1119 		return (-1);
1120 	}
1121 
1122 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1123 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1124 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1125 		(*vtoc)->efi_parts[i].p_start = 0;
1126 		(*vtoc)->efi_parts[i].p_size = 0;
1127 	}
1128 	/*
1129 	 * Make constants first
1130 	 * and variable partitions later
1131 	 */
1132 
1133 	/* root partition - s0 128 MB */
1134 	(*vtoc)->efi_parts[0].p_start = 34;
1135 	(*vtoc)->efi_parts[0].p_size = 262144;
1136 
1137 	/* partition - s1  128 MB */
1138 	(*vtoc)->efi_parts[1].p_start = 262178;
1139 	(*vtoc)->efi_parts[1].p_size = 262144;
1140 
1141 	/* partition -s2 is NOT the Backup disk */
1142 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1143 
1144 	/* partition -s6 /usr partition - HOG */
1145 	(*vtoc)->efi_parts[6].p_start = 524322;
1146 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1147 	    - (1024 * 16);
1148 
1149 	/* efi reserved partition - s9 16K */
1150 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1151 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
1152 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
1153 	return (0);
1154 }
1155