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