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