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