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 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright 2014 Toomas Soome <tsoome@me.com>
26  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <smbios.h>
35 #include <uuid/uuid.h>
36 #include <libintl.h>
37 #include <sys/types.h>
38 #include <sys/dkio.h>
39 #include <sys/vtoc.h>
40 #include <sys/mhd.h>
41 #include <sys/param.h>
42 #include <sys/dktp/fdisk.h>
43 #include <sys/efi_partition.h>
44 #include <sys/byteorder.h>
45 #include <sys/ddi.h>
46 
47 static struct uuid_to_ptag {
48 	struct uuid	uuid;
49 } conversion_array[] = {
50 	{ EFI_UNUSED },
51 	{ EFI_BOOT },
52 	{ EFI_ROOT },
53 	{ EFI_SWAP },
54 	{ EFI_USR },
55 	{ EFI_BACKUP },
56 	{ 0 },			/* STAND is never used */
57 	{ EFI_VAR },
58 	{ EFI_HOME },
59 	{ EFI_ALTSCTR },
60 	{ 0 },			/* CACHE is never used */
61 	{ EFI_RESERVED },
62 	{ EFI_SYSTEM },
63 	{ EFI_LEGACY_MBR },
64 	{ EFI_SYMC_PUB },
65 	{ EFI_SYMC_CDS },
66 	{ EFI_MSFT_RESV },
67 	{ EFI_DELL_BASIC },
68 	{ EFI_DELL_RAID },
69 	{ EFI_DELL_SWAP },
70 	{ EFI_DELL_LVM },
71 	{ EFI_DELL_RESV },
72 	{ EFI_AAPL_HFS },
73 	{ EFI_AAPL_UFS },
74 	{ EFI_BIOS_BOOT },
75 	{ EFI_FREEBSD_BOOT },
76 	{ EFI_FREEBSD_SWAP },
77 	{ EFI_FREEBSD_UFS },
78 	{ EFI_FREEBSD_VINUM },
79 	{ EFI_FREEBSD_ZFS }
80 };
81 
82 /*
83  * Default vtoc information for non-SVr4 partitions
84  */
85 struct dk_map2  default_vtoc_map[NDKMAP] = {
86 	{	V_ROOT,		0	},		/* a - 0 */
87 	{	V_SWAP,		V_UNMNT	},		/* b - 1 */
88 	{	V_BACKUP,	V_UNMNT	},		/* c - 2 */
89 	{	V_UNASSIGNED,	0	},		/* d - 3 */
90 	{	V_UNASSIGNED,	0	},		/* e - 4 */
91 	{	V_UNASSIGNED,	0	},		/* f - 5 */
92 	{	V_USR,		0	},		/* g - 6 */
93 	{	V_UNASSIGNED,	0	},		/* h - 7 */
94 
95 #if defined(_SUNOS_VTOC_16)
96 
97 #if defined(i386) || defined(__amd64)
98 	{	V_BOOT,		V_UNMNT	},		/* i - 8 */
99 	{	V_ALTSCTR,	0	},		/* j - 9 */
100 
101 #else
102 #error No VTOC format defined.
103 #endif			/* defined(i386) */
104 
105 	{	V_UNASSIGNED,	0	},		/* k - 10 */
106 	{	V_UNASSIGNED,	0	},		/* l - 11 */
107 	{	V_UNASSIGNED,	0	},		/* m - 12 */
108 	{	V_UNASSIGNED,	0	},		/* n - 13 */
109 	{	V_UNASSIGNED,	0	},		/* o - 14 */
110 	{	V_UNASSIGNED,	0	},		/* p - 15 */
111 #endif			/* defined(_SUNOS_VTOC_16) */
112 };
113 
114 #ifdef DEBUG
115 int efi_debug = 1;
116 #else
117 int efi_debug = 0;
118 #endif
119 
120 #define	EFI_FIXES_DB "/usr/share/hwdata/efi.fixes"
121 
122 extern unsigned int	efi_crc32(const unsigned char *, unsigned int);
123 static int		efi_read(int, struct dk_gpt *);
124 
125 static int
126 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
127 {
128 	struct dk_minfo		disk_info;
129 
130 	if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1)
131 		return (errno);
132 	*capacity = disk_info.dki_capacity;
133 	*lbsize = disk_info.dki_lbsize;
134 	return (0);
135 }
136 
137 /*
138  * the number of blocks the EFI label takes up (round up to nearest
139  * block)
140  */
141 #define	NBLOCKS(p, l)	(1 + ((((p) * (int)sizeof (efi_gpe_t))  + \
142 				((l) - 1)) / (l)))
143 /* number of partitions -- limited by what we can malloc */
144 #define	MAX_PARTS	((4294967295UL - sizeof (struct dk_gpt)) / \
145 			    sizeof (struct dk_part))
146 
147 int
148 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
149 {
150 	diskaddr_t	capacity;
151 	uint_t		lbsize;
152 	uint_t		nblocks;
153 	size_t		length;
154 	struct dk_gpt	*vptr;
155 	struct uuid	uuid;
156 
157 	if (read_disk_info(fd, &capacity, &lbsize) != 0) {
158 		if (efi_debug)
159 			(void) fprintf(stderr,
160 			    "couldn't read disk information\n");
161 		return (-1);
162 	}
163 
164 	nblocks = NBLOCKS(nparts, lbsize);
165 	if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
166 		/* 16K plus one block for the GPT */
167 		nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
168 	}
169 
170 	if (nparts > MAX_PARTS) {
171 		if (efi_debug) {
172 			(void) fprintf(stderr,
173 			"the maximum number of partitions supported is %lu\n",
174 			    MAX_PARTS);
175 		}
176 		return (-1);
177 	}
178 
179 	length = sizeof (struct dk_gpt) +
180 	    sizeof (struct dk_part) * (nparts - 1);
181 
182 	if ((*vtoc = calloc(length, 1)) == NULL)
183 		return (-1);
184 
185 	vptr = *vtoc;
186 
187 	vptr->efi_version = EFI_VERSION_CURRENT;
188 	vptr->efi_lbasize = lbsize;
189 	vptr->efi_nparts = nparts;
190 	/*
191 	 * add one block here for the PMBR; on disks with a 512 byte
192 	 * block size and 128 or fewer partitions, efi_first_u_lba
193 	 * should work out to "34"
194 	 */
195 	vptr->efi_first_u_lba = nblocks + 1;
196 	vptr->efi_last_lba = capacity - 1;
197 	vptr->efi_altern_lba = capacity -1;
198 	vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
199 
200 	(void) uuid_generate((uchar_t *)&uuid);
201 	UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
202 	return (0);
203 }
204 
205 /*
206  * Read EFI - return partition number upon success.
207  */
208 int
209 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
210 {
211 	int			rval;
212 	uint32_t		nparts;
213 	int			length;
214 
215 	/* figure out the number of entries that would fit into 16K */
216 	nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
217 	length = (int) sizeof (struct dk_gpt) +
218 	    (int) sizeof (struct dk_part) * (nparts - 1);
219 	if ((*vtoc = calloc(length, 1)) == NULL)
220 		return (VT_ERROR);
221 
222 	(*vtoc)->efi_nparts = nparts;
223 	rval = efi_read(fd, *vtoc);
224 
225 	if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) {
226 		void *tmp;
227 		length = (int) sizeof (struct dk_gpt) +
228 		    (int) sizeof (struct dk_part) *
229 		    ((*vtoc)->efi_nparts - 1);
230 		nparts = (*vtoc)->efi_nparts;
231 		if ((tmp = realloc(*vtoc, length)) == NULL) {
232 			free (*vtoc);
233 			*vtoc = NULL;
234 			return (VT_ERROR);
235 		} else {
236 			*vtoc = tmp;
237 			rval = efi_read(fd, *vtoc);
238 		}
239 	}
240 
241 	if (rval < 0) {
242 		if (efi_debug) {
243 			(void) fprintf(stderr,
244 			    "read of EFI table failed, rval=%d\n", rval);
245 		}
246 		free (*vtoc);
247 		*vtoc = NULL;
248 	}
249 
250 	return (rval);
251 }
252 
253 static int
254 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
255 {
256 	void *data = dk_ioc->dki_data;
257 	int error;
258 
259 	dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data;
260 	error = ioctl(fd, cmd, (void *)dk_ioc);
261 	dk_ioc->dki_data = data;
262 
263 	return (error);
264 }
265 
266 static int
267 check_label(int fd, dk_efi_t *dk_ioc)
268 {
269 	efi_gpt_t		*efi;
270 	uint_t			crc;
271 
272 	if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
273 		switch (errno) {
274 		case EIO:
275 			return (VT_EIO);
276 		default:
277 			return (VT_ERROR);
278 		}
279 	}
280 	efi = dk_ioc->dki_data;
281 	if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
282 		if (efi_debug)
283 			(void) fprintf(stderr,
284 			    "Bad EFI signature: 0x%llx != 0x%llx\n",
285 			    (long long)efi->efi_gpt_Signature,
286 			    (long long)LE_64(EFI_SIGNATURE));
287 		return (VT_EINVAL);
288 	}
289 
290 	/*
291 	 * check CRC of the header; the size of the header should
292 	 * never be larger than one block
293 	 */
294 	crc = efi->efi_gpt_HeaderCRC32;
295 	efi->efi_gpt_HeaderCRC32 = 0;
296 
297 	if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) ||
298 	    crc != LE_32(efi_crc32((unsigned char *)efi,
299 	    LE_32(efi->efi_gpt_HeaderSize)))) {
300 		if (efi_debug)
301 			(void) fprintf(stderr,
302 			    "Bad EFI CRC: 0x%x != 0x%x\n",
303 			    crc,
304 			    LE_32(efi_crc32((unsigned char *)efi,
305 			    sizeof (struct efi_gpt))));
306 		return (VT_EINVAL);
307 	}
308 
309 	return (0);
310 }
311 
312 static int
313 efi_read(int fd, struct dk_gpt *vtoc)
314 {
315 	int			i, j;
316 	int			label_len;
317 	int			rval = 0;
318 	int			vdc_flag = 0;
319 	struct dk_minfo		disk_info;
320 	dk_efi_t		dk_ioc;
321 	efi_gpt_t		*efi;
322 	efi_gpe_t		*efi_parts;
323 	struct dk_cinfo		dki_info;
324 	uint32_t		user_length;
325 	boolean_t		legacy_label = B_FALSE;
326 
327 	/*
328 	 * get the partition number for this file descriptor.
329 	 */
330 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
331 		if (efi_debug) {
332 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
333 		}
334 		switch (errno) {
335 		case EIO:
336 			return (VT_EIO);
337 		case EINVAL:
338 			return (VT_EINVAL);
339 		default:
340 			return (VT_ERROR);
341 		}
342 	}
343 
344 	if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
345 	    (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
346 		/*
347 		 * The controller and drive name "vdc" (virtual disk client)
348 		 * indicates a LDoms virtual disk.
349 		 */
350 		vdc_flag++;
351 	}
352 
353 	/* get the LBA size */
354 	if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) {
355 		if (efi_debug) {
356 			(void) fprintf(stderr,
357 			    "assuming LBA 512 bytes %d\n",
358 			    errno);
359 		}
360 		disk_info.dki_lbsize = DEV_BSIZE;
361 	}
362 	if (disk_info.dki_lbsize == 0) {
363 		if (efi_debug) {
364 			(void) fprintf(stderr,
365 			    "efi_read: assuming LBA 512 bytes\n");
366 		}
367 		disk_info.dki_lbsize = DEV_BSIZE;
368 	}
369 	/*
370 	 * Read the EFI GPT to figure out how many partitions we need
371 	 * to deal with.
372 	 */
373 	dk_ioc.dki_lba = 1;
374 	if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
375 		label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
376 	} else {
377 		label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
378 		    disk_info.dki_lbsize;
379 		if (label_len % disk_info.dki_lbsize) {
380 			/* pad to physical sector size */
381 			label_len += disk_info.dki_lbsize;
382 			label_len &= ~(disk_info.dki_lbsize - 1);
383 		}
384 	}
385 
386 	if ((dk_ioc.dki_data = calloc(label_len, 1)) == NULL)
387 		return (VT_ERROR);
388 
389 	dk_ioc.dki_length = disk_info.dki_lbsize;
390 	user_length = vtoc->efi_nparts;
391 	efi = dk_ioc.dki_data;
392 	if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
393 		/*
394 		 * No valid label here; try the alternate. Note that here
395 		 * we just read GPT header and save it into dk_ioc.data,
396 		 * Later, we will read GUID partition entry array if we
397 		 * can get valid GPT header.
398 		 */
399 
400 		/*
401 		 * This is a workaround for legacy systems. In the past, the
402 		 * last sector of SCSI disk was invisible on x86 platform. At
403 		 * that time, backup label was saved on the next to the last
404 		 * sector. It is possible for users to move a disk from previous
405 		 * solaris system to present system. Here, we attempt to search
406 		 * legacy backup EFI label first.
407 		 */
408 		dk_ioc.dki_lba = disk_info.dki_capacity - 2;
409 		dk_ioc.dki_length = disk_info.dki_lbsize;
410 		rval = check_label(fd, &dk_ioc);
411 		if (rval == VT_EINVAL) {
412 			/*
413 			 * we didn't find legacy backup EFI label, try to
414 			 * search backup EFI label in the last block.
415 			 */
416 			dk_ioc.dki_lba = disk_info.dki_capacity - 1;
417 			dk_ioc.dki_length = disk_info.dki_lbsize;
418 			rval = check_label(fd, &dk_ioc);
419 			if (rval == 0) {
420 				legacy_label = B_TRUE;
421 				if (efi_debug)
422 					(void) fprintf(stderr,
423 					    "efi_read: primary label corrupt; "
424 					    "using EFI backup label located on"
425 					    " the last block\n");
426 			}
427 		} else {
428 			if ((efi_debug) && (rval == 0))
429 				(void) fprintf(stderr, "efi_read: primary label"
430 				    " corrupt; using legacy EFI backup label "
431 				    " located on the next to last block\n");
432 		}
433 
434 		if (rval == 0) {
435 			dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
436 			vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
437 			vtoc->efi_nparts =
438 			    LE_32(efi->efi_gpt_NumberOfPartitionEntries);
439 			/*
440 			 * Partition tables are between backup GPT header
441 			 * table and ParitionEntryLBA (the starting LBA of
442 			 * the GUID partition entries array). Now that we
443 			 * already got valid GPT header and saved it in
444 			 * dk_ioc.dki_data, we try to get GUID partition
445 			 * entry array here.
446 			 */
447 			/* LINTED */
448 			dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
449 			    + disk_info.dki_lbsize);
450 			if (legacy_label)
451 				dk_ioc.dki_length = disk_info.dki_capacity - 1 -
452 				    dk_ioc.dki_lba;
453 			else
454 				dk_ioc.dki_length = disk_info.dki_capacity - 2 -
455 				    dk_ioc.dki_lba;
456 			dk_ioc.dki_length *= disk_info.dki_lbsize;
457 			if (dk_ioc.dki_length >
458 			    ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
459 				rval = VT_EINVAL;
460 			} else {
461 				/*
462 				 * read GUID partition entry array
463 				 */
464 				rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
465 			}
466 		}
467 
468 	} else if (rval == 0) {
469 
470 		dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
471 		/* LINTED */
472 		dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
473 		    + disk_info.dki_lbsize);
474 		dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
475 		rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
476 
477 	} else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
478 		/*
479 		 * When the device is a LDoms virtual disk, the DKIOCGETEFI
480 		 * ioctl can fail with EINVAL if the virtual disk backend
481 		 * is a ZFS volume serviced by a domain running an old version
482 		 * of Solaris. This is because the DKIOCGETEFI ioctl was
483 		 * initially incorrectly implemented for a ZFS volume and it
484 		 * expected the GPT and GPE to be retrieved with a single ioctl.
485 		 * So we try to read the GPT and the GPE using that old style
486 		 * ioctl.
487 		 */
488 		dk_ioc.dki_lba = 1;
489 		dk_ioc.dki_length = label_len;
490 		rval = check_label(fd, &dk_ioc);
491 	}
492 
493 	if (rval < 0) {
494 		free(efi);
495 		return (rval);
496 	}
497 
498 	/* LINTED -- always longlong aligned */
499 	efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
500 
501 	/*
502 	 * Assemble this into a "dk_gpt" struct for easier
503 	 * digestibility by applications.
504 	 */
505 	vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
506 	vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
507 	vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
508 	vtoc->efi_lbasize = disk_info.dki_lbsize;
509 	vtoc->efi_last_lba = disk_info.dki_capacity - 1;
510 	vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
511 	vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
512 	vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
513 	UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
514 
515 	/*
516 	 * If the array the user passed in is too small, set the length
517 	 * to what it needs to be and return
518 	 */
519 	if (user_length < vtoc->efi_nparts) {
520 		return (VT_EINVAL);
521 	}
522 
523 	for (i = 0; i < vtoc->efi_nparts; i++) {
524 
525 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
526 		    efi_parts[i].efi_gpe_PartitionTypeGUID);
527 
528 		for (j = 0;
529 		    j < sizeof (conversion_array)
530 		    / sizeof (struct uuid_to_ptag); j++) {
531 
532 			if (bcmp(&vtoc->efi_parts[i].p_guid,
533 			    &conversion_array[j].uuid,
534 			    sizeof (struct uuid)) == 0) {
535 				vtoc->efi_parts[i].p_tag = j;
536 				break;
537 			}
538 		}
539 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
540 			continue;
541 		vtoc->efi_parts[i].p_flag =
542 		    LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
543 		vtoc->efi_parts[i].p_start =
544 		    LE_64(efi_parts[i].efi_gpe_StartingLBA);
545 		vtoc->efi_parts[i].p_size =
546 		    LE_64(efi_parts[i].efi_gpe_EndingLBA) -
547 		    vtoc->efi_parts[i].p_start + 1;
548 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
549 			vtoc->efi_parts[i].p_name[j] =
550 			    (uchar_t)LE_16(
551 			    efi_parts[i].efi_gpe_PartitionName[j]);
552 		}
553 
554 		UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
555 		    efi_parts[i].efi_gpe_UniquePartitionGUID);
556 	}
557 	free(efi);
558 
559 	return (dki_info.dki_partition);
560 }
561 
562 static void
563 hardware_workarounds(int *slot, int *active)
564 {
565 	smbios_struct_t s_sys, s_mb;
566 	smbios_info_t sys, mb;
567 	smbios_hdl_t *shp;
568 	char buf[0x400];
569 	FILE *fp;
570 	int err;
571 
572 	if ((fp = fopen(EFI_FIXES_DB, "rF")) == NULL)
573 		return;
574 
575 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL) {
576 		if (efi_debug)
577 			(void) fprintf(stderr,
578 			    "libefi failed to load SMBIOS: %s\n",
579 			    smbios_errmsg(err));
580 		(void) fclose(fp);
581 		return;
582 	}
583 
584 	if (smbios_lookup_type(shp, SMB_TYPE_SYSTEM, &s_sys) == SMB_ERR ||
585 	    smbios_info_common(shp, s_sys.smbstr_id, &sys) == SMB_ERR)
586 		(void) memset(&sys, '\0', sizeof (sys));
587 	if (smbios_lookup_type(shp, SMB_TYPE_BASEBOARD, &s_mb) == SMB_ERR ||
588 	    smbios_info_common(shp, s_mb.smbstr_id, &mb) == SMB_ERR)
589 		(void) memset(&mb, '\0', sizeof (mb));
590 
591 	while (fgets(buf, sizeof (buf), fp) != NULL) {
592 		char *tok, *val, *end;
593 
594 		tok = buf + strspn(buf, " \t");
595 		if (*tok == '#')
596 			continue;
597 		while (*tok != '\0') {
598 			tok += strspn(tok, " \t");
599 			if ((val = strchr(tok, '=')) == NULL)
600 				break;
601 			*val++ = '\0';
602 			if (*val == '"')
603 				end = strchr(++val, '"');
604 			else
605 				end = strpbrk(val, " \t\n");
606 			if (end == NULL)
607 				break;
608 			*end++ = '\0';
609 
610 			if (strcmp(tok, "sys.manufacturer") == 0 &&
611 			    (sys.smbi_manufacturer == NULL ||
612 			    strcasecmp(val, sys.smbi_manufacturer)))
613 				break;
614 			if (strcmp(tok, "sys.product") == 0 &&
615 			    (sys.smbi_product == NULL ||
616 			    strcasecmp(val, sys.smbi_product)))
617 				break;
618 			if (strcmp(tok, "sys.version") == 0 &&
619 			    (sys.smbi_version == NULL ||
620 			    strcasecmp(val, sys.smbi_version)))
621 				break;
622 			if (strcmp(tok, "mb.manufacturer") == 0 &&
623 			    (mb.smbi_manufacturer == NULL ||
624 			    strcasecmp(val, mb.smbi_manufacturer)))
625 				break;
626 			if (strcmp(tok, "mb.product") == 0 &&
627 			    (mb.smbi_product == NULL ||
628 			    strcasecmp(val, mb.smbi_product)))
629 				break;
630 			if (strcmp(tok, "mb.version") == 0 &&
631 			    (mb.smbi_version == NULL ||
632 			    strcasecmp(val, mb.smbi_version)))
633 				break;
634 
635 			if (strcmp(tok, "pmbr_slot") == 0) {
636 				*slot = atoi(val);
637 				if (*slot < 0 || *slot > 3)
638 					*slot = 0;
639 				if (efi_debug)
640 					(void) fprintf(stderr,
641 					    "Using slot %d\n", *slot);
642 			}
643 
644 			if (strcmp(tok, "pmbr_active") == 0) {
645 				*active = atoi(val);
646 				if (*active < 0 || *active > 1)
647 					*active = 0;
648 				if (efi_debug)
649 					(void) fprintf(stderr,
650 					    "Using active %d\n", *active);
651 			}
652 
653 			tok = end;
654 		}
655 	}
656 	(void) fclose(fp);
657 	smbios_close(shp);
658 }
659 
660 /* writes a "protective" MBR */
661 static int
662 write_pmbr(int fd, struct dk_gpt *vtoc)
663 {
664 	dk_efi_t	dk_ioc;
665 	struct mboot	mb;
666 	uchar_t		*cp;
667 	diskaddr_t	size_in_lba;
668 	uchar_t		*buf;
669 	int		len, slot, active;
670 
671 	slot = active = 0;
672 
673 	hardware_workarounds(&slot, &active);
674 
675 	len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
676 	buf = calloc(len, 1);
677 
678 	/*
679 	 * Preserve any boot code and disk signature if the first block is
680 	 * already an MBR.
681 	 */
682 	dk_ioc.dki_lba = 0;
683 	dk_ioc.dki_length = len;
684 	/* LINTED -- always longlong aligned */
685 	dk_ioc.dki_data = (efi_gpt_t *)buf;
686 	if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
687 		(void) memcpy(&mb, buf, sizeof (mb));
688 		bzero(&mb, sizeof (mb));
689 		mb.signature = LE_16(MBB_MAGIC);
690 	} else {
691 		(void) memcpy(&mb, buf, sizeof (mb));
692 		if (mb.signature != LE_16(MBB_MAGIC)) {
693 			bzero(&mb, sizeof (mb));
694 			mb.signature = LE_16(MBB_MAGIC);
695 		}
696 	}
697 
698 	bzero(&mb.parts, sizeof (mb.parts));
699 	cp = (uchar_t *)&mb.parts[slot * sizeof (struct ipart)];
700 	/* bootable or not */
701 	*cp++ = active ? ACTIVE : NOTACTIVE;
702 	/* beginning CHS; 0xffffff if not representable */
703 	*cp++ = 0xff;
704 	*cp++ = 0xff;
705 	*cp++ = 0xff;
706 	/* OS type */
707 	*cp++ = EFI_PMBR;
708 	/* ending CHS; 0xffffff if not representable */
709 	*cp++ = 0xff;
710 	*cp++ = 0xff;
711 	*cp++ = 0xff;
712 	/* starting LBA: 1 (little endian format) by EFI definition */
713 	*cp++ = 0x01;
714 	*cp++ = 0x00;
715 	*cp++ = 0x00;
716 	*cp++ = 0x00;
717 	/* ending LBA: last block on the disk (little endian format) */
718 	size_in_lba = vtoc->efi_last_lba;
719 	if (size_in_lba < 0xffffffff) {
720 		*cp++ = (size_in_lba & 0x000000ff);
721 		*cp++ = (size_in_lba & 0x0000ff00) >> 8;
722 		*cp++ = (size_in_lba & 0x00ff0000) >> 16;
723 		*cp++ = (size_in_lba & 0xff000000) >> 24;
724 	} else {
725 		*cp++ = 0xff;
726 		*cp++ = 0xff;
727 		*cp++ = 0xff;
728 		*cp++ = 0xff;
729 	}
730 
731 	(void) memcpy(buf, &mb, sizeof (mb));
732 	/* LINTED -- always longlong aligned */
733 	dk_ioc.dki_data = (efi_gpt_t *)buf;
734 	dk_ioc.dki_lba = 0;
735 	dk_ioc.dki_length = len;
736 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
737 		free(buf);
738 		switch (errno) {
739 		case EIO:
740 			return (VT_EIO);
741 		case EINVAL:
742 			return (VT_EINVAL);
743 		default:
744 			return (VT_ERROR);
745 		}
746 	}
747 	free(buf);
748 	return (0);
749 }
750 
751 /* make sure the user specified something reasonable */
752 static int
753 check_input(struct dk_gpt *vtoc)
754 {
755 	int			resv_part = -1;
756 	int			i, j;
757 	diskaddr_t		istart, jstart, isize, jsize, endsect;
758 
759 	/*
760 	 * Sanity-check the input (make sure no partitions overlap)
761 	 */
762 	for (i = 0; i < vtoc->efi_nparts; i++) {
763 		/* It can't be unassigned and have an actual size */
764 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
765 		    (vtoc->efi_parts[i].p_size != 0)) {
766 			if (efi_debug) {
767 				(void) fprintf(stderr,
768 "partition %d is \"unassigned\" but has a size of %llu",
769 				    i,
770 				    vtoc->efi_parts[i].p_size);
771 			}
772 			return (VT_EINVAL);
773 		}
774 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
775 			if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
776 				continue;
777 			/* we have encountered an unknown uuid */
778 			vtoc->efi_parts[i].p_tag = 0xff;
779 		}
780 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
781 			if (resv_part != -1) {
782 				if (efi_debug) {
783 					(void) fprintf(stderr,
784 "found duplicate reserved partition at %d\n",
785 					    i);
786 				}
787 				return (VT_EINVAL);
788 			}
789 			resv_part = i;
790 		}
791 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
792 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
793 			if (efi_debug) {
794 				(void) fprintf(stderr,
795 				    "Partition %d starts at %llu.  ",
796 				    i,
797 				    vtoc->efi_parts[i].p_start);
798 				(void) fprintf(stderr,
799 				    "It must be between %llu and %llu.\n",
800 				    vtoc->efi_first_u_lba,
801 				    vtoc->efi_last_u_lba);
802 			}
803 			return (VT_EINVAL);
804 		}
805 		if ((vtoc->efi_parts[i].p_start +
806 		    vtoc->efi_parts[i].p_size <
807 		    vtoc->efi_first_u_lba) ||
808 		    (vtoc->efi_parts[i].p_start +
809 		    vtoc->efi_parts[i].p_size >
810 		    vtoc->efi_last_u_lba + 1)) {
811 			if (efi_debug) {
812 				(void) fprintf(stderr,
813 				    "Partition %d ends at %llu.  ",
814 				    i,
815 				    vtoc->efi_parts[i].p_start +
816 				    vtoc->efi_parts[i].p_size);
817 				(void) fprintf(stderr,
818 				    "It must be between %llu and %llu.\n",
819 				    vtoc->efi_first_u_lba,
820 				    vtoc->efi_last_u_lba);
821 			}
822 			return (VT_EINVAL);
823 		}
824 
825 		for (j = 0; j < vtoc->efi_nparts; j++) {
826 			isize = vtoc->efi_parts[i].p_size;
827 			jsize = vtoc->efi_parts[j].p_size;
828 			istart = vtoc->efi_parts[i].p_start;
829 			jstart = vtoc->efi_parts[j].p_start;
830 			if ((i != j) && (isize != 0) && (jsize != 0)) {
831 				endsect = jstart + jsize -1;
832 				if ((jstart <= istart) &&
833 				    (istart <= endsect)) {
834 					if (efi_debug) {
835 						(void) fprintf(stderr,
836 "Partition %d overlaps partition %d.",
837 						    i, j);
838 					}
839 					return (VT_EINVAL);
840 				}
841 			}
842 		}
843 	}
844 	/* just a warning for now */
845 	if ((resv_part == -1) && efi_debug) {
846 		(void) fprintf(stderr,
847 		    "no reserved partition found\n");
848 	}
849 	return (0);
850 }
851 
852 /*
853  * add all the unallocated space to the current label
854  */
855 int
856 efi_use_whole_disk(int fd)
857 {
858 	struct dk_gpt		*efi_label;
859 	int			rval;
860 	int			i;
861 	uint_t			phy_last_slice = 0;
862 	diskaddr_t		pl_start = 0;
863 	diskaddr_t		pl_size;
864 
865 	rval = efi_alloc_and_read(fd, &efi_label);
866 	if (rval < 0) {
867 		return (rval);
868 	}
869 
870 	/* find the last physically non-zero partition */
871 	for (i = 0; i < efi_label->efi_nparts - 2; i ++) {
872 		if (pl_start < efi_label->efi_parts[i].p_start) {
873 			pl_start = efi_label->efi_parts[i].p_start;
874 			phy_last_slice = i;
875 		}
876 	}
877 	pl_size = efi_label->efi_parts[phy_last_slice].p_size;
878 
879 	/*
880 	 * If alter_lba is 1, we are using the backup label.
881 	 * Since we can locate the backup label by disk capacity,
882 	 * there must be no unallocated space.
883 	 */
884 	if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
885 	    >= efi_label->efi_last_lba)) {
886 		if (efi_debug) {
887 			(void) fprintf(stderr,
888 			    "efi_use_whole_disk: requested space not found\n");
889 		}
890 		efi_free(efi_label);
891 		return (VT_ENOSPC);
892 	}
893 
894 	/*
895 	 * If there is space between the last physically non-zero partition
896 	 * and the reserved partition, just add the unallocated space to this
897 	 * area. Otherwise, the unallocated space is added to the last
898 	 * physically non-zero partition.
899 	 */
900 	if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba -
901 	    EFI_MIN_RESV_SIZE) {
902 		efi_label->efi_parts[phy_last_slice].p_size +=
903 		    efi_label->efi_last_lba - efi_label->efi_altern_lba;
904 	}
905 
906 	/*
907 	 * Move the reserved partition. There is currently no data in
908 	 * here except fabricated devids (which get generated via
909 	 * efi_write()). So there is no need to copy data.
910 	 */
911 	efi_label->efi_parts[efi_label->efi_nparts - 1].p_start +=
912 	    efi_label->efi_last_lba - efi_label->efi_altern_lba;
913 	efi_label->efi_last_u_lba += efi_label->efi_last_lba
914 	    - efi_label->efi_altern_lba;
915 
916 	rval = efi_write(fd, efi_label);
917 	if (rval < 0) {
918 		if (efi_debug) {
919 			(void) fprintf(stderr,
920 			    "efi_use_whole_disk:fail to write label, rval=%d\n",
921 			    rval);
922 		}
923 		efi_free(efi_label);
924 		return (rval);
925 	}
926 
927 	efi_free(efi_label);
928 	return (0);
929 }
930 
931 
932 /*
933  * write EFI label and backup label
934  */
935 int
936 efi_write(int fd, struct dk_gpt *vtoc)
937 {
938 	dk_efi_t		dk_ioc;
939 	efi_gpt_t		*efi;
940 	efi_gpe_t		*efi_parts;
941 	int			i, j;
942 	struct dk_cinfo		dki_info;
943 	int			nblocks;
944 	diskaddr_t		lba_backup_gpt_hdr;
945 
946 	if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) {
947 		if (efi_debug)
948 			(void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
949 		switch (errno) {
950 		case EIO:
951 			return (VT_EIO);
952 		case EINVAL:
953 			return (VT_EINVAL);
954 		default:
955 			return (VT_ERROR);
956 		}
957 	}
958 
959 	if (check_input(vtoc))
960 		return (VT_EINVAL);
961 
962 	dk_ioc.dki_lba = 1;
963 	if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
964 		dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
965 	} else {
966 		dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
967 		    vtoc->efi_lbasize) *
968 		    vtoc->efi_lbasize;
969 	}
970 
971 	/*
972 	 * the number of blocks occupied by GUID partition entry array
973 	 */
974 	nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
975 
976 	/*
977 	 * Backup GPT header is located on the block after GUID
978 	 * partition entry array. Here, we calculate the address
979 	 * for backup GPT header.
980 	 */
981 	lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
982 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL)
983 		return (VT_ERROR);
984 
985 	efi = dk_ioc.dki_data;
986 
987 	/* stuff user's input into EFI struct */
988 	efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
989 	efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
990 	efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt));
991 	efi->efi_gpt_Reserved1 = 0;
992 	efi->efi_gpt_MyLBA = LE_64(1ULL);
993 	efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
994 	efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
995 	efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
996 	efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
997 	efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
998 	efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
999 	UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
1000 
1001 	/* LINTED -- always longlong aligned */
1002 	efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
1003 
1004 	for (i = 0; i < vtoc->efi_nparts; i++) {
1005 		for (j = 0;
1006 		    j < sizeof (conversion_array) /
1007 		    sizeof (struct uuid_to_ptag); j++) {
1008 
1009 			if (vtoc->efi_parts[i].p_tag == j) {
1010 				UUID_LE_CONVERT(
1011 				    efi_parts[i].efi_gpe_PartitionTypeGUID,
1012 				    conversion_array[j].uuid);
1013 				break;
1014 			}
1015 		}
1016 
1017 		if (j == sizeof (conversion_array) /
1018 		    sizeof (struct uuid_to_ptag)) {
1019 			/*
1020 			 * If we didn't have a matching uuid match, bail here.
1021 			 * Don't write a label with unknown uuid.
1022 			 */
1023 			if (efi_debug) {
1024 				(void) fprintf(stderr,
1025 				    "Unknown uuid for p_tag %d\n",
1026 				    vtoc->efi_parts[i].p_tag);
1027 			}
1028 			return (VT_EINVAL);
1029 		}
1030 
1031 		efi_parts[i].efi_gpe_StartingLBA =
1032 		    LE_64(vtoc->efi_parts[i].p_start);
1033 		efi_parts[i].efi_gpe_EndingLBA =
1034 		    LE_64(vtoc->efi_parts[i].p_start +
1035 		    vtoc->efi_parts[i].p_size - 1);
1036 		efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
1037 		    LE_16(vtoc->efi_parts[i].p_flag);
1038 		for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1039 			efi_parts[i].efi_gpe_PartitionName[j] =
1040 			    LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1041 		}
1042 		if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1043 		    uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1044 			(void) uuid_generate((uchar_t *)
1045 			    &vtoc->efi_parts[i].p_uguid);
1046 		}
1047 		bcopy(&vtoc->efi_parts[i].p_uguid,
1048 		    &efi_parts[i].efi_gpe_UniquePartitionGUID,
1049 		    sizeof (uuid_t));
1050 	}
1051 	efi->efi_gpt_PartitionEntryArrayCRC32 =
1052 	    LE_32(efi_crc32((unsigned char *)efi_parts,
1053 	    vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1054 	efi->efi_gpt_HeaderCRC32 =
1055 	    LE_32(efi_crc32((unsigned char *)efi, sizeof (struct efi_gpt)));
1056 
1057 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1058 		free(dk_ioc.dki_data);
1059 		switch (errno) {
1060 		case EIO:
1061 			return (VT_EIO);
1062 		case EINVAL:
1063 			return (VT_EINVAL);
1064 		default:
1065 			return (VT_ERROR);
1066 		}
1067 	}
1068 
1069 	/* write backup partition array */
1070 	dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
1071 	dk_ioc.dki_length -= vtoc->efi_lbasize;
1072 	/* LINTED */
1073 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
1074 	    vtoc->efi_lbasize);
1075 
1076 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1077 		/*
1078 		 * we wrote the primary label okay, so don't fail
1079 		 */
1080 		if (efi_debug) {
1081 			(void) fprintf(stderr,
1082 			    "write of backup partitions to block %llu "
1083 			    "failed, errno %d\n",
1084 			    vtoc->efi_last_u_lba + 1,
1085 			    errno);
1086 		}
1087 	}
1088 	/*
1089 	 * now swap MyLBA and AlternateLBA fields and write backup
1090 	 * partition table header
1091 	 */
1092 	dk_ioc.dki_lba = lba_backup_gpt_hdr;
1093 	dk_ioc.dki_length = vtoc->efi_lbasize;
1094 	/* LINTED */
1095 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1096 	    vtoc->efi_lbasize);
1097 	efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1098 	efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1099 	efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1100 	efi->efi_gpt_HeaderCRC32 = 0;
1101 	efi->efi_gpt_HeaderCRC32 =
1102 	    LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1103 	    sizeof (struct efi_gpt)));
1104 
1105 	if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1106 		if (efi_debug) {
1107 			(void) fprintf(stderr,
1108 			    "write of backup header to block %llu failed, "
1109 			    "errno %d\n",
1110 			    lba_backup_gpt_hdr,
1111 			    errno);
1112 		}
1113 	}
1114 	/* write the PMBR */
1115 	(void) write_pmbr(fd, vtoc);
1116 	free(dk_ioc.dki_data);
1117 	return (0);
1118 }
1119 
1120 void
1121 efi_free(struct dk_gpt *ptr)
1122 {
1123 	free(ptr);
1124 }
1125 
1126 /*
1127  * Input: File descriptor
1128  * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR.
1129  * Otherwise 0.
1130  */
1131 int
1132 efi_type(int fd)
1133 {
1134 	struct vtoc vtoc;
1135 	struct extvtoc extvtoc;
1136 
1137 	if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) {
1138 		if (errno == ENOTSUP)
1139 			return (1);
1140 		else if (errno == ENOTTY) {
1141 			if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1)
1142 				if (errno == ENOTSUP)
1143 					return (1);
1144 		}
1145 	}
1146 	return (0);
1147 }
1148 
1149 void
1150 efi_err_check(struct dk_gpt *vtoc)
1151 {
1152 	int			resv_part = -1;
1153 	int			i, j;
1154 	diskaddr_t		istart, jstart, isize, jsize, endsect;
1155 	int			overlap = 0;
1156 
1157 	/*
1158 	 * make sure no partitions overlap
1159 	 */
1160 	for (i = 0; i < vtoc->efi_nparts; i++) {
1161 		/* It can't be unassigned and have an actual size */
1162 		if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1163 		    (vtoc->efi_parts[i].p_size != 0)) {
1164 			(void) fprintf(stderr,
1165 			    "partition %d is \"unassigned\" but has a size "
1166 			    "of %llu\n", i, vtoc->efi_parts[i].p_size);
1167 		}
1168 		if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1169 			continue;
1170 		}
1171 		if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1172 			if (resv_part != -1) {
1173 				(void) fprintf(stderr,
1174 				    "found duplicate reserved partition at "
1175 				    "%d\n", i);
1176 			}
1177 			resv_part = i;
1178 			if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1179 				(void) fprintf(stderr,
1180 				    "Warning: reserved partition size must "
1181 				    "be %d sectors\n", EFI_MIN_RESV_SIZE);
1182 		}
1183 		if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1184 		    (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1185 			(void) fprintf(stderr,
1186 			    "Partition %d starts at %llu\n",
1187 			    i,
1188 			    vtoc->efi_parts[i].p_start);
1189 			(void) fprintf(stderr,
1190 			    "It must be between %llu and %llu.\n",
1191 			    vtoc->efi_first_u_lba,
1192 			    vtoc->efi_last_u_lba);
1193 		}
1194 		if ((vtoc->efi_parts[i].p_start +
1195 		    vtoc->efi_parts[i].p_size <
1196 		    vtoc->efi_first_u_lba) ||
1197 		    (vtoc->efi_parts[i].p_start +
1198 		    vtoc->efi_parts[i].p_size >
1199 		    vtoc->efi_last_u_lba + 1)) {
1200 			(void) fprintf(stderr,
1201 			    "Partition %d ends at %llu\n",
1202 			    i,
1203 			    vtoc->efi_parts[i].p_start +
1204 			    vtoc->efi_parts[i].p_size);
1205 			(void) fprintf(stderr,
1206 			    "It must be between %llu and %llu.\n",
1207 			    vtoc->efi_first_u_lba,
1208 			    vtoc->efi_last_u_lba);
1209 		}
1210 
1211 		for (j = 0; j < vtoc->efi_nparts; j++) {
1212 			isize = vtoc->efi_parts[i].p_size;
1213 			jsize = vtoc->efi_parts[j].p_size;
1214 			istart = vtoc->efi_parts[i].p_start;
1215 			jstart = vtoc->efi_parts[j].p_start;
1216 			if ((i != j) && (isize != 0) && (jsize != 0)) {
1217 				endsect = jstart + jsize -1;
1218 				if ((jstart <= istart) &&
1219 				    (istart <= endsect)) {
1220 					if (!overlap) {
1221 					(void) fprintf(stderr,
1222 					    "label error: EFI Labels do not "
1223 					    "support overlapping partitions\n");
1224 					}
1225 					(void) fprintf(stderr,
1226 					    "Partition %d overlaps partition "
1227 					    "%d.\n", i, j);
1228 					overlap = 1;
1229 				}
1230 			}
1231 		}
1232 	}
1233 	/* make sure there is a reserved partition */
1234 	if (resv_part == -1) {
1235 		(void) fprintf(stderr,
1236 		    "no reserved partition found\n");
1237 	}
1238 }
1239 
1240 /*
1241  * We need to get information necessary to construct a *new* efi
1242  * label type
1243  */
1244 int
1245 efi_auto_sense(int fd, struct dk_gpt **vtoc)
1246 {
1247 
1248 	int	i;
1249 
1250 	/*
1251 	 * Now build the default partition table
1252 	 */
1253 	if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) {
1254 		if (efi_debug) {
1255 			(void) fprintf(stderr, "efi_alloc_and_init failed.\n");
1256 		}
1257 		return (-1);
1258 	}
1259 
1260 	for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) {
1261 		(*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
1262 		(*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
1263 		(*vtoc)->efi_parts[i].p_start = 0;
1264 		(*vtoc)->efi_parts[i].p_size = 0;
1265 	}
1266 	/*
1267 	 * Make constants first
1268 	 * and variable partitions later
1269 	 */
1270 
1271 	/* root partition - s0 128 MB */
1272 	(*vtoc)->efi_parts[0].p_start = 34;
1273 	(*vtoc)->efi_parts[0].p_size = 262144;
1274 
1275 	/* partition - s1  128 MB */
1276 	(*vtoc)->efi_parts[1].p_start = 262178;
1277 	(*vtoc)->efi_parts[1].p_size = 262144;
1278 
1279 	/* partition -s2 is NOT the Backup disk */
1280 	(*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED;
1281 
1282 	/* partition -s6 /usr partition - HOG */
1283 	(*vtoc)->efi_parts[6].p_start = 524322;
1284 	(*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322
1285 	    - (1024 * 16);
1286 
1287 	/* efi reserved partition - s9 16K */
1288 	(*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16);
1289 	(*vtoc)->efi_parts[8].p_size = (1024 * 16);
1290 	(*vtoc)->efi_parts[8].p_tag = V_RESERVED;
1291 	return (0);
1292 }
1293