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