1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 25 * Copyright 2014 Toomas Soome <tsoome@me.com> 26 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association. 27 * Copyright 2019 Joyent, Inc. 28 */ 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <errno.h> 33 #include <strings.h> 34 #include <unistd.h> 35 #include <smbios.h> 36 #include <uuid/uuid.h> 37 #include <libintl.h> 38 #include <sys/types.h> 39 #include <sys/dkio.h> 40 #include <sys/vtoc.h> 41 #include <sys/mhd.h> 42 #include <sys/param.h> 43 #include <sys/dktp/fdisk.h> 44 #include <sys/efi_partition.h> 45 #include <sys/byteorder.h> 46 #include <sys/ddi.h> 47 48 /* 49 * The original conversion array used simple array index, but since 50 * we do need to take account of VTOC tag numbers from other systems, 51 * we need to provide tag values too, or the array will grow too large. 52 * 53 * Still we will fabricate the missing p_tag values. 54 */ 55 static struct uuid_to_ptag { 56 struct uuid uuid; 57 ushort_t p_tag; 58 } conversion_array[] = { 59 { EFI_UNUSED, V_UNASSIGNED }, 60 { EFI_BOOT, V_BOOT }, 61 { EFI_ROOT, V_ROOT }, 62 { EFI_SWAP, V_SWAP }, 63 { EFI_USR, V_USR }, 64 { EFI_BACKUP, V_BACKUP }, 65 { EFI_VAR, V_VAR }, 66 { EFI_HOME, V_HOME }, 67 { EFI_ALTSCTR, V_ALTSCTR }, 68 { EFI_RESERVED, V_RESERVED }, 69 { EFI_SYSTEM, V_SYSTEM }, /* V_SYSTEM is 0xc */ 70 { EFI_LEGACY_MBR, 0x10 }, 71 { EFI_SYMC_PUB, 0x11 }, 72 { EFI_SYMC_CDS, 0x12 }, 73 { EFI_MSFT_RESV, 0x13 }, 74 { EFI_DELL_BASIC, 0x14 }, 75 { EFI_DELL_RAID, 0x15 }, 76 { EFI_DELL_SWAP, 0x16 }, 77 { EFI_DELL_LVM, 0x17 }, 78 { EFI_DELL_RESV, 0x19 }, 79 { EFI_AAPL_HFS, 0x1a }, 80 { EFI_AAPL_UFS, 0x1b }, 81 { EFI_AAPL_ZFS, 0x1c }, 82 { EFI_AAPL_APFS, 0x1d }, 83 { EFI_BIOS_BOOT, V_BIOS_BOOT }, /* V_BIOS_BOOT is 0x18 */ 84 { EFI_FREEBSD_BOOT, V_FREEBSD_BOOT }, 85 { EFI_FREEBSD_SWAP, V_FREEBSD_SWAP }, 86 { EFI_FREEBSD_UFS, V_FREEBSD_UFS }, 87 { EFI_FREEBSD_VINUM, V_FREEBSD_VINUM }, 88 { EFI_FREEBSD_ZFS, V_FREEBSD_ZFS }, 89 { EFI_FREEBSD_NANDFS, V_FREEBSD_NANDFS } 90 }; 91 92 /* 93 * Default vtoc information for non-SVr4 partitions 94 */ 95 struct dk_map2 default_vtoc_map[NDKMAP] = { 96 { V_ROOT, 0 }, /* a - 0 */ 97 { V_SWAP, V_UNMNT }, /* b - 1 */ 98 { V_BACKUP, V_UNMNT }, /* c - 2 */ 99 { V_UNASSIGNED, 0 }, /* d - 3 */ 100 { V_UNASSIGNED, 0 }, /* e - 4 */ 101 { V_UNASSIGNED, 0 }, /* f - 5 */ 102 { V_USR, 0 }, /* g - 6 */ 103 { V_UNASSIGNED, 0 }, /* h - 7 */ 104 105 #if defined(_SUNOS_VTOC_16) 106 107 #if defined(i386) || defined(__amd64) 108 { V_BOOT, V_UNMNT }, /* i - 8 */ 109 { V_ALTSCTR, 0 }, /* j - 9 */ 110 111 #else 112 #error No VTOC format defined. 113 #endif /* defined(i386) */ 114 115 { V_UNASSIGNED, 0 }, /* k - 10 */ 116 { V_UNASSIGNED, 0 }, /* l - 11 */ 117 { V_UNASSIGNED, 0 }, /* m - 12 */ 118 { V_UNASSIGNED, 0 }, /* n - 13 */ 119 { V_UNASSIGNED, 0 }, /* o - 14 */ 120 { V_UNASSIGNED, 0 }, /* p - 15 */ 121 #endif /* defined(_SUNOS_VTOC_16) */ 122 }; 123 124 #ifdef DEBUG 125 int efi_debug = 1; 126 #else 127 int efi_debug = 0; 128 #endif 129 130 #define EFI_FIXES_DB "/usr/share/hwdata/efi.fixes" 131 132 extern unsigned int efi_crc32(const unsigned char *, unsigned int); 133 static int efi_read(int, struct dk_gpt *); 134 135 static int 136 read_disk_info(int fd, diskaddr_t *capacity, uint_t *lbsize) 137 { 138 struct dk_minfo disk_info; 139 140 if ((ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info)) == -1) 141 return (errno); 142 *capacity = disk_info.dki_capacity; 143 *lbsize = disk_info.dki_lbsize; 144 return (0); 145 } 146 147 /* 148 * the number of blocks the EFI label takes up (round up to nearest 149 * block) 150 */ 151 #define NBLOCKS(p, l) (1 + ((((p) * (int)sizeof (efi_gpe_t)) + \ 152 ((l) - 1)) / (l))) 153 /* number of partitions -- limited by what we can malloc */ 154 #define MAX_PARTS ((4294967295UL - sizeof (struct dk_gpt)) / \ 155 sizeof (struct dk_part)) 156 157 /* 158 * The EFI reserved partition size is 8 MiB. This calculates the number of 159 * sectors required to store 8 MiB, taking into account the device's sector 160 * size. 161 */ 162 uint_t 163 efi_reserved_sectors(dk_gpt_t *efi) 164 { 165 /* roundup to sector size */ 166 return ((EFI_MIN_RESV_SIZE * DEV_BSIZE + efi->efi_lbasize - 1) / 167 efi->efi_lbasize); 168 } 169 170 int 171 efi_alloc_and_init(int fd, uint32_t nparts, struct dk_gpt **vtoc) 172 { 173 diskaddr_t capacity; 174 uint_t lbsize; 175 uint_t nblocks; 176 size_t length; 177 struct dk_gpt *vptr; 178 struct uuid uuid; 179 180 if (read_disk_info(fd, &capacity, &lbsize) != 0) { 181 if (efi_debug) 182 (void) fprintf(stderr, 183 "couldn't read disk information\n"); 184 return (-1); 185 } 186 187 nblocks = NBLOCKS(nparts, lbsize); 188 if ((nblocks * lbsize) < EFI_MIN_ARRAY_SIZE + lbsize) { 189 /* 16K plus one block for the GPT */ 190 nblocks = EFI_MIN_ARRAY_SIZE / lbsize + 1; 191 } 192 193 if (nparts > MAX_PARTS) { 194 if (efi_debug) { 195 (void) fprintf(stderr, 196 "the maximum number of partitions supported is %lu\n", 197 MAX_PARTS); 198 } 199 return (-1); 200 } 201 202 length = sizeof (struct dk_gpt) + 203 sizeof (struct dk_part) * (nparts - 1); 204 205 if ((*vtoc = calloc(1, length)) == NULL) 206 return (-1); 207 208 vptr = *vtoc; 209 210 vptr->efi_version = EFI_VERSION_CURRENT; 211 vptr->efi_lbasize = lbsize; 212 vptr->efi_nparts = nparts; 213 /* 214 * add one block here for the PMBR; on disks with a 512 byte 215 * block size and 128 or fewer partitions, efi_first_u_lba 216 * should work out to "34" 217 */ 218 vptr->efi_first_u_lba = nblocks + 1; 219 vptr->efi_last_lba = capacity - 1; 220 vptr->efi_altern_lba = capacity -1; 221 vptr->efi_last_u_lba = vptr->efi_last_lba - nblocks; 222 223 (void) uuid_generate((uchar_t *)&uuid); 224 UUID_LE_CONVERT(vptr->efi_disk_uguid, uuid); 225 return (0); 226 } 227 228 /* 229 * Read EFI - return partition number upon success. 230 */ 231 int 232 efi_alloc_and_read(int fd, struct dk_gpt **vtoc) 233 { 234 int rval; 235 uint32_t nparts; 236 int length; 237 struct mboot *mbr; 238 struct ipart *ipart; 239 diskaddr_t capacity; 240 uint_t lbsize; 241 int i; 242 243 if (read_disk_info(fd, &capacity, &lbsize) != 0) 244 return (VT_ERROR); 245 246 if ((mbr = calloc(1, lbsize)) == NULL) 247 return (VT_ERROR); 248 249 if ((ioctl(fd, DKIOCGMBOOT, (caddr_t)mbr)) == -1) { 250 free(mbr); 251 return (VT_ERROR); 252 } 253 254 if (mbr->signature != MBB_MAGIC) { 255 free(mbr); 256 return (VT_EINVAL); 257 } 258 ipart = (struct ipart *)(uintptr_t)mbr->parts; 259 260 /* Check if we have partition with ID EFI_PMBR */ 261 for (i = 0; i < FD_NUMPART; i++) { 262 if (ipart[i].systid == EFI_PMBR) 263 break; 264 } 265 free(mbr); 266 if (i == FD_NUMPART) 267 return (VT_EINVAL); 268 269 /* figure out the number of entries that would fit into 16K */ 270 nparts = EFI_MIN_ARRAY_SIZE / sizeof (efi_gpe_t); 271 length = (int) sizeof (struct dk_gpt) + 272 (int) sizeof (struct dk_part) * (nparts - 1); 273 if ((*vtoc = calloc(1, length)) == NULL) 274 return (VT_ERROR); 275 276 (*vtoc)->efi_nparts = nparts; 277 rval = efi_read(fd, *vtoc); 278 279 if ((rval == VT_EINVAL) && (*vtoc)->efi_nparts > nparts) { 280 void *tmp; 281 length = (int) sizeof (struct dk_gpt) + 282 (int) sizeof (struct dk_part) * 283 ((*vtoc)->efi_nparts - 1); 284 nparts = (*vtoc)->efi_nparts; 285 if ((tmp = realloc(*vtoc, length)) == NULL) { 286 free (*vtoc); 287 *vtoc = NULL; 288 return (VT_ERROR); 289 } else { 290 *vtoc = tmp; 291 rval = efi_read(fd, *vtoc); 292 } 293 } 294 295 if (rval < 0) { 296 if (efi_debug) { 297 (void) fprintf(stderr, 298 "read of EFI table failed, rval=%d\n", rval); 299 } 300 free (*vtoc); 301 *vtoc = NULL; 302 } 303 304 return (rval); 305 } 306 307 static int 308 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 309 { 310 void *data = dk_ioc->dki_data; 311 int error; 312 313 dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data; 314 error = ioctl(fd, cmd, (void *)dk_ioc); 315 dk_ioc->dki_data = data; 316 317 return (error); 318 } 319 320 static int 321 check_label(int fd, dk_efi_t *dk_ioc) 322 { 323 efi_gpt_t *efi; 324 uint_t crc; 325 326 if (efi_ioctl(fd, DKIOCGETEFI, dk_ioc) == -1) { 327 switch (errno) { 328 case EIO: 329 return (VT_EIO); 330 default: 331 return (VT_ERROR); 332 } 333 } 334 efi = dk_ioc->dki_data; 335 if (efi->efi_gpt_Signature != LE_64(EFI_SIGNATURE)) { 336 if (efi_debug) 337 (void) fprintf(stderr, 338 "Bad EFI signature: 0x%llx != 0x%llx\n", 339 (long long)efi->efi_gpt_Signature, 340 (long long)LE_64(EFI_SIGNATURE)); 341 return (VT_EINVAL); 342 } 343 344 /* 345 * check CRC of the header; the size of the header should 346 * never be larger than one block 347 */ 348 crc = efi->efi_gpt_HeaderCRC32; 349 efi->efi_gpt_HeaderCRC32 = 0; 350 351 if (((len_t)LE_32(efi->efi_gpt_HeaderSize) > dk_ioc->dki_length) || 352 crc != LE_32(efi_crc32((unsigned char *)efi, 353 LE_32(efi->efi_gpt_HeaderSize)))) { 354 if (efi_debug) 355 (void) fprintf(stderr, 356 "Bad EFI CRC: 0x%x != 0x%x\n", 357 crc, LE_32(efi_crc32((unsigned char *)efi, 358 LE_32(efi->efi_gpt_HeaderSize)))); 359 return (VT_EINVAL); 360 } 361 362 return (0); 363 } 364 365 static int 366 efi_read(int fd, struct dk_gpt *vtoc) 367 { 368 int i, j; 369 int label_len; 370 int rval = 0; 371 int vdc_flag = 0; 372 struct dk_minfo disk_info; 373 dk_efi_t dk_ioc; 374 efi_gpt_t *efi; 375 efi_gpe_t *efi_parts; 376 struct dk_cinfo dki_info; 377 uint32_t user_length; 378 boolean_t legacy_label = B_FALSE; 379 380 /* 381 * get the partition number for this file descriptor. 382 */ 383 if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) { 384 if (efi_debug) { 385 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno); 386 } 387 switch (errno) { 388 case EIO: 389 return (VT_EIO); 390 case EINVAL: 391 return (VT_EINVAL); 392 default: 393 return (VT_ERROR); 394 } 395 } 396 397 if ((strncmp(dki_info.dki_cname, "vdc", 4) == 0) && 398 (strncmp(dki_info.dki_dname, "vdc", 4) == 0)) { 399 /* 400 * The controller and drive name "vdc" (virtual disk client) 401 * indicates a LDoms virtual disk. 402 */ 403 vdc_flag++; 404 } 405 406 /* get the LBA size */ 407 if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) { 408 if (efi_debug) { 409 (void) fprintf(stderr, 410 "assuming LBA 512 bytes %d\n", 411 errno); 412 } 413 disk_info.dki_lbsize = DEV_BSIZE; 414 } 415 if (disk_info.dki_lbsize == 0) { 416 if (efi_debug) { 417 (void) fprintf(stderr, 418 "efi_read: assuming LBA 512 bytes\n"); 419 } 420 disk_info.dki_lbsize = DEV_BSIZE; 421 } 422 /* 423 * Read the EFI GPT to figure out how many partitions we need 424 * to deal with. 425 */ 426 dk_ioc.dki_lba = 1; 427 if (NBLOCKS(vtoc->efi_nparts, disk_info.dki_lbsize) < 34) { 428 label_len = EFI_MIN_ARRAY_SIZE + disk_info.dki_lbsize; 429 } else { 430 label_len = vtoc->efi_nparts * (int) sizeof (efi_gpe_t) + 431 disk_info.dki_lbsize; 432 if (label_len % disk_info.dki_lbsize) { 433 /* pad to physical sector size */ 434 label_len += disk_info.dki_lbsize; 435 label_len &= ~(disk_info.dki_lbsize - 1); 436 } 437 } 438 439 if ((dk_ioc.dki_data = calloc(1, label_len)) == NULL) 440 return (VT_ERROR); 441 442 dk_ioc.dki_length = disk_info.dki_lbsize; 443 user_length = vtoc->efi_nparts; 444 efi = dk_ioc.dki_data; 445 if ((rval = check_label(fd, &dk_ioc)) == VT_EINVAL) { 446 /* 447 * No valid label here; try the alternate. Note that here 448 * we just read GPT header and save it into dk_ioc.data, 449 * Later, we will read GUID partition entry array if we 450 * can get valid GPT header. 451 */ 452 453 /* 454 * This is a workaround for legacy systems. In the past, the 455 * last sector of SCSI disk was invisible on x86 platform. At 456 * that time, backup label was saved on the next to the last 457 * sector. It is possible for users to move a disk from previous 458 * solaris system to present system. Here, we attempt to search 459 * legacy backup EFI label first. 460 */ 461 dk_ioc.dki_lba = disk_info.dki_capacity - 2; 462 dk_ioc.dki_length = disk_info.dki_lbsize; 463 rval = check_label(fd, &dk_ioc); 464 if (rval == VT_EINVAL) { 465 /* 466 * we didn't find legacy backup EFI label, try to 467 * search backup EFI label in the last block. 468 */ 469 dk_ioc.dki_lba = disk_info.dki_capacity - 1; 470 dk_ioc.dki_length = disk_info.dki_lbsize; 471 rval = check_label(fd, &dk_ioc); 472 if (rval == 0) { 473 legacy_label = B_TRUE; 474 if (efi_debug) 475 (void) fprintf(stderr, 476 "efi_read: primary label corrupt; " 477 "using EFI backup label located on" 478 " the last block\n"); 479 } 480 } else { 481 if ((efi_debug) && (rval == 0)) 482 (void) fprintf(stderr, "efi_read: primary label" 483 " corrupt; using legacy EFI backup label " 484 " located on the next to last block\n"); 485 } 486 487 if (rval == 0) { 488 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA); 489 vtoc->efi_flags |= EFI_GPT_PRIMARY_CORRUPT; 490 vtoc->efi_nparts = 491 LE_32(efi->efi_gpt_NumberOfPartitionEntries); 492 /* 493 * Partition tables are between backup GPT header 494 * table and ParitionEntryLBA (the starting LBA of 495 * the GUID partition entries array). Now that we 496 * already got valid GPT header and saved it in 497 * dk_ioc.dki_data, we try to get GUID partition 498 * entry array here. 499 */ 500 /* LINTED */ 501 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data 502 + disk_info.dki_lbsize); 503 if (legacy_label) 504 dk_ioc.dki_length = disk_info.dki_capacity - 1 - 505 dk_ioc.dki_lba; 506 else 507 dk_ioc.dki_length = disk_info.dki_capacity - 2 - 508 dk_ioc.dki_lba; 509 dk_ioc.dki_length *= disk_info.dki_lbsize; 510 if (dk_ioc.dki_length > 511 ((len_t)label_len - sizeof (*dk_ioc.dki_data))) { 512 rval = VT_EINVAL; 513 } else { 514 /* 515 * read GUID partition entry array 516 */ 517 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc); 518 } 519 } 520 521 } else if (rval == 0) { 522 523 dk_ioc.dki_lba = LE_64(efi->efi_gpt_PartitionEntryLBA); 524 /* LINTED */ 525 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data 526 + disk_info.dki_lbsize); 527 dk_ioc.dki_length = label_len - disk_info.dki_lbsize; 528 rval = efi_ioctl(fd, DKIOCGETEFI, &dk_ioc); 529 530 } else if (vdc_flag && rval == VT_ERROR && errno == EINVAL) { 531 /* 532 * When the device is a LDoms virtual disk, the DKIOCGETEFI 533 * ioctl can fail with EINVAL if the virtual disk backend 534 * is a ZFS volume serviced by a domain running an old version 535 * of Solaris. This is because the DKIOCGETEFI ioctl was 536 * initially incorrectly implemented for a ZFS volume and it 537 * expected the GPT and GPE to be retrieved with a single ioctl. 538 * So we try to read the GPT and the GPE using that old style 539 * ioctl. 540 */ 541 dk_ioc.dki_lba = 1; 542 dk_ioc.dki_length = label_len; 543 rval = check_label(fd, &dk_ioc); 544 } 545 546 if (rval < 0) { 547 free(efi); 548 return (rval); 549 } 550 551 /* LINTED -- always longlong aligned */ 552 efi_parts = (efi_gpe_t *)(((char *)efi) + disk_info.dki_lbsize); 553 554 /* 555 * Assemble this into a "dk_gpt" struct for easier 556 * digestibility by applications. 557 */ 558 vtoc->efi_version = LE_32(efi->efi_gpt_Revision); 559 vtoc->efi_nparts = LE_32(efi->efi_gpt_NumberOfPartitionEntries); 560 vtoc->efi_part_size = LE_32(efi->efi_gpt_SizeOfPartitionEntry); 561 vtoc->efi_lbasize = disk_info.dki_lbsize; 562 vtoc->efi_last_lba = disk_info.dki_capacity - 1; 563 vtoc->efi_first_u_lba = LE_64(efi->efi_gpt_FirstUsableLBA); 564 vtoc->efi_last_u_lba = LE_64(efi->efi_gpt_LastUsableLBA); 565 vtoc->efi_altern_lba = LE_64(efi->efi_gpt_AlternateLBA); 566 UUID_LE_CONVERT(vtoc->efi_disk_uguid, efi->efi_gpt_DiskGUID); 567 568 /* 569 * If the array the user passed in is too small, set the length 570 * to what it needs to be and return 571 */ 572 if (user_length < vtoc->efi_nparts) { 573 return (VT_EINVAL); 574 } 575 576 for (i = 0; i < vtoc->efi_nparts; i++) { 577 578 UUID_LE_CONVERT(vtoc->efi_parts[i].p_guid, 579 efi_parts[i].efi_gpe_PartitionTypeGUID); 580 581 for (j = 0; 582 j < sizeof (conversion_array) 583 / sizeof (struct uuid_to_ptag); j++) { 584 585 if (bcmp(&vtoc->efi_parts[i].p_guid, 586 &conversion_array[j].uuid, 587 sizeof (struct uuid)) == 0) { 588 vtoc->efi_parts[i].p_tag = 589 conversion_array[j].p_tag; 590 break; 591 } 592 } 593 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) 594 continue; 595 vtoc->efi_parts[i].p_flag = 596 LE_16(efi_parts[i].efi_gpe_Attributes.PartitionAttrs); 597 vtoc->efi_parts[i].p_start = 598 LE_64(efi_parts[i].efi_gpe_StartingLBA); 599 vtoc->efi_parts[i].p_size = 600 LE_64(efi_parts[i].efi_gpe_EndingLBA) - 601 vtoc->efi_parts[i].p_start + 1; 602 for (j = 0; j < EFI_PART_NAME_LEN; j++) { 603 vtoc->efi_parts[i].p_name[j] = 604 (uchar_t)LE_16( 605 efi_parts[i].efi_gpe_PartitionName[j]); 606 } 607 608 UUID_LE_CONVERT(vtoc->efi_parts[i].p_uguid, 609 efi_parts[i].efi_gpe_UniquePartitionGUID); 610 } 611 free(efi); 612 613 return (dki_info.dki_partition); 614 } 615 616 static void 617 hardware_workarounds(int *slot, int *active) 618 { 619 smbios_struct_t s_sys, s_mb; 620 smbios_info_t sys, mb; 621 smbios_hdl_t *shp; 622 char buf[0x400]; 623 FILE *fp; 624 int err; 625 626 if ((fp = fopen(EFI_FIXES_DB, "rF")) == NULL) 627 return; 628 629 if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL) { 630 if (efi_debug) 631 (void) fprintf(stderr, 632 "libefi failed to load SMBIOS: %s\n", 633 smbios_errmsg(err)); 634 (void) fclose(fp); 635 return; 636 } 637 638 if (smbios_lookup_type(shp, SMB_TYPE_SYSTEM, &s_sys) == SMB_ERR || 639 smbios_info_common(shp, s_sys.smbstr_id, &sys) == SMB_ERR) 640 (void) memset(&sys, '\0', sizeof (sys)); 641 if (smbios_lookup_type(shp, SMB_TYPE_BASEBOARD, &s_mb) == SMB_ERR || 642 smbios_info_common(shp, s_mb.smbstr_id, &mb) == SMB_ERR) 643 (void) memset(&mb, '\0', sizeof (mb)); 644 645 while (fgets(buf, sizeof (buf), fp) != NULL) { 646 char *tok, *val, *end; 647 648 tok = buf + strspn(buf, " \t"); 649 if (*tok == '#') 650 continue; 651 while (*tok != '\0') { 652 tok += strspn(tok, " \t"); 653 if ((val = strchr(tok, '=')) == NULL) 654 break; 655 *val++ = '\0'; 656 if (*val == '"') 657 end = strchr(++val, '"'); 658 else 659 end = strpbrk(val, " \t\n"); 660 if (end == NULL) 661 break; 662 *end++ = '\0'; 663 664 if (strcmp(tok, "sys.manufacturer") == 0 && 665 (sys.smbi_manufacturer == NULL || 666 strcasecmp(val, sys.smbi_manufacturer))) 667 break; 668 if (strcmp(tok, "sys.product") == 0 && 669 (sys.smbi_product == NULL || 670 strcasecmp(val, sys.smbi_product))) 671 break; 672 if (strcmp(tok, "sys.version") == 0 && 673 (sys.smbi_version == NULL || 674 strcasecmp(val, sys.smbi_version))) 675 break; 676 if (strcmp(tok, "mb.manufacturer") == 0 && 677 (mb.smbi_manufacturer == NULL || 678 strcasecmp(val, mb.smbi_manufacturer))) 679 break; 680 if (strcmp(tok, "mb.product") == 0 && 681 (mb.smbi_product == NULL || 682 strcasecmp(val, mb.smbi_product))) 683 break; 684 if (strcmp(tok, "mb.version") == 0 && 685 (mb.smbi_version == NULL || 686 strcasecmp(val, mb.smbi_version))) 687 break; 688 689 if (strcmp(tok, "pmbr_slot") == 0) { 690 *slot = atoi(val); 691 if (*slot < 0 || *slot > 3) 692 *slot = 0; 693 if (efi_debug) 694 (void) fprintf(stderr, 695 "Using slot %d\n", *slot); 696 } 697 698 if (strcmp(tok, "pmbr_active") == 0) { 699 *active = atoi(val); 700 if (*active < 0 || *active > 1) 701 *active = 0; 702 if (efi_debug) 703 (void) fprintf(stderr, 704 "Using active %d\n", *active); 705 } 706 707 tok = end; 708 } 709 } 710 (void) fclose(fp); 711 smbios_close(shp); 712 } 713 714 /* writes a "protective" MBR */ 715 static int 716 write_pmbr(int fd, struct dk_gpt *vtoc) 717 { 718 dk_efi_t dk_ioc; 719 struct mboot mb; 720 uchar_t *cp; 721 diskaddr_t size_in_lba; 722 uchar_t *buf; 723 int len, slot, active; 724 725 slot = active = 0; 726 727 hardware_workarounds(&slot, &active); 728 729 len = (vtoc->efi_lbasize == 0) ? sizeof (mb) : vtoc->efi_lbasize; 730 buf = calloc(1, len); 731 732 /* 733 * Preserve any boot code and disk signature if the first block is 734 * already an MBR. 735 */ 736 dk_ioc.dki_lba = 0; 737 dk_ioc.dki_length = len; 738 /* LINTED -- always longlong aligned */ 739 dk_ioc.dki_data = (efi_gpt_t *)buf; 740 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) == -1) { 741 (void) memcpy(&mb, buf, sizeof (mb)); 742 bzero(&mb, sizeof (mb)); 743 mb.signature = LE_16(MBB_MAGIC); 744 } else { 745 (void) memcpy(&mb, buf, sizeof (mb)); 746 if (mb.signature != LE_16(MBB_MAGIC)) { 747 bzero(&mb, sizeof (mb)); 748 mb.signature = LE_16(MBB_MAGIC); 749 } 750 } 751 752 bzero(&mb.parts, sizeof (mb.parts)); 753 cp = (uchar_t *)&mb.parts[slot * sizeof (struct ipart)]; 754 /* bootable or not */ 755 *cp++ = active ? ACTIVE : NOTACTIVE; 756 /* beginning CHS; same as starting LBA (but one-based) */ 757 *cp++ = 0x0; 758 *cp++ = 0x2; 759 *cp++ = 0x0; 760 /* OS type */ 761 *cp++ = EFI_PMBR; 762 /* ending CHS; 0xffffff if not representable */ 763 *cp++ = 0xff; 764 *cp++ = 0xff; 765 *cp++ = 0xff; 766 /* starting LBA: 1 (little endian format) by EFI definition */ 767 *cp++ = 0x01; 768 *cp++ = 0x00; 769 *cp++ = 0x00; 770 *cp++ = 0x00; 771 /* ending LBA: last block on the disk (little endian format) */ 772 size_in_lba = vtoc->efi_last_lba; 773 if (size_in_lba < 0xffffffff) { 774 *cp++ = (size_in_lba & 0x000000ff); 775 *cp++ = (size_in_lba & 0x0000ff00) >> 8; 776 *cp++ = (size_in_lba & 0x00ff0000) >> 16; 777 *cp++ = (size_in_lba & 0xff000000) >> 24; 778 } else { 779 *cp++ = 0xff; 780 *cp++ = 0xff; 781 *cp++ = 0xff; 782 *cp++ = 0xff; 783 } 784 785 (void) memcpy(buf, &mb, sizeof (mb)); 786 /* LINTED -- always longlong aligned */ 787 dk_ioc.dki_data = (efi_gpt_t *)buf; 788 dk_ioc.dki_lba = 0; 789 dk_ioc.dki_length = len; 790 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) { 791 free(buf); 792 switch (errno) { 793 case EIO: 794 return (VT_EIO); 795 case EINVAL: 796 return (VT_EINVAL); 797 default: 798 return (VT_ERROR); 799 } 800 } 801 free(buf); 802 return (0); 803 } 804 805 /* make sure the user specified something reasonable */ 806 static int 807 check_input(struct dk_gpt *vtoc) 808 { 809 int resv_part = -1; 810 int i, j; 811 diskaddr_t istart, jstart, isize, jsize, endsect; 812 813 /* 814 * Sanity-check the input (make sure no partitions overlap) 815 */ 816 for (i = 0; i < vtoc->efi_nparts; i++) { 817 /* It can't be unassigned and have an actual size */ 818 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) && 819 (vtoc->efi_parts[i].p_size != 0)) { 820 if (efi_debug) { 821 (void) fprintf(stderr, 822 "partition %d is \"unassigned\" but has a size of %llu", 823 i, 824 vtoc->efi_parts[i].p_size); 825 } 826 return (VT_EINVAL); 827 } 828 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) { 829 if (uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_guid)) 830 continue; 831 /* we have encountered an unknown uuid */ 832 vtoc->efi_parts[i].p_tag = 0xff; 833 } 834 if (vtoc->efi_parts[i].p_tag == V_RESERVED) { 835 if (resv_part != -1) { 836 if (efi_debug) { 837 (void) fprintf(stderr, 838 "found duplicate reserved partition at %d\n", 839 i); 840 } 841 return (VT_EINVAL); 842 } 843 resv_part = i; 844 } 845 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) || 846 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) { 847 if (efi_debug) { 848 (void) fprintf(stderr, 849 "Partition %d starts at %llu. ", 850 i, 851 vtoc->efi_parts[i].p_start); 852 (void) fprintf(stderr, 853 "It must be between %llu and %llu.\n", 854 vtoc->efi_first_u_lba, 855 vtoc->efi_last_u_lba); 856 } 857 return (VT_EINVAL); 858 } 859 if ((vtoc->efi_parts[i].p_start + 860 vtoc->efi_parts[i].p_size < 861 vtoc->efi_first_u_lba) || 862 (vtoc->efi_parts[i].p_start + 863 vtoc->efi_parts[i].p_size > 864 vtoc->efi_last_u_lba + 1)) { 865 if (efi_debug) { 866 (void) fprintf(stderr, 867 "Partition %d ends at %llu. ", 868 i, 869 vtoc->efi_parts[i].p_start + 870 vtoc->efi_parts[i].p_size); 871 (void) fprintf(stderr, 872 "It must be between %llu and %llu.\n", 873 vtoc->efi_first_u_lba, 874 vtoc->efi_last_u_lba); 875 } 876 return (VT_EINVAL); 877 } 878 879 for (j = 0; j < vtoc->efi_nparts; j++) { 880 isize = vtoc->efi_parts[i].p_size; 881 jsize = vtoc->efi_parts[j].p_size; 882 istart = vtoc->efi_parts[i].p_start; 883 jstart = vtoc->efi_parts[j].p_start; 884 if ((i != j) && (isize != 0) && (jsize != 0)) { 885 endsect = jstart + jsize -1; 886 if ((jstart <= istart) && 887 (istart <= endsect)) { 888 if (efi_debug) { 889 (void) fprintf(stderr, 890 "Partition %d overlaps partition %d.", 891 i, j); 892 } 893 return (VT_EINVAL); 894 } 895 } 896 } 897 } 898 /* just a warning for now */ 899 if ((resv_part == -1) && efi_debug) { 900 (void) fprintf(stderr, 901 "no reserved partition found\n"); 902 } 903 return (0); 904 } 905 906 /* 907 * add all the unallocated space to the current label 908 */ 909 int 910 efi_use_whole_disk(int fd) 911 { 912 struct dk_gpt *efi_label; 913 int rval; 914 int i; 915 uint_t phy_last_slice = 0; 916 diskaddr_t pl_start = 0; 917 diskaddr_t pl_size; 918 919 rval = efi_alloc_and_read(fd, &efi_label); 920 if (rval < 0) { 921 return (rval); 922 } 923 924 /* find the last physically non-zero partition */ 925 for (i = 0; i < efi_label->efi_nparts - 2; i ++) { 926 if (pl_start < efi_label->efi_parts[i].p_start) { 927 pl_start = efi_label->efi_parts[i].p_start; 928 phy_last_slice = i; 929 } 930 } 931 pl_size = efi_label->efi_parts[phy_last_slice].p_size; 932 933 /* 934 * If alter_lba is 1, we are using the backup label. 935 * Since we can locate the backup label by disk capacity, 936 * there must be no unallocated space. 937 */ 938 if ((efi_label->efi_altern_lba == 1) || (efi_label->efi_altern_lba 939 >= efi_label->efi_last_lba)) { 940 if (efi_debug) { 941 (void) fprintf(stderr, 942 "efi_use_whole_disk: requested space not found\n"); 943 } 944 efi_free(efi_label); 945 return (VT_ENOSPC); 946 } 947 948 /* 949 * If there is space between the last physically non-zero partition 950 * and the reserved partition, just add the unallocated space to this 951 * area. Otherwise, the unallocated space is added to the last 952 * physically non-zero partition. 953 */ 954 if (pl_start + pl_size - 1 == efi_label->efi_last_u_lba - 955 efi_reserved_sectors(efi_label)) { 956 efi_label->efi_parts[phy_last_slice].p_size += 957 efi_label->efi_last_lba - efi_label->efi_altern_lba; 958 } 959 960 /* 961 * Move the reserved partition. There is currently no data in 962 * here except fabricated devids (which get generated via 963 * efi_write()). So there is no need to copy data. 964 */ 965 efi_label->efi_parts[efi_label->efi_nparts - 1].p_start += 966 efi_label->efi_last_lba - efi_label->efi_altern_lba; 967 efi_label->efi_last_u_lba += efi_label->efi_last_lba 968 - efi_label->efi_altern_lba; 969 970 rval = efi_write(fd, efi_label); 971 if (rval < 0) { 972 if (efi_debug) { 973 (void) fprintf(stderr, 974 "efi_use_whole_disk:fail to write label, rval=%d\n", 975 rval); 976 } 977 efi_free(efi_label); 978 return (rval); 979 } 980 981 efi_free(efi_label); 982 return (0); 983 } 984 985 986 /* 987 * write EFI label and backup label 988 */ 989 int 990 efi_write(int fd, struct dk_gpt *vtoc) 991 { 992 dk_efi_t dk_ioc; 993 efi_gpt_t *efi; 994 efi_gpe_t *efi_parts; 995 int i, j; 996 struct dk_cinfo dki_info; 997 int nblocks; 998 diskaddr_t lba_backup_gpt_hdr; 999 1000 if (ioctl(fd, DKIOCINFO, (caddr_t)&dki_info) == -1) { 1001 if (efi_debug) 1002 (void) fprintf(stderr, "DKIOCINFO errno 0x%x\n", errno); 1003 switch (errno) { 1004 case EIO: 1005 return (VT_EIO); 1006 case EINVAL: 1007 return (VT_EINVAL); 1008 default: 1009 return (VT_ERROR); 1010 } 1011 } 1012 1013 if (check_input(vtoc)) 1014 return (VT_EINVAL); 1015 1016 dk_ioc.dki_lba = 1; 1017 if (NBLOCKS(vtoc->efi_nparts, vtoc->efi_lbasize) < 34) { 1018 dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + vtoc->efi_lbasize; 1019 } else { 1020 dk_ioc.dki_length = NBLOCKS(vtoc->efi_nparts, 1021 vtoc->efi_lbasize) * 1022 vtoc->efi_lbasize; 1023 } 1024 1025 /* 1026 * the number of blocks occupied by GUID partition entry array 1027 */ 1028 nblocks = dk_ioc.dki_length / vtoc->efi_lbasize - 1; 1029 1030 /* 1031 * Backup GPT header is located on the block after GUID 1032 * partition entry array. Here, we calculate the address 1033 * for backup GPT header. 1034 */ 1035 lba_backup_gpt_hdr = vtoc->efi_last_u_lba + 1 + nblocks; 1036 if ((dk_ioc.dki_data = calloc(1, dk_ioc.dki_length)) == NULL) 1037 return (VT_ERROR); 1038 1039 efi = dk_ioc.dki_data; 1040 1041 /* stuff user's input into EFI struct */ 1042 efi->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 1043 efi->efi_gpt_Revision = LE_32(vtoc->efi_version); /* 0x02000100 */ 1044 efi->efi_gpt_HeaderSize = LE_32(EFI_HEADER_SIZE); 1045 efi->efi_gpt_Reserved1 = 0; 1046 efi->efi_gpt_MyLBA = LE_64(1ULL); 1047 efi->efi_gpt_AlternateLBA = LE_64(lba_backup_gpt_hdr); 1048 efi->efi_gpt_FirstUsableLBA = LE_64(vtoc->efi_first_u_lba); 1049 efi->efi_gpt_LastUsableLBA = LE_64(vtoc->efi_last_u_lba); 1050 efi->efi_gpt_PartitionEntryLBA = LE_64(2ULL); 1051 efi->efi_gpt_NumberOfPartitionEntries = LE_32(vtoc->efi_nparts); 1052 efi->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (struct efi_gpe)); 1053 UUID_LE_CONVERT(efi->efi_gpt_DiskGUID, vtoc->efi_disk_uguid); 1054 1055 /* LINTED -- always longlong aligned */ 1056 efi_parts = (efi_gpe_t *)((char *)dk_ioc.dki_data + vtoc->efi_lbasize); 1057 1058 for (i = 0; i < vtoc->efi_nparts; i++) { 1059 for (j = 0; 1060 j < sizeof (conversion_array) / 1061 sizeof (struct uuid_to_ptag); j++) { 1062 1063 if (vtoc->efi_parts[i].p_tag == 1064 conversion_array[j].p_tag) { 1065 UUID_LE_CONVERT( 1066 efi_parts[i].efi_gpe_PartitionTypeGUID, 1067 conversion_array[j].uuid); 1068 break; 1069 } 1070 } 1071 1072 if (j == sizeof (conversion_array) / 1073 sizeof (struct uuid_to_ptag)) { 1074 /* 1075 * If we didn't have a matching uuid match, bail here. 1076 * Don't write a label with unknown uuid. 1077 */ 1078 if (efi_debug) { 1079 (void) fprintf(stderr, 1080 "Unknown uuid for p_tag %d\n", 1081 vtoc->efi_parts[i].p_tag); 1082 } 1083 return (VT_EINVAL); 1084 } 1085 1086 efi_parts[i].efi_gpe_StartingLBA = 1087 LE_64(vtoc->efi_parts[i].p_start); 1088 efi_parts[i].efi_gpe_EndingLBA = 1089 LE_64(vtoc->efi_parts[i].p_start + 1090 vtoc->efi_parts[i].p_size - 1); 1091 efi_parts[i].efi_gpe_Attributes.PartitionAttrs = 1092 LE_16(vtoc->efi_parts[i].p_flag); 1093 for (j = 0; j < EFI_PART_NAME_LEN; j++) { 1094 efi_parts[i].efi_gpe_PartitionName[j] = 1095 LE_16((ushort_t)vtoc->efi_parts[i].p_name[j]); 1096 } 1097 if ((vtoc->efi_parts[i].p_tag != V_UNASSIGNED) && 1098 uuid_is_null((uchar_t *)&vtoc->efi_parts[i].p_uguid)) { 1099 (void) uuid_generate((uchar_t *) 1100 &vtoc->efi_parts[i].p_uguid); 1101 } 1102 bcopy(&vtoc->efi_parts[i].p_uguid, 1103 &efi_parts[i].efi_gpe_UniquePartitionGUID, 1104 sizeof (uuid_t)); 1105 } 1106 efi->efi_gpt_PartitionEntryArrayCRC32 = 1107 LE_32(efi_crc32((unsigned char *)efi_parts, 1108 vtoc->efi_nparts * (int)sizeof (struct efi_gpe))); 1109 efi->efi_gpt_HeaderCRC32 = LE_32(efi_crc32((unsigned char *)efi, 1110 EFI_HEADER_SIZE)); 1111 1112 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) { 1113 free(dk_ioc.dki_data); 1114 switch (errno) { 1115 case EIO: 1116 return (VT_EIO); 1117 case EINVAL: 1118 return (VT_EINVAL); 1119 default: 1120 return (VT_ERROR); 1121 } 1122 } 1123 1124 /* write backup partition array */ 1125 dk_ioc.dki_lba = vtoc->efi_last_u_lba + 1; 1126 dk_ioc.dki_length -= vtoc->efi_lbasize; 1127 /* LINTED */ 1128 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data + 1129 vtoc->efi_lbasize); 1130 1131 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) { 1132 /* 1133 * we wrote the primary label okay, so don't fail 1134 */ 1135 if (efi_debug) { 1136 (void) fprintf(stderr, 1137 "write of backup partitions to block %llu " 1138 "failed, errno %d\n", 1139 vtoc->efi_last_u_lba + 1, 1140 errno); 1141 } 1142 } 1143 /* 1144 * now swap MyLBA and AlternateLBA fields and write backup 1145 * partition table header 1146 */ 1147 dk_ioc.dki_lba = lba_backup_gpt_hdr; 1148 dk_ioc.dki_length = vtoc->efi_lbasize; 1149 /* LINTED */ 1150 dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data - 1151 vtoc->efi_lbasize); 1152 efi->efi_gpt_AlternateLBA = LE_64(1ULL); 1153 efi->efi_gpt_MyLBA = LE_64(lba_backup_gpt_hdr); 1154 efi->efi_gpt_PartitionEntryLBA = LE_64(vtoc->efi_last_u_lba + 1); 1155 efi->efi_gpt_HeaderCRC32 = 0; 1156 efi->efi_gpt_HeaderCRC32 = 1157 LE_32(efi_crc32((unsigned char *)dk_ioc.dki_data, EFI_HEADER_SIZE)); 1158 1159 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) { 1160 if (efi_debug) { 1161 (void) fprintf(stderr, 1162 "write of backup header to block %llu failed, " 1163 "errno %d\n", 1164 lba_backup_gpt_hdr, 1165 errno); 1166 } 1167 } 1168 /* write the PMBR */ 1169 (void) write_pmbr(fd, vtoc); 1170 free(dk_ioc.dki_data); 1171 return (0); 1172 } 1173 1174 void 1175 efi_free(struct dk_gpt *ptr) 1176 { 1177 free(ptr); 1178 } 1179 1180 /* 1181 * Input: File descriptor 1182 * Output: 1 if disk has an EFI label, or > 2TB with no VTOC or legacy MBR. 1183 * Otherwise 0. 1184 */ 1185 int 1186 efi_type(int fd) 1187 { 1188 struct vtoc vtoc; 1189 struct extvtoc extvtoc; 1190 1191 if (ioctl(fd, DKIOCGEXTVTOC, &extvtoc) == -1) { 1192 if (errno == ENOTSUP) 1193 return (1); 1194 else if (errno == ENOTTY) { 1195 if (ioctl(fd, DKIOCGVTOC, &vtoc) == -1) 1196 if (errno == ENOTSUP) 1197 return (1); 1198 } 1199 } 1200 return (0); 1201 } 1202 1203 void 1204 efi_err_check(struct dk_gpt *vtoc) 1205 { 1206 int resv_part = -1; 1207 int i, j; 1208 diskaddr_t istart, jstart, isize, jsize, endsect; 1209 int overlap = 0; 1210 uint_t reserved; 1211 1212 /* 1213 * make sure no partitions overlap 1214 */ 1215 reserved = efi_reserved_sectors(vtoc); 1216 for (i = 0; i < vtoc->efi_nparts; i++) { 1217 /* It can't be unassigned and have an actual size */ 1218 if ((vtoc->efi_parts[i].p_tag == V_UNASSIGNED) && 1219 (vtoc->efi_parts[i].p_size != 0)) { 1220 (void) fprintf(stderr, 1221 "partition %d is \"unassigned\" but has a size " 1222 "of %llu\n", i, vtoc->efi_parts[i].p_size); 1223 } 1224 if (vtoc->efi_parts[i].p_tag == V_UNASSIGNED) { 1225 continue; 1226 } 1227 if (vtoc->efi_parts[i].p_tag == V_RESERVED) { 1228 if (resv_part != -1) { 1229 (void) fprintf(stderr, 1230 "found duplicate reserved partition at " 1231 "%d\n", i); 1232 } 1233 resv_part = i; 1234 if (vtoc->efi_parts[i].p_size != reserved) 1235 (void) fprintf(stderr, 1236 "Warning: reserved partition size must " 1237 "be %u sectors\n", reserved); 1238 } 1239 if ((vtoc->efi_parts[i].p_start < vtoc->efi_first_u_lba) || 1240 (vtoc->efi_parts[i].p_start > vtoc->efi_last_u_lba)) { 1241 (void) fprintf(stderr, 1242 "Partition %d starts at %llu\n", 1243 i, 1244 vtoc->efi_parts[i].p_start); 1245 (void) fprintf(stderr, 1246 "It must be between %llu and %llu.\n", 1247 vtoc->efi_first_u_lba, 1248 vtoc->efi_last_u_lba); 1249 } 1250 if ((vtoc->efi_parts[i].p_start + 1251 vtoc->efi_parts[i].p_size < 1252 vtoc->efi_first_u_lba) || 1253 (vtoc->efi_parts[i].p_start + 1254 vtoc->efi_parts[i].p_size > 1255 vtoc->efi_last_u_lba + 1)) { 1256 (void) fprintf(stderr, 1257 "Partition %d ends at %llu\n", 1258 i, 1259 vtoc->efi_parts[i].p_start + 1260 vtoc->efi_parts[i].p_size); 1261 (void) fprintf(stderr, 1262 "It must be between %llu and %llu.\n", 1263 vtoc->efi_first_u_lba, 1264 vtoc->efi_last_u_lba); 1265 } 1266 1267 for (j = 0; j < vtoc->efi_nparts; j++) { 1268 isize = vtoc->efi_parts[i].p_size; 1269 jsize = vtoc->efi_parts[j].p_size; 1270 istart = vtoc->efi_parts[i].p_start; 1271 jstart = vtoc->efi_parts[j].p_start; 1272 if ((i != j) && (isize != 0) && (jsize != 0)) { 1273 endsect = jstart + jsize -1; 1274 if ((jstart <= istart) && 1275 (istart <= endsect)) { 1276 if (!overlap) { 1277 (void) fprintf(stderr, 1278 "label error: EFI Labels do not " 1279 "support overlapping partitions\n"); 1280 } 1281 (void) fprintf(stderr, 1282 "Partition %d overlaps partition " 1283 "%d.\n", i, j); 1284 overlap = 1; 1285 } 1286 } 1287 } 1288 } 1289 /* make sure there is a reserved partition */ 1290 if (resv_part == -1) { 1291 (void) fprintf(stderr, 1292 "no reserved partition found\n"); 1293 } 1294 } 1295 1296 /* 1297 * We need to get information necessary to construct a *new* efi 1298 * label type 1299 */ 1300 int 1301 efi_auto_sense(int fd, struct dk_gpt **vtoc) 1302 { 1303 1304 int i; 1305 1306 /* 1307 * Now build the default partition table 1308 */ 1309 if (efi_alloc_and_init(fd, EFI_NUMPAR, vtoc) != 0) { 1310 if (efi_debug) { 1311 (void) fprintf(stderr, "efi_alloc_and_init failed.\n"); 1312 } 1313 return (-1); 1314 } 1315 1316 for (i = 0; i < min((*vtoc)->efi_nparts, V_NUMPAR); i++) { 1317 (*vtoc)->efi_parts[i].p_tag = default_vtoc_map[i].p_tag; 1318 (*vtoc)->efi_parts[i].p_flag = default_vtoc_map[i].p_flag; 1319 (*vtoc)->efi_parts[i].p_start = 0; 1320 (*vtoc)->efi_parts[i].p_size = 0; 1321 } 1322 /* 1323 * Make constants first 1324 * and variable partitions later 1325 */ 1326 1327 /* root partition - s0 128 MB */ 1328 (*vtoc)->efi_parts[0].p_start = 34; 1329 (*vtoc)->efi_parts[0].p_size = 262144; 1330 1331 /* partition - s1 128 MB */ 1332 (*vtoc)->efi_parts[1].p_start = 262178; 1333 (*vtoc)->efi_parts[1].p_size = 262144; 1334 1335 /* partition -s2 is NOT the Backup disk */ 1336 (*vtoc)->efi_parts[2].p_tag = V_UNASSIGNED; 1337 1338 /* partition -s6 /usr partition - HOG */ 1339 (*vtoc)->efi_parts[6].p_start = 524322; 1340 (*vtoc)->efi_parts[6].p_size = (*vtoc)->efi_last_u_lba - 524322 1341 - (1024 * 16); 1342 1343 /* efi reserved partition - s9 16K */ 1344 (*vtoc)->efi_parts[8].p_start = (*vtoc)->efi_last_u_lba - (1024 * 16); 1345 (*vtoc)->efi_parts[8].p_size = (1024 * 16); 1346 (*vtoc)->efi_parts[8].p_tag = V_RESERVED; 1347 return (0); 1348 } 1349