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