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