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