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