]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libefi/rdwr_efi.c
Remove bcopy(), bzero(), bcmp()
[mirror_zfs.git] / lib / libefi / rdwr_efi.c
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 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2018 by Delphix. All rights reserved.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <uuid/uuid.h>
35 #include <zlib.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/vdev_disk.h>
46 #include <linux/fs.h>
47 #include <linux/blkpg.h>
48
49 static struct uuid_to_ptag {
50 struct uuid uuid;
51 } conversion_array[] = {
52 { EFI_UNUSED },
53 { EFI_BOOT },
54 { EFI_ROOT },
55 { EFI_SWAP },
56 { EFI_USR },
57 { EFI_BACKUP },
58 { EFI_UNUSED }, /* STAND is never used */
59 { EFI_VAR },
60 { EFI_HOME },
61 { EFI_ALTSCTR },
62 { EFI_UNUSED }, /* CACHE (cachefs) is never used */
63 { EFI_RESERVED },
64 { EFI_SYSTEM },
65 { EFI_LEGACY_MBR },
66 { EFI_SYMC_PUB },
67 { EFI_SYMC_CDS },
68 { EFI_MSFT_RESV },
69 { EFI_DELL_BASIC },
70 { EFI_DELL_RAID },
71 { EFI_DELL_SWAP },
72 { EFI_DELL_LVM },
73 { EFI_DELL_RESV },
74 { EFI_AAPL_HFS },
75 { EFI_AAPL_UFS },
76 { EFI_FREEBSD_BOOT },
77 { EFI_FREEBSD_SWAP },
78 { EFI_FREEBSD_UFS },
79 { EFI_FREEBSD_VINUM },
80 { EFI_FREEBSD_ZFS },
81 { EFI_BIOS_BOOT },
82 { EFI_INTC_RS },
83 { EFI_SNE_BOOT },
84 { EFI_LENOVO_BOOT },
85 { EFI_MSFT_LDMM },
86 { EFI_MSFT_LDMD },
87 { EFI_MSFT_RE },
88 { EFI_IBM_GPFS },
89 { EFI_MSFT_STORAGESPACES },
90 { EFI_HPQ_DATA },
91 { EFI_HPQ_SVC },
92 { EFI_RHT_DATA },
93 { EFI_RHT_HOME },
94 { EFI_RHT_SRV },
95 { EFI_RHT_DMCRYPT },
96 { EFI_RHT_LUKS },
97 { EFI_FREEBSD_DISKLABEL },
98 { EFI_AAPL_RAID },
99 { EFI_AAPL_RAIDOFFLINE },
100 { EFI_AAPL_BOOT },
101 { EFI_AAPL_LABEL },
102 { EFI_AAPL_TVRECOVERY },
103 { EFI_AAPL_CORESTORAGE },
104 { EFI_NETBSD_SWAP },
105 { EFI_NETBSD_FFS },
106 { EFI_NETBSD_LFS },
107 { EFI_NETBSD_RAID },
108 { EFI_NETBSD_CAT },
109 { EFI_NETBSD_CRYPT },
110 { EFI_GOOG_KERN },
111 { EFI_GOOG_ROOT },
112 { EFI_GOOG_RESV },
113 { EFI_HAIKU_BFS },
114 { EFI_MIDNIGHTBSD_BOOT },
115 { EFI_MIDNIGHTBSD_DATA },
116 { EFI_MIDNIGHTBSD_SWAP },
117 { EFI_MIDNIGHTBSD_UFS },
118 { EFI_MIDNIGHTBSD_VINUM },
119 { EFI_MIDNIGHTBSD_ZFS },
120 { EFI_CEPH_JOURNAL },
121 { EFI_CEPH_DMCRYPTJOURNAL },
122 { EFI_CEPH_OSD },
123 { EFI_CEPH_DMCRYPTOSD },
124 { EFI_CEPH_CREATE },
125 { EFI_CEPH_DMCRYPTCREATE },
126 { EFI_OPENBSD_DISKLABEL },
127 { EFI_BBRY_QNX },
128 { EFI_BELL_PLAN9 },
129 { EFI_VMW_KCORE },
130 { EFI_VMW_VMFS },
131 { EFI_VMW_RESV },
132 { EFI_RHT_ROOTX86 },
133 { EFI_RHT_ROOTAMD64 },
134 { EFI_RHT_ROOTARM },
135 { EFI_RHT_ROOTARM64 },
136 { EFI_ACRONIS_SECUREZONE },
137 { EFI_ONIE_BOOT },
138 { EFI_ONIE_CONFIG },
139 { EFI_IBM_PPRPBOOT },
140 { EFI_FREEDESKTOP_BOOT }
141 };
142
143 int efi_debug = 0;
144
145 static int efi_read(int, struct dk_gpt *);
146
147 /*
148 * Return a 32-bit CRC of the contents of the buffer. Pre-and-post
149 * one's conditioning will be handled by crc32() internally.
150 */
151 static uint32_t
152 efi_crc32(const unsigned char *buf, unsigned int size)
153 {
154 uint32_t crc = crc32(0, Z_NULL, 0);
155
156 crc = crc32(crc, buf, size);
157
158 return (crc);
159 }
160
161 static int
162 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize)
163 {
164 int sector_size;
165 unsigned long long capacity_size;
166
167 if (ioctl(fd, BLKSSZGET, &sector_size) < 0)
168 return (-1);
169
170 if (ioctl(fd, BLKGETSIZE64, &capacity_size) < 0)
171 return (-1);
172
173 *lbsize = (uint_t)sector_size;
174 *capacity = (diskaddr_t)(capacity_size / sector_size);
175
176 return (0);
177 }
178
179 /*
180 * Return back the device name associated with the file descriptor. The
181 * caller is responsible for freeing the memory associated with the
182 * returned string.
183 */
184 static char *
185 efi_get_devname(int fd)
186 {
187 char path[32];
188
189 /*
190 * The libefi API only provides the open fd and not the file path.
191 * To handle this realpath(3) is used to resolve the block device
192 * name from /proc/self/fd/<fd>.
193 */
194 (void) snprintf(path, sizeof (path), "/proc/self/fd/%d", fd);
195 return (realpath(path, NULL));
196 }
197
198 static int
199 efi_get_info(int fd, struct dk_cinfo *dki_info)
200 {
201 char *dev_path;
202 int rval = 0;
203
204 memset(dki_info, 0, sizeof (*dki_info));
205
206 /*
207 * The simplest way to get the partition number under linux is
208 * to parse it out of the /dev/<disk><partition> block device name.
209 * The kernel creates this using the partition number when it
210 * populates /dev/ so it may be trusted. The tricky bit here is
211 * that the naming convention is based on the block device type.
212 * So we need to take this in to account when parsing out the
213 * partition information. Aside from the partition number we collect
214 * some additional device info.
215 */
216 dev_path = efi_get_devname(fd);
217 if (dev_path == NULL)
218 goto error;
219
220 if ((strncmp(dev_path, "/dev/sd", 7) == 0)) {
221 strcpy(dki_info->dki_cname, "sd");
222 dki_info->dki_ctype = DKC_SCSI_CCS;
223 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
224 dki_info->dki_dname,
225 &dki_info->dki_partition);
226 } else if ((strncmp(dev_path, "/dev/hd", 7) == 0)) {
227 strcpy(dki_info->dki_cname, "hd");
228 dki_info->dki_ctype = DKC_DIRECT;
229 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
230 dki_info->dki_dname,
231 &dki_info->dki_partition);
232 } else if ((strncmp(dev_path, "/dev/md", 7) == 0)) {
233 strcpy(dki_info->dki_cname, "pseudo");
234 dki_info->dki_ctype = DKC_MD;
235 strcpy(dki_info->dki_dname, "md");
236 rval = sscanf(dev_path, "/dev/md%[0-9]p%hu",
237 dki_info->dki_dname + 2,
238 &dki_info->dki_partition);
239 } else if ((strncmp(dev_path, "/dev/vd", 7) == 0)) {
240 strcpy(dki_info->dki_cname, "vd");
241 dki_info->dki_ctype = DKC_MD;
242 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
243 dki_info->dki_dname,
244 &dki_info->dki_partition);
245 } else if ((strncmp(dev_path, "/dev/xvd", 8) == 0)) {
246 strcpy(dki_info->dki_cname, "xvd");
247 dki_info->dki_ctype = DKC_MD;
248 rval = sscanf(dev_path, "/dev/%[a-zA-Z]%hu",
249 dki_info->dki_dname,
250 &dki_info->dki_partition);
251 } else if ((strncmp(dev_path, "/dev/zd", 7) == 0)) {
252 strcpy(dki_info->dki_cname, "zd");
253 dki_info->dki_ctype = DKC_MD;
254 strcpy(dki_info->dki_dname, "zd");
255 rval = sscanf(dev_path, "/dev/zd%[0-9]p%hu",
256 dki_info->dki_dname + 2,
257 &dki_info->dki_partition);
258 } else if ((strncmp(dev_path, "/dev/dm-", 8) == 0)) {
259 strcpy(dki_info->dki_cname, "pseudo");
260 dki_info->dki_ctype = DKC_VBD;
261 strcpy(dki_info->dki_dname, "dm-");
262 rval = sscanf(dev_path, "/dev/dm-%[0-9]p%hu",
263 dki_info->dki_dname + 3,
264 &dki_info->dki_partition);
265 } else if ((strncmp(dev_path, "/dev/ram", 8) == 0)) {
266 strcpy(dki_info->dki_cname, "pseudo");
267 dki_info->dki_ctype = DKC_PCMCIA_MEM;
268 strcpy(dki_info->dki_dname, "ram");
269 rval = sscanf(dev_path, "/dev/ram%[0-9]p%hu",
270 dki_info->dki_dname + 3,
271 &dki_info->dki_partition);
272 } else if ((strncmp(dev_path, "/dev/loop", 9) == 0)) {
273 strcpy(dki_info->dki_cname, "pseudo");
274 dki_info->dki_ctype = DKC_VBD;
275 strcpy(dki_info->dki_dname, "loop");
276 rval = sscanf(dev_path, "/dev/loop%[0-9]p%hu",
277 dki_info->dki_dname + 4,
278 &dki_info->dki_partition);
279 } else if ((strncmp(dev_path, "/dev/nvme", 9) == 0)) {
280 strcpy(dki_info->dki_cname, "nvme");
281 dki_info->dki_ctype = DKC_SCSI_CCS;
282 strcpy(dki_info->dki_dname, "nvme");
283 (void) sscanf(dev_path, "/dev/nvme%[0-9]",
284 dki_info->dki_dname + 4);
285 size_t controller_length = strlen(
286 dki_info->dki_dname);
287 strcpy(dki_info->dki_dname + controller_length,
288 "n");
289 rval = sscanf(dev_path,
290 "/dev/nvme%*[0-9]n%[0-9]p%hu",
291 dki_info->dki_dname + controller_length + 1,
292 &dki_info->dki_partition);
293 } else {
294 strcpy(dki_info->dki_dname, "unknown");
295 strcpy(dki_info->dki_cname, "unknown");
296 dki_info->dki_ctype = DKC_UNKNOWN;
297 }
298
299 switch (rval) {
300 case 0:
301 errno = EINVAL;
302 goto error;
303 case 1:
304 dki_info->dki_partition = 0;
305 }
306
307 free(dev_path);
308
309 return (0);
310 error:
311 if (efi_debug)
312 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno);
313
314 switch (errno) {
315 case EIO:
316 return (VT_EIO);
317 case EINVAL:
318 return (VT_EINVAL);
319 default:
320 return (VT_ERROR);
321 }
322 }
323
324 /*
325 * the number of blocks the EFI label takes up (round up to nearest
326 * block)
327 */
328 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \
329 ((l) - 1)) / (l)))
330 /* number of partitions -- limited by what we can malloc */
331 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \
332 sizeof (struct dk_part))
333
334 int
335 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc)
336 {
337 diskaddr_t capacity = 0;
338 uint_t lbsize = 0;
339 uint_t nblocks;
340 size_t length;
341 struct dk_gpt *vptr;
342 struct uuid uuid;
343 struct dk_cinfo dki_info;
344
345 if (read_disk_info(fd, &capacity, &lbsize) != 0)
346 return (-1);
347
348 if (efi_get_info(fd, &dki_info) != 0)
349 return (-1);
350
351 if (dki_info.dki_partition != 0)
352 return (-1);
353
354 if ((dki_info.dki_ctype == DKC_PCMCIA_MEM) ||
355 (dki_info.dki_ctype == DKC_VBD) ||
356 (dki_info.dki_ctype == DKC_UNKNOWN))
357 return (-1);
358
359 nblocks = NBLOCKS(nparts, lbsize);
360 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) {
361 /* 16K plus one block for the GPT */
362 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1;
363 }
364
365 if (nparts > MAX_PARTS) {
366 if (efi_debug) {
367 (void) fprintf(stderr,
368 "the maximum number of partitions supported is %lu\n",
369 MAX_PARTS);
370 }
371 return (-1);
372 }
373
374 length = sizeof (struct dk_gpt) +
375 sizeof (struct dk_part) * (nparts - 1);
376
377 vptr = calloc(1, length);
378 if (vptr == NULL)
379 return (-1);
380
381 *vtoc = vptr;
382
383 vptr->efi_version = EFI_VERSION_CURRENT;
384 vptr->efi_lbasize = lbsize;
385 vptr->efi_nparts = nparts;
386 /*
387 * add one block here for the PMBR; on disks with a 512 byte
388 * block size and 128 or fewer partitions, efi_first_u_lba
389 * should work out to "34"
390 */
391 vptr->efi_first_u_lba = nblocks + 1;
392 vptr->efi_last_lba = capacity - 1;
393 vptr->efi_altern_lba = capacity -1;
394 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks;
395
396 (void) uuid_generate((uchar_t *)&uuid);
397 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid);
398 return (0);
399 }
400
401 /*
402 * Read EFI - return partition number upon success.
403 */
404 int
405 efi_alloc_and_read(int fd, struct dk_gpt **vtoc)
406 {
407 int rval;
408 uint32_t nparts;
409 int length;
410 struct dk_gpt *vptr;
411
412 /* figure out the number of entries that would fit into 16K */
413 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t);
414 length = (int) sizeof (struct dk_gpt) +
415 (int) sizeof (struct dk_part) * (nparts - 1);
416 vptr = calloc(1, length);
417
418 if (vptr == NULL)
419 return (VT_ERROR);
420
421 vptr->efi_nparts = nparts;
422 rval = efi_read(fd, vptr);
423
424 if ((rval == VT_EINVAL) && vptr->efi_nparts > nparts) {
425 void *tmp;
426 length = (int) sizeof (struct dk_gpt) +
427 (int) sizeof (struct dk_part) * (vptr->efi_nparts - 1);
428 nparts = vptr->efi_nparts;
429 if ((tmp = realloc(vptr, length)) == NULL) {
430 /* cppcheck-suppress doubleFree */
431 free(vptr);
432 *vtoc = NULL;
433 return (VT_ERROR);
434 } else {
435 vptr = tmp;
436 rval = efi_read(fd, vptr);
437 }
438 }
439
440 if (rval < 0) {
441 if (efi_debug) {
442 (void) fprintf(stderr,
443 "read of EFI table failed, rval=%d\n", rval);
444 }
445 free(vptr);
446 *vtoc = NULL;
447 } else {
448 *vtoc = vptr;
449 }
450
451 return (rval);
452 }
453
454 static int
455 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
456 {
457 void *data = dk_ioc->dki_data;
458 int error;
459 diskaddr_t capacity;
460 uint_t lbsize;
461
462 /*
463 * When the IO is not being performed in kernel as an ioctl we need
464 * to know the sector size so we can seek to the proper byte offset.
465 */
466 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
467 if (efi_debug)
468 fprintf(stderr, "unable to read disk info: %d", errno);
469
470 errno = EIO;
471 return (-1);
472 }
473
474 switch (cmd) {
475 case DKIOCGETEFI:
476 if (lbsize == 0) {
477 if (efi_debug)
478 (void) fprintf(stderr, "DKIOCGETEFI assuming "
479 "LBA %d bytes\n", DEV_BSIZE);
480
481 lbsize = DEV_BSIZE;
482 }
483
484 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
485 if (error == -1) {
486 if (efi_debug)
487 (void) fprintf(stderr, "DKIOCGETEFI lseek "
488 "error: %d\n", errno);
489 return (error);
490 }
491
492 error = read(fd, data, dk_ioc->dki_length);
493 if (error == -1) {
494 if (efi_debug)
495 (void) fprintf(stderr, "DKIOCGETEFI read "
496 "error: %d\n", errno);
497 return (error);
498 }
499
500 if (error != dk_ioc->dki_length) {
501 if (efi_debug)
502 (void) fprintf(stderr, "DKIOCGETEFI short "
503 "read of %d bytes\n", error);
504 errno = EIO;
505 return (-1);
506 }
507 error = 0;
508 break;
509
510 case DKIOCSETEFI:
511 if (lbsize == 0) {
512 if (efi_debug)
513 (void) fprintf(stderr, "DKIOCSETEFI unknown "
514 "LBA size\n");
515 errno = EIO;
516 return (-1);
517 }
518
519 error = lseek(fd, dk_ioc->dki_lba * lbsize, SEEK_SET);
520 if (error == -1) {
521 if (efi_debug)
522 (void) fprintf(stderr, "DKIOCSETEFI lseek "
523 "error: %d\n", errno);
524 return (error);
525 }
526
527 error = write(fd, data, dk_ioc->dki_length);
528 if (error == -1) {
529 if (efi_debug)
530 (void) fprintf(stderr, "DKIOCSETEFI write "
531 "error: %d\n", errno);
532 return (error);
533 }
534
535 if (error != dk_ioc->dki_length) {
536 if (efi_debug)
537 (void) fprintf(stderr, "DKIOCSETEFI short "
538 "write of %d bytes\n", error);
539 errno = EIO;
540 return (-1);
541 }
542
543 /* Sync the new EFI table to disk */
544 error = fsync(fd);
545 if (error == -1)
546 return (error);
547
548 /* Ensure any local disk cache is also flushed */
549 if (ioctl(fd, BLKFLSBUF, 0) == -1)
550 return (error);
551
552 error = 0;
553 break;
554
555 default:
556 if (efi_debug)
557 (void) fprintf(stderr, "unsupported ioctl()\n");
558
559 errno = EIO;
560 return (-1);
561 }
562
563 return (error);
564 }
565
566 int
567 efi_rescan(int fd)
568 {
569 int retry = 10;
570 int error;
571
572 /* Notify the kernel a devices partition table has been updated */
573 while ((error = ioctl(fd, BLKRRPART)) != 0) {
574 if ((--retry == 0) || (errno != EBUSY)) {
575 (void) fprintf(stderr, "the kernel failed to rescan "
576 "the partition table: %d\n", errno);
577 return (-1);
578 }
579 usleep(50000);
580 }
581
582 return (0);
583 }
584
585 static int
586 check_label(int fd, dk_efi_t *dk_ioc)
587 {
588 efi_gpt_t *efi;
589 uint_t crc;
590
591 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) {
592 switch (errno) {
593 case EIO:
594 return (VT_EIO);
595 default:
596 return (VT_ERROR);
597 }
598 }
599 efi = dk_ioc->dki_data;
600 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) {
601 if (efi_debug)
602 (void) fprintf(stderr,
603 "Bad EFI signature: 0x%llx != 0x%llx\n",
604 (long long)efi->efi_gpt_Signature,
605 (long long)LE_64(EFI_SIGNATURE));
606 return (VT_EINVAL);
607 }
608
609 /*
610 * check CRC of the header; the size of the header should
611 * never be larger than one block
612 */
613 crc = efi->efi_gpt_HeaderCRC32;
614 efi->efi_gpt_HeaderCRC32 = 0;
615 len_t headerSize = (len_t)LE_32(efi->efi_gpt_HeaderSize);
616
617 if (headerSize < EFI_MIN_LABEL_SIZE || headerSize > EFI_LABEL_SIZE) {
618 if (efi_debug)
619 (void) fprintf(stderr,
620 "Invalid EFI HeaderSize %llu. Assuming %d.\n",
621 headerSize, EFI_MIN_LABEL_SIZE);
622 }
623
624 if ((headerSize > dk_ioc->dki_length) ||
625 crc != LE_32(efi_crc32((unsigned char *)efi, headerSize))) {
626 if (efi_debug)
627 (void) fprintf(stderr,
628 "Bad EFI CRC: 0x%x != 0x%x\n",
629 crc, LE_32(efi_crc32((unsigned char *)efi,
630 headerSize)));
631 return (VT_EINVAL);
632 }
633
634 return (0);
635 }
636
637 static int
638 efi_read(int fd, struct dk_gpt *vtoc)
639 {
640 int i, j;
641 int label_len;
642 int rval = 0;
643 int md_flag = 0;
644 int vdc_flag = 0;
645 diskaddr_t capacity = 0;
646 uint_t lbsize = 0;
647 struct dk_minfo disk_info;
648 dk_efi_t dk_ioc;
649 efi_gpt_t *efi;
650 efi_gpe_t *efi_parts;
651 struct dk_cinfo dki_info;
652 uint32_t user_length;
653 boolean_t legacy_label = B_FALSE;
654
655 /*
656 * get the partition number for this file descriptor.
657 */
658 if ((rval = efi_get_info(fd, &dki_info)) != 0)
659 return (rval);
660
661 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
662 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
663 md_flag++;
664 } else if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) &&
665 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) {
666 /*
667 * The controller and drive name "vdc" (virtual disk client)
668 * indicates a LDoms virtual disk.
669 */
670 vdc_flag++;
671 }
672
673 /* get the LBA size */
674 if (read_disk_info(fd, &capacity, &lbsize) == -1) {
675 if (efi_debug) {
676 (void) fprintf(stderr,
677 "unable to read disk info: %d",
678 errno);
679 }
680 return (VT_EINVAL);
681 }
682
683 disk_info.dki_lbsize = lbsize;
684 disk_info.dki_capacity = capacity;
685
686 if (disk_info.dki_lbsize == 0) {
687 if (efi_debug) {
688 (void) fprintf(stderr,
689 "efi_read: assuming LBA 512 bytes\n");
690 }
691 disk_info.dki_lbsize = DEV_BSIZE;
692 }
693 /*
694 * Read the EFI GPT to figure out how many partitions we need
695 * to deal with.
696 */
697 dk_ioc.dki_lba = 1;
698 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) {
699 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize;
700 } else {
701 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) +
702 disk_info.dki_lbsize;
703 if (label_len % disk_info.dki_lbsize) {
704 /* pad to physical sector size */
705 label_len += disk_info.dki_lbsize;
706 label_len &= ~(disk_info.dki_lbsize - 1);
707 }
708 }
709
710 if (posix_memalign((void **)&dk_ioc.dki_data,
711 disk_info.dki_lbsize, label_len))
712 return (VT_ERROR);
713
714 memset(dk_ioc.dki_data, 0, label_len);
715 dk_ioc.dki_length = disk_info.dki_lbsize;
716 user_length = vtoc->efi_nparts;
717 efi = dk_ioc.dki_data;
718 if (md_flag) {
719 dk_ioc.dki_length = label_len;
720 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
721 switch (errno) {
722 case EIO:
723 return (VT_EIO);
724 default:
725 return (VT_ERROR);
726 }
727 }
728 } else if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) {
729 /*
730 * No valid label here; try the alternate. Note that here
731 * we just read GPT header and save it into dk_ioc.data,
732 * Later, we will read GUID partition entry array if we
733 * can get valid GPT header.
734 */
735
736 /*
737 * This is a workaround for legacy systems. In the past, the
738 * last sector of SCSI disk was invisible on x86 platform. At
739 * that time, backup label was saved on the next to the last
740 * sector. It is possible for users to move a disk from previous
741 * solaris system to present system. Here, we attempt to search
742 * legacy backup EFI label first.
743 */
744 dk_ioc.dki_lba = disk_info.dki_capacity - 2;
745 dk_ioc.dki_length = disk_info.dki_lbsize;
746 rval = check_label(fd, &dk_ioc);
747 if (rval == VT_EINVAL) {
748 /*
749 * we didn't find legacy backup EFI label, try to
750 * search backup EFI label in the last block.
751 */
752 dk_ioc.dki_lba = disk_info.dki_capacity - 1;
753 dk_ioc.dki_length = disk_info.dki_lbsize;
754 rval = check_label(fd, &dk_ioc);
755 if (rval == 0) {
756 legacy_label = B_TRUE;
757 if (efi_debug)
758 (void) fprintf(stderr,
759 "efi_read: primary label corrupt; "
760 "using EFI backup label located on"
761 " the last block\n");
762 }
763 } else {
764 if ((efi_debug) && (rval == 0))
765 (void) fprintf(stderr, "efi_read: primary label"
766 " corrupt; using legacy EFI backup label "
767 " located on the next to last block\n");
768 }
769
770 if (rval == 0) {
771 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
772 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT;
773 vtoc->efi_nparts =
774 LE_32(efi->efi_gpt_NumberOfPartitionEntries);
775 /*
776 * Partition tables are between backup GPT header
777 * table and ParitionEntryLBA (the starting LBA of
778 * the GUID partition entries array). Now that we
779 * already got valid GPT header and saved it in
780 * dk_ioc.dki_data, we try to get GUID partition
781 * entry array here.
782 */
783 /* LINTED */
784 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
785 + disk_info.dki_lbsize);
786 if (legacy_label)
787 dk_ioc.dki_length = disk_info.dki_capacity - 1 -
788 dk_ioc.dki_lba;
789 else
790 dk_ioc.dki_length = disk_info.dki_capacity - 2 -
791 dk_ioc.dki_lba;
792 dk_ioc.dki_length *= disk_info.dki_lbsize;
793 if (dk_ioc.dki_length >
794 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) {
795 rval = VT_EINVAL;
796 } else {
797 /*
798 * read GUID partition entry array
799 */
800 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
801 }
802 }
803
804 } else if (rval == 0) {
805
806 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA);
807 /* LINTED */
808 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data
809 + disk_info.dki_lbsize);
810 dk_ioc.dki_length = label_len - disk_info.dki_lbsize;
811 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc);
812
813 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) {
814 /*
815 * When the device is a LDoms virtual disk, the DKIOCGETEFI
816 * ioctl can fail with EINVAL if the virtual disk backend
817 * is a ZFS volume serviced by a domain running an old version
818 * of Solaris. This is because the DKIOCGETEFI ioctl was
819 * initially incorrectly implemented for a ZFS volume and it
820 * expected the GPT and GPE to be retrieved with a single ioctl.
821 * So we try to read the GPT and the GPE using that old style
822 * ioctl.
823 */
824 dk_ioc.dki_lba = 1;
825 dk_ioc.dki_length = label_len;
826 rval = check_label(fd, &dk_ioc);
827 }
828
829 if (rval < 0) {
830 free(efi);
831 return (rval);
832 }
833
834 /* LINTED -- always longlong aligned */
835 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize);
836
837 /*
838 * Assemble this into a "dk_gpt" struct for easier
839 * digestibility by applications.
840 */
841 vtoc->efi_version = LE_32(efi->efi_gpt_Revision);
842 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries);
843 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry);
844 vtoc->efi_lbasize = disk_info.dki_lbsize;
845 vtoc->efi_last_lba = disk_info.dki_capacity - 1;
846 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA);
847 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA);
848 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA);
849 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID);
850
851 /*
852 * If the array the user passed in is too small, set the length
853 * to what it needs to be and return
854 */
855 if (user_length < vtoc->efi_nparts) {
856 return (VT_EINVAL);
857 }
858
859 for (i = 0; i < vtoc->efi_nparts; i++) {
860
861 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid,
862 efi_parts[i].efi_gpe_PartitionTypeGUID);
863
864 for (j = 0;
865 j < sizeof (conversion_array)
866 / sizeof (struct uuid_to_ptag); j++) {
867
868 if (memcmp(&vtoc->efi_parts[i].p_guid,
869 &conversion_array[j].uuid,
870 sizeof (struct uuid)) == 0) {
871 vtoc->efi_parts[i].p_tag = j;
872 break;
873 }
874 }
875 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
876 continue;
877 vtoc->efi_parts[i].p_flag =
878 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs);
879 vtoc->efi_parts[i].p_start =
880 LE_64(efi_parts[i].efi_gpe_StartingLBA);
881 vtoc->efi_parts[i].p_size =
882 LE_64(efi_parts[i].efi_gpe_EndingLBA) -
883 vtoc->efi_parts[i].p_start + 1;
884 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
885 vtoc->efi_parts[i].p_name[j] =
886 (uchar_t)LE_16(
887 efi_parts[i].efi_gpe_PartitionName[j]);
888 }
889
890 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid,
891 efi_parts[i].efi_gpe_UniquePartitionGUID);
892 }
893 free(efi);
894
895 return (dki_info.dki_partition);
896 }
897
898 /* writes a "protective" MBR */
899 static int
900 write_pmbr(int fd, struct dk_gpt *vtoc)
901 {
902 dk_efi_t dk_ioc;
903 struct mboot mb;
904 uchar_t *cp;
905 diskaddr_t size_in_lba;
906 uchar_t *buf;
907 int len;
908
909 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize;
910 if (posix_memalign((void **)&buf, len, len))
911 return (VT_ERROR);
912
913 /*
914 * Preserve any boot code and disk signature if the first block is
915 * already an MBR.
916 */
917 memset(buf, 0, len);
918 dk_ioc.dki_lba = 0;
919 dk_ioc.dki_length = len;
920 /* LINTED -- always longlong aligned */
921 dk_ioc.dki_data = (efi_gpt_t *)buf;
922 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) {
923 memset(&mb, 0, sizeof (mb));
924 mb.signature = LE_16(MBB_MAGIC);
925 } else {
926 (void) memcpy(&mb, buf, sizeof (mb));
927 if (mb.signature != LE_16(MBB_MAGIC)) {
928 memset(&mb, 0, sizeof (mb));
929 mb.signature = LE_16(MBB_MAGIC);
930 }
931 }
932
933 memset(&mb.parts, 0, sizeof (mb.parts));
934 cp = (uchar_t *)&mb.parts[0];
935 /* bootable or not */
936 *cp++ = 0;
937 /* beginning CHS; 0xffffff if not representable */
938 *cp++ = 0xff;
939 *cp++ = 0xff;
940 *cp++ = 0xff;
941 /* OS type */
942 *cp++ = EFI_PMBR;
943 /* ending CHS; 0xffffff if not representable */
944 *cp++ = 0xff;
945 *cp++ = 0xff;
946 *cp++ = 0xff;
947 /* starting LBA: 1 (little endian format) by EFI definition */
948 *cp++ = 0x01;
949 *cp++ = 0x00;
950 *cp++ = 0x00;
951 *cp++ = 0x00;
952 /* ending LBA: last block on the disk (little endian format) */
953 size_in_lba = vtoc->efi_last_lba;
954 if (size_in_lba < 0xffffffff) {
955 *cp++ = (size_in_lba & 0x000000ff);
956 *cp++ = (size_in_lba & 0x0000ff00) >> 8;
957 *cp++ = (size_in_lba & 0x00ff0000) >> 16;
958 *cp++ = (size_in_lba & 0xff000000) >> 24;
959 } else {
960 *cp++ = 0xff;
961 *cp++ = 0xff;
962 *cp++ = 0xff;
963 *cp++ = 0xff;
964 }
965
966 (void) memcpy(buf, &mb, sizeof (mb));
967 /* LINTED -- always longlong aligned */
968 dk_ioc.dki_data = (efi_gpt_t *)buf;
969 dk_ioc.dki_lba = 0;
970 dk_ioc.dki_length = len;
971 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
972 free(buf);
973 switch (errno) {
974 case EIO:
975 return (VT_EIO);
976 case EINVAL:
977 return (VT_EINVAL);
978 default:
979 return (VT_ERROR);
980 }
981 }
982 free(buf);
983 return (0);
984 }
985
986 /* make sure the user specified something reasonable */
987 static int
988 check_input(struct dk_gpt *vtoc)
989 {
990 int resv_part = -1;
991 int i, j;
992 diskaddr_t istart, jstart, isize, jsize, endsect;
993
994 /*
995 * Sanity-check the input (make sure no partitions overlap)
996 */
997 for (i = 0; i < vtoc->efi_nparts; i++) {
998 /* It can't be unassigned and have an actual size */
999 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1000 (vtoc->efi_parts[i].p_size != 0)) {
1001 if (efi_debug) {
1002 (void) fprintf(stderr, "partition %d is "
1003 "\"unassigned\" but has a size of %llu",
1004 i, vtoc->efi_parts[i].p_size);
1005 }
1006 return (VT_EINVAL);
1007 }
1008 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1009 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid))
1010 continue;
1011 /* we have encountered an unknown uuid */
1012 vtoc->efi_parts[i].p_tag = 0xff;
1013 }
1014 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1015 if (resv_part != -1) {
1016 if (efi_debug) {
1017 (void) fprintf(stderr, "found "
1018 "duplicate reserved partition "
1019 "at %d\n", i);
1020 }
1021 return (VT_EINVAL);
1022 }
1023 resv_part = i;
1024 }
1025 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1026 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1027 if (efi_debug) {
1028 (void) fprintf(stderr,
1029 "Partition %d starts at %llu. ",
1030 i,
1031 vtoc->efi_parts[i].p_start);
1032 (void) fprintf(stderr,
1033 "It must be between %llu and %llu.\n",
1034 vtoc->efi_first_u_lba,
1035 vtoc->efi_last_u_lba);
1036 }
1037 return (VT_EINVAL);
1038 }
1039 if ((vtoc->efi_parts[i].p_start +
1040 vtoc->efi_parts[i].p_size <
1041 vtoc->efi_first_u_lba) ||
1042 (vtoc->efi_parts[i].p_start +
1043 vtoc->efi_parts[i].p_size >
1044 vtoc->efi_last_u_lba + 1)) {
1045 if (efi_debug) {
1046 (void) fprintf(stderr,
1047 "Partition %d ends at %llu. ",
1048 i,
1049 vtoc->efi_parts[i].p_start +
1050 vtoc->efi_parts[i].p_size);
1051 (void) fprintf(stderr,
1052 "It must be between %llu and %llu.\n",
1053 vtoc->efi_first_u_lba,
1054 vtoc->efi_last_u_lba);
1055 }
1056 return (VT_EINVAL);
1057 }
1058
1059 for (j = 0; j < vtoc->efi_nparts; j++) {
1060 isize = vtoc->efi_parts[i].p_size;
1061 jsize = vtoc->efi_parts[j].p_size;
1062 istart = vtoc->efi_parts[i].p_start;
1063 jstart = vtoc->efi_parts[j].p_start;
1064 if ((i != j) && (isize != 0) && (jsize != 0)) {
1065 endsect = jstart + jsize -1;
1066 if ((jstart <= istart) &&
1067 (istart <= endsect)) {
1068 if (efi_debug) {
1069 (void) fprintf(stderr,
1070 "Partition %d overlaps "
1071 "partition %d.", i, j);
1072 }
1073 return (VT_EINVAL);
1074 }
1075 }
1076 }
1077 }
1078 /* just a warning for now */
1079 if ((resv_part == -1) && efi_debug) {
1080 (void) fprintf(stderr,
1081 "no reserved partition found\n");
1082 }
1083 return (0);
1084 }
1085
1086 static int
1087 call_blkpg_ioctl(int fd, int command, diskaddr_t start,
1088 diskaddr_t size, uint_t pno)
1089 {
1090 struct blkpg_ioctl_arg ioctl_arg;
1091 struct blkpg_partition linux_part;
1092 memset(&linux_part, 0, sizeof (linux_part));
1093
1094 char *path = efi_get_devname(fd);
1095 if (path == NULL) {
1096 (void) fprintf(stderr, "failed to retrieve device name\n");
1097 return (VT_EINVAL);
1098 }
1099
1100 linux_part.start = start;
1101 linux_part.length = size;
1102 linux_part.pno = pno;
1103 snprintf(linux_part.devname, BLKPG_DEVNAMELTH - 1, "%s%u", path, pno);
1104 linux_part.devname[BLKPG_DEVNAMELTH - 1] = '\0';
1105 free(path);
1106
1107 ioctl_arg.op = command;
1108 ioctl_arg.flags = 0;
1109 ioctl_arg.datalen = sizeof (struct blkpg_partition);
1110 ioctl_arg.data = &linux_part;
1111
1112 return (ioctl(fd, BLKPG, &ioctl_arg));
1113 }
1114
1115 /*
1116 * add all the unallocated space to the current label
1117 */
1118 int
1119 efi_use_whole_disk(int fd)
1120 {
1121 struct dk_gpt *efi_label = NULL;
1122 int rval;
1123 int i;
1124 uint_t resv_index = 0, data_index = 0;
1125 diskaddr_t resv_start = 0, data_start = 0;
1126 diskaddr_t data_size, limit, difference;
1127 boolean_t sync_needed = B_FALSE;
1128 uint_t nblocks;
1129
1130 rval = efi_alloc_and_read(fd, &efi_label);
1131 if (rval < 0) {
1132 if (efi_label != NULL)
1133 efi_free(efi_label);
1134 return (rval);
1135 }
1136
1137 /*
1138 * Find the last physically non-zero partition.
1139 * This should be the reserved partition.
1140 */
1141 for (i = 0; i < efi_label->efi_nparts; i ++) {
1142 if (resv_start < efi_label->efi_parts[i].p_start) {
1143 resv_start = efi_label->efi_parts[i].p_start;
1144 resv_index = i;
1145 }
1146 }
1147
1148 /*
1149 * Find the last physically non-zero partition before that.
1150 * This is the data partition.
1151 */
1152 for (i = 0; i < resv_index; i ++) {
1153 if (data_start < efi_label->efi_parts[i].p_start) {
1154 data_start = efi_label->efi_parts[i].p_start;
1155 data_index = i;
1156 }
1157 }
1158 data_size = efi_label->efi_parts[data_index].p_size;
1159
1160 /*
1161 * See the "efi_alloc_and_init" function for more information
1162 * about where this "nblocks" value comes from.
1163 */
1164 nblocks = efi_label->efi_first_u_lba - 1;
1165
1166 /*
1167 * Determine if the EFI label is out of sync. We check that:
1168 *
1169 * 1. the data partition ends at the limit we set, and
1170 * 2. the reserved partition starts at the limit we set.
1171 *
1172 * If either of these conditions is not met, then we need to
1173 * resync the EFI label.
1174 *
1175 * The limit is the last usable LBA, determined by the last LBA
1176 * and the first usable LBA fields on the EFI label of the disk
1177 * (see the lines directly above). Additionally, we factor in
1178 * EFI_MIN_RESV_SIZE (per its use in "zpool_label_disk") and
1179 * P2ALIGN it to ensure the partition boundaries are aligned
1180 * (for performance reasons). The alignment should match the
1181 * alignment used by the "zpool_label_disk" function.
1182 */
1183 limit = P2ALIGN(efi_label->efi_last_lba - nblocks - EFI_MIN_RESV_SIZE,
1184 PARTITION_END_ALIGNMENT);
1185 if (data_start + data_size != limit || resv_start != limit)
1186 sync_needed = B_TRUE;
1187
1188 if (efi_debug && sync_needed)
1189 (void) fprintf(stderr, "efi_use_whole_disk: sync needed\n");
1190
1191 /*
1192 * If alter_lba is 1, we are using the backup label.
1193 * Since we can locate the backup label by disk capacity,
1194 * there must be no unallocated space.
1195 */
1196 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba
1197 >= efi_label->efi_last_lba && !sync_needed)) {
1198 if (efi_debug) {
1199 (void) fprintf(stderr,
1200 "efi_use_whole_disk: requested space not found\n");
1201 }
1202 efi_free(efi_label);
1203 return (VT_ENOSPC);
1204 }
1205
1206 /*
1207 * Verify that we've found the reserved partition by checking
1208 * that it looks the way it did when we created it in zpool_label_disk.
1209 * If we've found the incorrect partition, then we know that this
1210 * device was reformatted and no longer is solely used by ZFS.
1211 */
1212 if ((efi_label->efi_parts[resv_index].p_size != EFI_MIN_RESV_SIZE) ||
1213 (efi_label->efi_parts[resv_index].p_tag != V_RESERVED) ||
1214 (resv_index != 8)) {
1215 if (efi_debug) {
1216 (void) fprintf(stderr,
1217 "efi_use_whole_disk: wholedisk not available\n");
1218 }
1219 efi_free(efi_label);
1220 return (VT_ENOSPC);
1221 }
1222
1223 if (data_start + data_size != resv_start) {
1224 if (efi_debug) {
1225 (void) fprintf(stderr,
1226 "efi_use_whole_disk: "
1227 "data_start (%lli) + "
1228 "data_size (%lli) != "
1229 "resv_start (%lli)\n",
1230 data_start, data_size, resv_start);
1231 }
1232
1233 return (VT_EINVAL);
1234 }
1235
1236 if (limit < resv_start) {
1237 if (efi_debug) {
1238 (void) fprintf(stderr,
1239 "efi_use_whole_disk: "
1240 "limit (%lli) < resv_start (%lli)\n",
1241 limit, resv_start);
1242 }
1243
1244 return (VT_EINVAL);
1245 }
1246
1247 difference = limit - resv_start;
1248
1249 if (efi_debug)
1250 (void) fprintf(stderr,
1251 "efi_use_whole_disk: difference is %lli\n", difference);
1252
1253 /*
1254 * Move the reserved partition. There is currently no data in
1255 * here except fabricated devids (which get generated via
1256 * efi_write()). So there is no need to copy data.
1257 */
1258 efi_label->efi_parts[data_index].p_size += difference;
1259 efi_label->efi_parts[resv_index].p_start += difference;
1260 efi_label->efi_last_u_lba = efi_label->efi_last_lba - nblocks;
1261
1262 /*
1263 * Rescanning the partition table in the kernel can result
1264 * in the device links to be removed (see comment in vdev_disk_open).
1265 * If BLKPG_RESIZE_PARTITION is available, then we can resize
1266 * the partition table online and avoid having to remove the device
1267 * links used by the pool. This provides a very deterministic
1268 * approach to resizing devices and does not require any
1269 * loops waiting for devices to reappear.
1270 */
1271 #ifdef BLKPG_RESIZE_PARTITION
1272 /*
1273 * Delete the reserved partition since we're about to expand
1274 * the data partition and it would overlap with the reserved
1275 * partition.
1276 * NOTE: The starting index for the ioctl is 1 while for the
1277 * EFI partitions it's 0. For that reason we have to add one
1278 * whenever we make an ioctl call.
1279 */
1280 rval = call_blkpg_ioctl(fd, BLKPG_DEL_PARTITION, 0, 0, resv_index + 1);
1281 if (rval != 0)
1282 goto out;
1283
1284 /*
1285 * Expand the data partition
1286 */
1287 rval = call_blkpg_ioctl(fd, BLKPG_RESIZE_PARTITION,
1288 efi_label->efi_parts[data_index].p_start * efi_label->efi_lbasize,
1289 efi_label->efi_parts[data_index].p_size * efi_label->efi_lbasize,
1290 data_index + 1);
1291 if (rval != 0) {
1292 (void) fprintf(stderr, "Unable to resize data "
1293 "partition: %d\n", rval);
1294 /*
1295 * Since we failed to resize, we need to reset the start
1296 * of the reserve partition and re-create it.
1297 */
1298 efi_label->efi_parts[resv_index].p_start -= difference;
1299 }
1300
1301 /*
1302 * Re-add the reserved partition. If we've expanded the data partition
1303 * then we'll move the reserve partition to the end of the data
1304 * partition. Otherwise, we'll recreate the partition in its original
1305 * location. Note that we do this as best-effort and ignore any
1306 * errors that may arise here. This will ensure that we finish writing
1307 * the EFI label.
1308 */
1309 (void) call_blkpg_ioctl(fd, BLKPG_ADD_PARTITION,
1310 efi_label->efi_parts[resv_index].p_start * efi_label->efi_lbasize,
1311 efi_label->efi_parts[resv_index].p_size * efi_label->efi_lbasize,
1312 resv_index + 1);
1313 #endif
1314
1315 /*
1316 * We're now ready to write the EFI label.
1317 */
1318 if (rval == 0) {
1319 rval = efi_write(fd, efi_label);
1320 if (rval < 0 && efi_debug) {
1321 (void) fprintf(stderr, "efi_use_whole_disk:fail "
1322 "to write label, rval=%d\n", rval);
1323 }
1324 }
1325
1326 out:
1327 efi_free(efi_label);
1328 return (rval);
1329 }
1330
1331 /*
1332 * write EFI label and backup label
1333 */
1334 int
1335 efi_write(int fd, struct dk_gpt *vtoc)
1336 {
1337 dk_efi_t dk_ioc;
1338 efi_gpt_t *efi;
1339 efi_gpe_t *efi_parts;
1340 int i, j;
1341 struct dk_cinfo dki_info;
1342 int rval;
1343 int md_flag = 0;
1344 int nblocks;
1345 diskaddr_t lba_backup_gpt_hdr;
1346
1347 if ((rval = efi_get_info(fd, &dki_info)) != 0)
1348 return (rval);
1349
1350 /* check if we are dealing with a metadevice */
1351 if ((strncmp(dki_info.dki_cname, "pseudo", 7) == 0) &&
1352 (strncmp(dki_info.dki_dname, "md", 3) == 0)) {
1353 md_flag = 1;
1354 }
1355
1356 if (check_input(vtoc)) {
1357 /*
1358 * not valid; if it's a metadevice just pass it down
1359 * because SVM will do its own checking
1360 */
1361 if (md_flag == 0) {
1362 return (VT_EINVAL);
1363 }
1364 }
1365
1366 dk_ioc.dki_lba = 1;
1367 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) {
1368 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize;
1369 } else {
1370 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts,
1371 vtoc->efi_lbasize) *
1372 vtoc->efi_lbasize;
1373 }
1374
1375 /*
1376 * the number of blocks occupied by GUID partition entry array
1377 */
1378 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1;
1379
1380 /*
1381 * Backup GPT header is located on the block after GUID
1382 * partition entry array. Here, we calculate the address
1383 * for backup GPT header.
1384 */
1385 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks;
1386 if (posix_memalign((void **)&dk_ioc.dki_data,
1387 vtoc->efi_lbasize, dk_ioc.dki_length))
1388 return (VT_ERROR);
1389
1390 memset(dk_ioc.dki_data, 0, dk_ioc.dki_length);
1391 efi = dk_ioc.dki_data;
1392
1393 /* stuff user's input into EFI struct */
1394 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1395 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */
1396 efi->efi_gpt_HeaderSize = LE_32(sizeof (struct efi_gpt) - LEN_EFI_PAD);
1397 efi->efi_gpt_Reserved1 = 0;
1398 efi->efi_gpt_MyLBA = LE_64(1ULL);
1399 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr);
1400 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba);
1401 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba);
1402 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1403 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts);
1404 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe));
1405 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid);
1406
1407 /* LINTED -- always longlong aligned */
1408 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize);
1409
1410 for (i = 0; i < vtoc->efi_nparts; i++) {
1411 for (j = 0;
1412 j < sizeof (conversion_array) /
1413 sizeof (struct uuid_to_ptag); j++) {
1414
1415 if (vtoc->efi_parts[i].p_tag == j) {
1416 UUID_LE_CONVERT(
1417 efi_parts[i].efi_gpe_PartitionTypeGUID,
1418 conversion_array[j].uuid);
1419 break;
1420 }
1421 }
1422
1423 if (j == sizeof (conversion_array) /
1424 sizeof (struct uuid_to_ptag)) {
1425 /*
1426 * If we didn't have a matching uuid match, bail here.
1427 * Don't write a label with unknown uuid.
1428 */
1429 if (efi_debug) {
1430 (void) fprintf(stderr,
1431 "Unknown uuid for p_tag %d\n",
1432 vtoc->efi_parts[i].p_tag);
1433 }
1434 return (VT_EINVAL);
1435 }
1436
1437 /* Zero's should be written for empty partitions */
1438 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED)
1439 continue;
1440
1441 efi_parts[i].efi_gpe_StartingLBA =
1442 LE_64(vtoc->efi_parts[i].p_start);
1443 efi_parts[i].efi_gpe_EndingLBA =
1444 LE_64(vtoc->efi_parts[i].p_start +
1445 vtoc->efi_parts[i].p_size - 1);
1446 efi_parts[i].efi_gpe_Attributes.PartitionAttrs =
1447 LE_16(vtoc->efi_parts[i].p_flag);
1448 for (j = 0; j < EFI_PART_NAME_LEN; j++) {
1449 efi_parts[i].efi_gpe_PartitionName[j] =
1450 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]);
1451 }
1452 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) &&
1453 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) {
1454 (void) uuid_generate((uchar_t *)
1455 &vtoc->efi_parts[i].p_uguid);
1456 }
1457 memcpy(&efi_parts[i].efi_gpe_UniquePartitionGUID,
1458 &vtoc->efi_parts[i].p_uguid,
1459 sizeof (uuid_t));
1460 }
1461 efi->efi_gpt_PartitionEntryArrayCRC32 =
1462 LE_32(efi_crc32((unsigned char *)efi_parts,
1463 vtoc->efi_nparts * (int)sizeof (struct efi_gpe)));
1464 efi->efi_gpt_HeaderCRC32 =
1465 LE_32(efi_crc32((unsigned char *)efi,
1466 LE_32(efi->efi_gpt_HeaderSize)));
1467
1468 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1469 free(dk_ioc.dki_data);
1470 switch (errno) {
1471 case EIO:
1472 return (VT_EIO);
1473 case EINVAL:
1474 return (VT_EINVAL);
1475 default:
1476 return (VT_ERROR);
1477 }
1478 }
1479 /* if it's a metadevice we're done */
1480 if (md_flag) {
1481 free(dk_ioc.dki_data);
1482 return (0);
1483 }
1484
1485 /* write backup partition array */
1486 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1;
1487 dk_ioc.dki_length -= vtoc->efi_lbasize;
1488 /* LINTED */
1489 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
1490 vtoc->efi_lbasize);
1491
1492 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1493 /*
1494 * we wrote the primary label okay, so don't fail
1495 */
1496 if (efi_debug) {
1497 (void) fprintf(stderr,
1498 "write of backup partitions to block %llu "
1499 "failed, errno %d\n",
1500 vtoc->efi_last_u_lba + 1,
1501 errno);
1502 }
1503 }
1504 /*
1505 * now swap MyLBA and AlternateLBA fields and write backup
1506 * partition table header
1507 */
1508 dk_ioc.dki_lba = lba_backup_gpt_hdr;
1509 dk_ioc.dki_length = vtoc->efi_lbasize;
1510 /* LINTED */
1511 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
1512 vtoc->efi_lbasize);
1513 efi->efi_gpt_AlternateLBA = LE_64(1ULL);
1514 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr);
1515 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1);
1516 efi->efi_gpt_HeaderCRC32 = 0;
1517 efi->efi_gpt_HeaderCRC32 =
1518 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data,
1519 LE_32(efi->efi_gpt_HeaderSize)));
1520
1521 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) {
1522 if (efi_debug) {
1523 (void) fprintf(stderr,
1524 "write of backup header to block %llu failed, "
1525 "errno %d\n",
1526 lba_backup_gpt_hdr,
1527 errno);
1528 }
1529 }
1530 /* write the PMBR */
1531 (void) write_pmbr(fd, vtoc);
1532 free(dk_ioc.dki_data);
1533
1534 return (0);
1535 }
1536
1537 void
1538 efi_free(struct dk_gpt *ptr)
1539 {
1540 free(ptr);
1541 }
1542
1543 void
1544 efi_err_check(struct dk_gpt *vtoc)
1545 {
1546 int resv_part = -1;
1547 int i, j;
1548 diskaddr_t istart, jstart, isize, jsize, endsect;
1549 int overlap = 0;
1550
1551 /*
1552 * make sure no partitions overlap
1553 */
1554 for (i = 0; i < vtoc->efi_nparts; i++) {
1555 /* It can't be unassigned and have an actual size */
1556 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) &&
1557 (vtoc->efi_parts[i].p_size != 0)) {
1558 (void) fprintf(stderr,
1559 "partition %d is \"unassigned\" but has a size "
1560 "of %llu\n", i, vtoc->efi_parts[i].p_size);
1561 }
1562 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) {
1563 continue;
1564 }
1565 if (vtoc->efi_parts[i].p_tag == V_RESERVED) {
1566 if (resv_part != -1) {
1567 (void) fprintf(stderr,
1568 "found duplicate reserved partition at "
1569 "%d\n", i);
1570 }
1571 resv_part = i;
1572 if (vtoc->efi_parts[i].p_size != EFI_MIN_RESV_SIZE)
1573 (void) fprintf(stderr,
1574 "Warning: reserved partition size must "
1575 "be %d sectors\n", EFI_MIN_RESV_SIZE);
1576 }
1577 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) ||
1578 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) {
1579 (void) fprintf(stderr,
1580 "Partition %d starts at %llu\n",
1581 i,
1582 vtoc->efi_parts[i].p_start);
1583 (void) fprintf(stderr,
1584 "It must be between %llu and %llu.\n",
1585 vtoc->efi_first_u_lba,
1586 vtoc->efi_last_u_lba);
1587 }
1588 if ((vtoc->efi_parts[i].p_start +
1589 vtoc->efi_parts[i].p_size <
1590 vtoc->efi_first_u_lba) ||
1591 (vtoc->efi_parts[i].p_start +
1592 vtoc->efi_parts[i].p_size >
1593 vtoc->efi_last_u_lba + 1)) {
1594 (void) fprintf(stderr,
1595 "Partition %d ends at %llu\n",
1596 i,
1597 vtoc->efi_parts[i].p_start +
1598 vtoc->efi_parts[i].p_size);
1599 (void) fprintf(stderr,
1600 "It must be between %llu and %llu.\n",
1601 vtoc->efi_first_u_lba,
1602 vtoc->efi_last_u_lba);
1603 }
1604
1605 for (j = 0; j < vtoc->efi_nparts; j++) {
1606 isize = vtoc->efi_parts[i].p_size;
1607 jsize = vtoc->efi_parts[j].p_size;
1608 istart = vtoc->efi_parts[i].p_start;
1609 jstart = vtoc->efi_parts[j].p_start;
1610 if ((i != j) && (isize != 0) && (jsize != 0)) {
1611 endsect = jstart + jsize -1;
1612 if ((jstart <= istart) &&
1613 (istart <= endsect)) {
1614 if (!overlap) {
1615 (void) fprintf(stderr,
1616 "label error: EFI Labels do not "
1617 "support overlapping partitions\n");
1618 }
1619 (void) fprintf(stderr,
1620 "Partition %d overlaps partition "
1621 "%d.\n", i, j);
1622 overlap = 1;
1623 }
1624 }
1625 }
1626 }
1627 /* make sure there is a reserved partition */
1628 if (resv_part == -1) {
1629 (void) fprintf(stderr,
1630 "no reserved partition found\n");
1631 }
1632 }