gpt.c revision 1.19 1 /* $NetBSD: gpt.c,v 1.19 2020/10/03 18:54:18 martin Exp $ */
2
3 /*
4 * Copyright 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include "defs.h"
31 #include "mbr.h"
32 #include "md.h"
33 #include "gpt_uuid.h"
34 #include <assert.h>
35 #include <err.h>
36 #include <paths.h>
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <util.h>
40 #include <uuid.h>
41
42 bool gpt_parts_check(void); /* check for needed binaries */
43
44
45 /*************** GPT ************************************************/
46 /* a GPT based disk_partitions interface */
47
48 #define GUID_STR_LEN 40
49 #define GPT_PTYPE_ALLOC 32 /* initial type array allocation, should be >
50 * gpt type -l | wc -l */
51 #define GPT_DEV_LEN 16 /* dkNN */
52
53 #define GPT_PARTS_PER_SEC 4 /* a 512 byte sector holds 4 entries */
54 #define GPT_DEFAULT_MAX_PARTS 128
55
56 /* a usable label will be short, so we can get away with an arbitrary limit */
57 #define GPT_LABEL_LEN 96
58
59 #define GPT_ATTR_BIOSBOOT 1
60 #define GPT_ATTR_BOOTME 2
61 #define GPT_ATTR_BOOTONCE 4
62 #define GPT_ATTR_BOOTFAILED 8
63 #define GPT_ATTR_NOBLOCKIO 16
64 #define GPT_ATTR_REQUIRED 32
65
66 /* when we don't care for BIOS or UEFI boot, use the combined boot flags */
67 #define GPT_ATTR_BOOT (GPT_ATTR_BIOSBOOT|GPT_ATTR_BOOTME)
68
69 struct gpt_attr_desc {
70 const char *name;
71 uint flag;
72 };
73 static const struct gpt_attr_desc gpt_avail_attrs[] = {
74 { "biosboot", GPT_ATTR_BIOSBOOT },
75 { "bootme", GPT_ATTR_BOOTME },
76 { "bootonce", GPT_ATTR_BOOTONCE },
77 { "bootfailed", GPT_ATTR_BOOTFAILED },
78 { "noblockio", GPT_ATTR_NOBLOCKIO },
79 { "required", GPT_ATTR_REQUIRED },
80 { NULL, 0 }
81 };
82
83 struct gpt_ptype_desc {
84 struct part_type_desc gent;
85 char tid[GUID_STR_LEN];
86 uint fsflags, default_fs_type;
87 };
88
89 static const
90 struct {
91 const char *name;
92 uint fstype;
93 enum part_type ptype;
94 uint fsflags;
95 } gpt_fs_types[] = {
96 { .name = "ffs", .fstype = FS_BSDFFS, .ptype = PT_root,
97 .fsflags = GLM_LIKELY_FFS },
98 { .name = "swap", .fstype = FS_SWAP, .ptype = PT_swap },
99 { .name = "windows", .fstype = FS_MSDOS, .ptype = PT_FAT,
100 .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
101 { .name = "windows", .fstype = FS_NTFS, .ptype = PT_FAT,
102 .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
103 { .name = "efi", .fstype = FS_MSDOS, .ptype = PT_EFI_SYSTEM,
104 .fsflags = GLM_MAYBE_FAT32 },
105 { .name = "bios", .fstype = FS_MSDOS, .ptype = PT_FAT,
106 .fsflags = GLM_MAYBE_FAT32 },
107 { .name = "lfs", .fstype = FS_BSDLFS, .ptype = PT_root },
108 { .name = "linux-data", .fstype = FS_EX2FS, .ptype = PT_root },
109 { .name = "apple", .fstype = FS_HFS, .ptype = PT_unknown },
110 { .name = "ccd", .fstype = FS_CCD, .ptype = PT_root },
111 { .name = "cgd", .fstype = FS_CGD, .ptype = PT_root },
112 { .name = "raid", .fstype = FS_RAID, .ptype = PT_root },
113 { .name = "vmcore", .fstype = FS_VMKCORE, .ptype = PT_unknown },
114 { .name = "vmfs", .fstype = FS_VMFS, .ptype = PT_unknown },
115 { .name = "vmresered", .fstype = FS_VMWRESV, .ptype = PT_unknown },
116 { .name = "zfs", .fstype = FS_ZFS, .ptype = PT_root },
117 };
118
119 static size_t gpt_ptype_cnt = 0, gpt_ptype_alloc = 0;
120 static struct gpt_ptype_desc *gpt_ptype_descs = NULL;
121
122 /* "well" known types with special handling */
123 static const struct part_type_desc *gpt_native_root;
124
125 /* similar to struct gpt_ent, but matching our needs */
126 struct gpt_part_entry {
127 const struct gpt_ptype_desc *gp_type;
128 char gp_id[GUID_STR_LEN]; /* partition guid as string */
129 daddr_t gp_start, gp_size;
130 uint gp_attr; /* various attribute bits */
131 char gp_label[GPT_LABEL_LEN]; /* user defined label */
132 char gp_dev_name[GPT_DEV_LEN]; /* name of wedge */
133 const char *last_mounted; /* last mounted if known */
134 uint fs_type, fs_sub_type; /* FS_* and maybe sub type */
135 uint gp_flags;
136 #define GPEF_ON_DISK 1 /* This entry exists on-disk */
137 #define GPEF_MODIFIED 2 /* this entry has been changed */
138 #define GPEF_WEDGE 4 /* wedge for this exists */
139 #define GPEF_RESIZED 8 /* size has changed */
140 #define GPEF_TARGET 16 /* marked install target */
141 struct gpt_part_entry *gp_next;
142 };
143
144 static const struct gpt_ptype_desc *gpt_find_native_type(
145 const struct part_type_desc *gent);
146 static const struct gpt_ptype_desc *gpt_find_guid_type(const char*);
147 static bool
148 gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
149 const char **err_msg);
150
151 const struct disk_partitioning_scheme gpt_parts;
152 struct gpt_disk_partitions {
153 struct disk_partitions dp;
154 /*
155 * We keep a list of our current valid partitions, pointed
156 * to by "partitions".
157 * dp.num_part is the number of entries in "partitions".
158 * When partitions that have a representation on disk already
159 * are deleted, we move them to the "obsolete" list so we
160 * can issue the proper commands to remove it when writing back.
161 */
162 struct gpt_part_entry *partitions, /* current partitions */
163 *obsolete; /* deleted partitions */
164 size_t max_num_parts; /* how many entries max? */
165 size_t prologue, epilogue; /* number of sectors res. */
166 bool has_gpt; /* disk already has a GPT */
167 };
168
169 /*
170 * Init global variables from MD details
171 */
172 static void
173 gpt_md_init(bool is_boot_disk, size_t *max_parts, size_t *head, size_t *tail)
174 {
175 size_t num;
176
177 if (is_boot_disk) {
178 #ifdef MD_GPT_INITIAL_SIZE
179 #if MD_GPT_INITIAL_SIZE < 2*512
180 #error impossible small GPT prologue
181 #endif
182 num = ((MD_GPT_INITIAL_SIZE-(2*512))/512)*GPT_PARTS_PER_SEC;
183 #else
184 num = GPT_DEFAULT_MAX_PARTS;
185 #endif
186 } else {
187 num = GPT_DEFAULT_MAX_PARTS;
188 }
189 *max_parts = num;
190 *head = 2 + num/GPT_PARTS_PER_SEC;
191 *tail = 1 + num/GPT_PARTS_PER_SEC;
192 }
193
194 /*
195 * Parse a part of "gpt show" output into a struct gpt_part_entry.
196 * Output is from "show -a" format if details = false, otherwise
197 * from details for a specific partition (show -i or show -b)
198 */
199 static void
200 gpt_add_info(struct gpt_part_entry *part, const char *tag, char *val,
201 bool details)
202 {
203 char *s, *e;
204
205 if (details && strcmp(tag, "Start:") == 0) {
206 part->gp_start = strtouq(val, NULL, 10);
207 } else if (details && strcmp(tag, "Size:") == 0) {
208 part->gp_size = strtouq(val, NULL, 10);
209 } else if (details && strcmp(tag, "Type:") == 0) {
210 s = strchr(val, '(');
211 if (!s)
212 return;
213 e = strchr(s, ')');
214 if (!e)
215 return;
216 *e = 0;
217 part->gp_type = gpt_find_guid_type(s+1);
218 } else if (strcmp(tag, "TypeID:") == 0) {
219 part->gp_type = gpt_find_guid_type(val);
220 } else if (strcmp(tag, "GUID:") == 0) {
221 strlcpy(part->gp_id, val, sizeof(part->gp_id));
222 } else if (strcmp(tag, "Label:") == 0) {
223 strlcpy(part->gp_label, val, sizeof(part->gp_label));
224 } else if (strcmp(tag, "Attributes:") == 0) {
225 char *n;
226
227 while ((n = strsep(&val, ", ")) != NULL) {
228 if (*n == 0)
229 continue;
230 for (const struct gpt_attr_desc *p = gpt_avail_attrs;
231 p->name != NULL; p++) {
232 if (strcmp(p->name, n) == 0)
233 part->gp_attr |= p->flag;
234 }
235 }
236 }
237 }
238
239 /*
240 * Find the partition matching this wedge info and record that we
241 * have a wedge already.
242 */
243 static void
244 update_part_from_wedge_info(struct gpt_disk_partitions *parts,
245 const struct dkwedge_info *dkw)
246 {
247 for (struct gpt_part_entry *p = parts->partitions; p != NULL;
248 p = p->gp_next) {
249 if (p->gp_start != dkw->dkw_offset ||
250 (uint64_t)p->gp_size != dkw->dkw_size)
251 continue;
252 p->gp_flags |= GPEF_WEDGE;
253 strlcpy(p->gp_dev_name, dkw->dkw_devname,
254 sizeof p->gp_dev_name);
255 return;
256 }
257 }
258
259 static struct disk_partitions *
260 gpt_read_from_disk(const char *dev, daddr_t start, daddr_t len, size_t bps,
261 const struct disk_partitioning_scheme *scheme)
262 {
263 char diskpath[MAXPATHLEN];
264 int fd;
265 struct dkwedge_info *dkw;
266 struct dkwedge_list dkwl;
267 size_t bufsize, dk;
268
269 assert(start == 0);
270 assert(have_gpt);
271
272 if (run_program(RUN_SILENT | RUN_ERROR_OK,
273 "gpt -rq header %s", dev) != 0)
274 return NULL;
275
276 /* read the partitions */
277 int i;
278 unsigned int p_index;
279 daddr_t p_start = 0, p_size = 0, avail_start = 0, avail_size = 0,
280 disk_size = 0;
281 char *textbuf, *t, *tt, p_type[STRSIZE];
282 static const char regpart_prefix[] = "GPT part - ";
283 struct gpt_disk_partitions *parts;
284 struct gpt_part_entry *last = NULL, *add_to = NULL;
285 const struct gpt_ptype_desc *native_root
286 = gpt_find_native_type(gpt_native_root);
287 bool have_target = false;
288
289 if (collect(T_OUTPUT, &textbuf, "gpt -r show -a %s 2>/dev/null", dev)
290 < 1)
291 return NULL;
292
293 /* parse output and create our list */
294 parts = calloc(1, sizeof(*parts));
295 if (parts == NULL)
296 return NULL;
297
298 (void)strtok(textbuf, "\n"); /* ignore first line */
299 while ((t = strtok(NULL, "\n")) != NULL) {
300 i = 0; p_start = 0; p_size = 0; p_index = 0;
301 p_type[0] = 0;
302 while ((tt = strsep(&t, " \t")) != NULL) {
303 if (strlen(tt) == 0)
304 continue;
305 if (i == 0) {
306 if (add_to != NULL)
307 gpt_add_info(add_to, tt, t, false);
308 p_start = strtouq(tt, NULL, 10);
309 if (p_start == 0 && add_to != NULL)
310 break;
311 else
312 add_to = NULL;
313 }
314 if (i == 1)
315 p_size = strtouq(tt, NULL, 10);
316 if (i == 2)
317 p_index = strtouq(tt, NULL, 10);
318 if (i > 2 || (i == 2 && p_index == 0)) {
319 if (p_type[0])
320 strlcat(p_type, " ", STRSIZE);
321 strlcat(p_type, tt, STRSIZE);
322 }
323 i++;
324 }
325
326 if (p_start == 0 || p_size == 0)
327 continue;
328 else if (strcmp(p_type, "Pri GPT table") == 0) {
329 avail_start = p_start + p_size;
330 parts->prologue = avail_start;
331 parts->epilogue = p_size + 1;
332 parts->max_num_parts = p_size * GPT_PARTS_PER_SEC;
333 } else if (strcmp(p_type, "Sec GPT table") == 0)
334 avail_size = p_start - avail_start;
335 else if(strcmp(p_type, "Sec GPT header") == 0)
336 disk_size = p_start + p_size;
337 else if (p_index == 0 && strlen(p_type) > 0)
338 /* Utilitary entry (PMBR, etc) */
339 continue;
340 else if (p_index == 0) {
341 /* Free space */
342 continue;
343 } else {
344 /* Usual partition */
345 tt = p_type;
346 if (strncmp(tt, regpart_prefix,
347 strlen(regpart_prefix)) == 0)
348 tt += strlen(regpart_prefix);
349
350 /* Add to our linked list */
351 struct gpt_part_entry *np = calloc(1, sizeof(*np));
352 if (np == NULL)
353 break;
354
355 strlcpy(np->gp_label, tt, sizeof(np->gp_label));
356 np->gp_start = p_start;
357 np->gp_size = p_size;
358 np->gp_flags |= GPEF_ON_DISK;
359 if (!have_target && native_root != NULL &&
360 strcmp(np->gp_id, native_root->tid) == 0) {
361 have_target = true;
362 np->gp_flags |= GPEF_TARGET;
363 }
364
365 if (last == NULL)
366 parts->partitions = np;
367 else
368 last->gp_next = np;
369 last = np;
370 add_to = np;
371 parts->dp.num_part++;
372 }
373 }
374 free(textbuf);
375
376 /* If the GPT was not complete (e.g. truncated image), barf */
377 if (disk_size <= 0) {
378 free(parts);
379 return NULL;
380 }
381
382 parts->dp.pscheme = scheme;
383 parts->dp.disk = strdup(dev);
384 parts->dp.disk_start = start;
385 parts->dp.disk_size = disk_size;
386 parts->dp.free_space = avail_size;
387 parts->dp.bytes_per_sector = bps;
388 parts->has_gpt = true;
389
390 fd = opendisk(parts->dp.disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
391 for (struct gpt_part_entry *p = parts->partitions; p != NULL;
392 p = p->gp_next) {
393 #ifdef DEFAULT_UFS2
394 bool fs_is_default = false;
395 #endif
396
397 if (p->gp_type != NULL) {
398
399 if (p->gp_type->fsflags != 0) {
400 const char *lm = get_last_mounted(fd,
401 p->gp_start, &p->fs_type,
402 &p->fs_sub_type, p->gp_type->fsflags);
403 if (lm != NULL && *lm != 0) {
404 char *path = strdup(lm);
405 canonicalize_last_mounted(path);
406 p->last_mounted = path;
407 } else {
408 p->fs_type = p->gp_type->
409 default_fs_type;
410 #ifdef DEFAULT_UFS2
411 fs_is_default = true;
412 #endif
413 }
414 } else {
415 p->fs_type = p->gp_type->default_fs_type;
416 #ifdef DEFAULT_UFS2
417 fs_is_default = true;
418 #endif
419 }
420 #ifdef DEFAULT_UFS2
421 if (fs_is_default && p->fs_type == FS_BSDFFS)
422 p->fs_sub_type = 2;
423 #endif
424 }
425
426 parts->dp.free_space -= p->gp_size;
427 }
428
429 /*
430 * Check if we have any (matching/auto-configured) wedges already
431 */
432 dkw = NULL;
433 dkwl.dkwl_buf = dkw;
434 dkwl.dkwl_bufsize = 0;
435 if (ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
436 /* do not even try to deal with any races at this point */
437 bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
438 dkw = malloc(bufsize);
439 dkwl.dkwl_buf = dkw;
440 dkwl.dkwl_bufsize = bufsize;
441 if (dkw != NULL && ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
442 for (dk = 0; dk < dkwl.dkwl_ncopied; dk++)
443 update_part_from_wedge_info(parts, &dkw[dk]);
444 }
445 free(dkw);
446 }
447
448 close(fd);
449
450 return &parts->dp;
451 }
452
453 static size_t
454 gpt_cyl_size(const struct disk_partitions *arg)
455 {
456 return MEG / 512;
457 }
458
459 static struct disk_partitions *
460 gpt_create_new(const char *disk, daddr_t start, daddr_t len,
461 bool is_boot_drive, struct disk_partitions *parent)
462 {
463 struct gpt_disk_partitions *parts;
464 struct disk_geom geo;
465
466 if (start != 0) {
467 assert(0);
468 return NULL;
469 }
470
471 if (!get_disk_geom(disk, &geo))
472 return NULL;
473
474 parts = calloc(1, sizeof(*parts));
475 if (!parts)
476 return NULL;
477
478 parts->dp.pscheme = &gpt_parts;
479 parts->dp.disk = strdup(disk);
480
481 gpt_md_init(is_boot_drive, &parts->max_num_parts, &parts->prologue,
482 &parts->epilogue);
483
484 parts->dp.disk_start = start;
485 parts->dp.disk_size = len;
486 parts->dp.bytes_per_sector = geo.dg_secsize;
487 parts->dp.free_space = len - start - parts->prologue - parts->epilogue;
488 parts->has_gpt = false;
489
490 return &parts->dp;
491 }
492
493 static bool
494 gpt_get_part_info(const struct disk_partitions *arg, part_id id,
495 struct disk_part_info *info)
496 {
497 static const struct part_type_desc gpt_unknown_type =
498 { .generic_ptype = PT_undef,
499 .short_desc = "<unknown>" };
500 const struct gpt_disk_partitions *parts =
501 (const struct gpt_disk_partitions*)arg;
502 const struct gpt_part_entry *p = parts->partitions;
503 part_id no;
504
505 for (no = 0; p != NULL && no < id; no++)
506 p = p->gp_next;
507
508 if (no != id || p == NULL)
509 return false;
510
511 memset(info, 0, sizeof(*info));
512 info->start = p->gp_start;
513 info->size = p->gp_size;
514 if (p->gp_type)
515 info->nat_type = &p->gp_type->gent;
516 else
517 info->nat_type = &gpt_unknown_type;
518 info->last_mounted = p->last_mounted;
519 info->fs_type = p->fs_type;
520 info->fs_sub_type = p->fs_sub_type;
521 if (p->gp_flags & GPEF_TARGET)
522 info->flags |= PTI_INSTALL_TARGET;
523
524 return true;
525 }
526
527 static bool
528 gpt_get_part_attr_str(const struct disk_partitions *arg, part_id id,
529 char *str, size_t avail_space)
530 {
531 const struct gpt_disk_partitions *parts =
532 (const struct gpt_disk_partitions*)arg;
533 const struct gpt_part_entry *p = parts->partitions;
534 part_id no;
535 static const char *flags = NULL;
536
537 for (no = 0; p != NULL && no < id; no++)
538 p = p->gp_next;
539
540 if (no != id || p == NULL)
541 return false;
542
543 if (flags == NULL)
544 flags = msg_string(MSG_gpt_flags);
545
546 if (avail_space < 2)
547 return false;
548
549 if (p->gp_attr & GPT_ATTR_BOOT)
550 *str++ = flags[0];
551 *str = 0;
552
553 return true;
554 }
555
556 /*
557 * Find insert position and check for duplicates.
558 * If all goes well, insert the new "entry" in the "list".
559 * If there are collisions, report "no free space".
560 * We keep all lists sorted by start sector number,
561 */
562 static bool
563 gpt_insert_part_into_list(struct gpt_disk_partitions *parts,
564 struct gpt_part_entry **list,
565 struct gpt_part_entry *entry, const char **err_msg)
566 {
567 struct gpt_part_entry *p, *last;
568
569 /* find the first entry past the new one (if any) */
570 for (last = NULL, p = *list; p != NULL; last = p, p = p->gp_next) {
571 if (p->gp_start > entry->gp_start)
572 break;
573 }
574
575 /* check if last partition overlaps with new one */
576 if (last) {
577 if (last->gp_start + last->gp_size > entry->gp_start) {
578 if (err_msg)
579 *err_msg = msg_string(MSG_No_free_space);
580 return false;
581 }
582 }
583
584 if (p == NULL) {
585 entry->gp_next = NULL;
586 if (last != NULL) {
587 last->gp_next = entry;
588 }
589 } else {
590 /* check if new entry overlaps with next */
591 if (entry->gp_start + entry->gp_size > p->gp_start) {
592 if (err_msg)
593 *err_msg = msg_string(MSG_No_free_space);
594 return false;
595 }
596
597 entry->gp_next = p;
598 if (last != NULL)
599 last->gp_next = entry;
600 else
601 *list = entry;
602 }
603 if (*list == NULL)
604 *list = entry;
605
606 return true;
607 }
608
609 static bool
610 gpt_set_part_info(struct disk_partitions *arg, part_id id,
611 const struct disk_part_info *info, const char **err_msg)
612 {
613 struct gpt_disk_partitions *parts =
614 (struct gpt_disk_partitions*)arg;
615 struct gpt_part_entry *p = parts->partitions, *n;
616 part_id no;
617 daddr_t lendiff;
618 bool was_target;
619
620 for (no = 0; p != NULL && no < id; no++)
621 p = p->gp_next;
622
623 if (no != id || p == NULL)
624 return false;
625
626 /* update target mark - we can only have one */
627 was_target = (p->gp_flags & GPEF_TARGET) != 0;
628 if (info->flags & PTI_INSTALL_TARGET)
629 p->gp_flags |= GPEF_TARGET;
630 else
631 p->gp_flags &= ~GPEF_TARGET;
632 if (was_target)
633 for (n = parts->partitions; n != NULL; n = n->gp_next)
634 if (n != p)
635 n->gp_flags &= ~GPEF_TARGET;
636
637 if ((p->gp_flags & GPEF_ON_DISK)) {
638 if (info->start != p->gp_start) {
639 /* partition moved, we need to delete and re-add */
640 n = calloc(1, sizeof(*n));
641 if (n == NULL) {
642 if (err_msg)
643 *err_msg = err_outofmem;
644 return false;
645 }
646 *n = *p;
647 p->gp_flags &= ~GPEF_ON_DISK;
648 if (!gpt_insert_part_into_list(parts, &parts->obsolete,
649 n, err_msg))
650 return false;
651 } else if (info->size != p->gp_size) {
652 p->gp_flags |= GPEF_RESIZED;
653 }
654 }
655
656 p->gp_flags |= GPEF_MODIFIED;
657
658 lendiff = info->size - p->gp_size;
659 parts->dp.free_space -= lendiff;
660 return gpt_info_to_part(p, info, err_msg);
661 }
662
663 static size_t
664 gpt_get_free_spaces_internal(const struct gpt_disk_partitions *parts,
665 struct disk_part_free_space *result, size_t max_num_result,
666 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
667 {
668 size_t cnt = 0;
669 daddr_t s, e, from, size, end_of_disk;
670 struct gpt_part_entry *p;
671
672 if (align > 1)
673 start = max(roundup(start, align), align);
674 if (start < 0 || start < (daddr_t)parts->prologue)
675 start = parts->prologue;
676 if (parts->dp.disk_start != 0 && parts->dp.disk_start > start)
677 start = parts->dp.disk_start;
678 if (min_space_size < 1)
679 min_space_size = 1;
680 end_of_disk = parts->dp.disk_start + parts->dp.disk_size
681 - parts->epilogue;
682 from = start;
683 while (from < end_of_disk && cnt < max_num_result) {
684 again:
685 size = parts->dp.disk_start + parts->dp.disk_size - from;
686 start = from;
687 if (start + size > end_of_disk)
688 size = end_of_disk - start;
689 for (p = parts->partitions; p != NULL; p = p->gp_next) {
690 s = p->gp_start;
691 e = p->gp_size + s;
692 if (s == ignore)
693 continue;
694 if (e < from)
695 continue;
696 if (s <= from && e > from) {
697 if (e - 1 >= end_of_disk)
698 return cnt;
699 from = e + 1;
700 if (align > 1) {
701 from = max(roundup(from, align), align);
702 if (from >= end_of_disk) {
703 size = 0;
704 break;
705 }
706 }
707 goto again;
708 }
709 if (s > from && s - from < size) {
710 size = s - from;
711 }
712 }
713 if (size >= min_space_size) {
714 result->start = start;
715 result->size = size;
716 result++;
717 cnt++;
718 }
719 from += size + 1;
720 if (align > 1)
721 from = max(roundup(from, align), align);
722 }
723
724 return cnt;
725 }
726
727 static daddr_t
728 gpt_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
729 {
730 const struct gpt_disk_partitions *parts =
731 (const struct gpt_disk_partitions*)arg;
732 struct disk_part_free_space space;
733
734 if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 0,
735 start, start) == 1)
736 return space.size;
737
738 return 0;
739 }
740
741 static size_t
742 gpt_get_free_spaces(const struct disk_partitions *arg,
743 struct disk_part_free_space *result, size_t max_num_result,
744 daddr_t min_space_size, daddr_t align, daddr_t start,
745 daddr_t ignore)
746 {
747 const struct gpt_disk_partitions *parts =
748 (const struct gpt_disk_partitions*)arg;
749
750 return gpt_get_free_spaces_internal(parts, result,
751 max_num_result, min_space_size, align, start, ignore);
752 }
753
754 static void
755 gpt_match_ptype(const char *name, struct gpt_ptype_desc *t)
756 {
757 size_t i;
758
759 for (i = 0; i < __arraycount(gpt_fs_types); i++) {
760 if (strcmp(name, gpt_fs_types[i].name) == 0) {
761 t->gent.generic_ptype = gpt_fs_types[i].ptype;
762 t->fsflags = gpt_fs_types[i].fsflags;
763 t->default_fs_type = gpt_fs_types[i].fstype;
764
765 /* recongnize special entries */
766 if (gpt_native_root == NULL && i == 0)
767 gpt_native_root = &t->gent;
768
769 return;
770 }
771 }
772
773 t->gent.generic_ptype = PT_unknown;
774 t->fsflags = 0;
775 t->default_fs_type = FS_BSDFFS;
776 }
777
778 static void
779 gpt_internal_add_ptype(const char *uid, const char *name, const char *desc)
780 {
781 if (gpt_ptype_cnt >= gpt_ptype_alloc) {
782 gpt_ptype_alloc = gpt_ptype_alloc ? 2*gpt_ptype_alloc
783 : GPT_PTYPE_ALLOC;
784 struct gpt_ptype_desc *nptypes = realloc(gpt_ptype_descs,
785 gpt_ptype_alloc*sizeof(*gpt_ptype_descs));
786 if (nptypes == 0)
787 errx(EXIT_FAILURE, "out of memory");
788 gpt_ptype_descs = nptypes;
789 }
790
791 strlcpy(gpt_ptype_descs[gpt_ptype_cnt].tid, uid,
792 sizeof(gpt_ptype_descs[gpt_ptype_cnt].tid));
793 gpt_ptype_descs[gpt_ptype_cnt].gent.short_desc = strdup(name);
794 gpt_ptype_descs[gpt_ptype_cnt].gent.description = strdup(desc);
795 gpt_match_ptype(name, &gpt_ptype_descs[gpt_ptype_cnt]);
796 gpt_ptype_cnt++;
797 }
798
799 static void
800 gpt_init_ptypes(void)
801 {
802 if (gpt_ptype_cnt == 0)
803 gpt_uuid_query(gpt_internal_add_ptype);
804 }
805
806 static void
807 gpt_cleanup(void)
808 {
809 /* free all of gpt_ptype_descs */
810 for (size_t i = 0; i < gpt_ptype_cnt; i++) {
811 free(__UNCONST(gpt_ptype_descs[i].gent.short_desc));
812 free(__UNCONST(gpt_ptype_descs[i].gent.description));
813 }
814 free(gpt_ptype_descs);
815 gpt_ptype_descs = NULL;
816 gpt_ptype_cnt = gpt_ptype_alloc = 0;
817 }
818
819 static size_t
820 gpt_type_count(void)
821 {
822 if (gpt_ptype_cnt == 0)
823 gpt_init_ptypes();
824
825 return gpt_ptype_cnt;
826 }
827
828 static const struct part_type_desc *
829 gpt_get_ptype(size_t ndx)
830 {
831 if (gpt_ptype_cnt == 0)
832 gpt_init_ptypes();
833
834 if (ndx >= gpt_ptype_cnt)
835 return NULL;
836
837 return &gpt_ptype_descs[ndx].gent;
838 }
839
840 static const struct part_type_desc *
841 gpt_get_generic_type(enum part_type gent)
842 {
843 if (gpt_ptype_cnt == 0)
844 gpt_init_ptypes();
845
846 if (gent == PT_root)
847 return gpt_native_root;
848 if (gent == PT_unknown)
849 return NULL;
850
851 for (size_t i = 0; i < gpt_ptype_cnt; i++)
852 if (gpt_ptype_descs[i].gent.generic_ptype == gent)
853 return &gpt_ptype_descs[i].gent;
854
855 return NULL;
856 }
857
858 static const struct gpt_ptype_desc *
859 gpt_find_native_type(const struct part_type_desc *gent)
860 {
861 if (gpt_ptype_cnt == 0)
862 gpt_init_ptypes();
863
864 if (gent == NULL)
865 return NULL;
866
867 for (size_t i = 0; i < gpt_ptype_cnt; i++)
868 if (gent == &gpt_ptype_descs[i].gent)
869 return &gpt_ptype_descs[i];
870
871 gent = gpt_get_generic_type(gent->generic_ptype);
872 if (gent == NULL)
873 return NULL;
874
875 /* this can not recurse deeper than once, we would not have found a
876 * generic type a few lines above if it would. */
877 return gpt_find_native_type(gent);
878 }
879
880 static const struct gpt_ptype_desc *
881 gpt_find_guid_type(const char *uid)
882 {
883 if (gpt_ptype_cnt == 0)
884 gpt_init_ptypes();
885
886 if (uid == NULL || uid[0] == 0)
887 return NULL;
888
889 for (size_t i = 0; i < gpt_ptype_cnt; i++)
890 if (strcmp(gpt_ptype_descs[i].tid, uid) == 0)
891 return &gpt_ptype_descs[i];
892
893 return NULL;
894 }
895
896 static const struct part_type_desc *
897 gpt_find_type(const char *desc)
898 {
899 if (gpt_ptype_cnt == 0)
900 gpt_init_ptypes();
901
902 if (desc == NULL || desc[0] == 0)
903 return NULL;
904
905 for (size_t i = 0; i < gpt_ptype_cnt; i++)
906 if (strcmp(gpt_ptype_descs[i].gent.short_desc, desc) == 0)
907 return &gpt_ptype_descs[i].gent;
908
909 return NULL;
910 }
911
912 static const struct part_type_desc *
913 gpt_get_fs_part_type(enum part_type pt, unsigned fstype, unsigned fs_sub_type)
914 {
915 size_t i;
916
917 /* Try with complete match (including part_type) first */
918 for (i = 0; i < __arraycount(gpt_fs_types); i++)
919 if (fstype == gpt_fs_types[i].fstype &&
920 pt == gpt_fs_types[i].ptype)
921 return gpt_find_type(gpt_fs_types[i].name);
922
923 /* If that did not work, ignore part_type */
924 for (i = 0; i < __arraycount(gpt_fs_types); i++)
925 if (fstype == gpt_fs_types[i].fstype)
926 return gpt_find_type(gpt_fs_types[i].name);
927
928 return NULL;
929 }
930
931 static bool
932 gpt_get_default_fstype(const struct part_type_desc *nat_type,
933 unsigned *fstype, unsigned *fs_sub_type)
934 {
935 const struct gpt_ptype_desc *gtype;
936
937 gtype = gpt_find_native_type(nat_type);
938 if (gtype == NULL)
939 return false;
940
941 *fstype = gtype->default_fs_type;
942 #ifdef DEFAULT_UFS2
943 if (gtype->default_fs_type == FS_BSDFFS)
944 *fs_sub_type = 2;
945 else
946 #endif
947 *fs_sub_type = 0;
948 return true;
949 }
950
951 static const struct part_type_desc *
952 gpt_get_uuid_part_type(const uuid_t *id)
953 {
954 char str[GUID_STR_LEN], desc[GUID_STR_LEN + MENUSTRSIZE];
955 const struct gpt_ptype_desc *t;
956 char *guid = NULL;
957 uint32_t err;
958
959 uuid_to_string(id, &guid, &err);
960 strlcpy(str, err == uuid_s_ok ? guid : "-", sizeof str);
961 free(guid);
962
963 t = gpt_find_guid_type(str);
964 if (t == NULL) {
965 snprintf(desc, sizeof desc, "%s (%s)",
966 msg_string(MSG_custom_type), str);
967 gpt_internal_add_ptype(str, str, desc);
968 t = gpt_find_guid_type(str);
969 assert(t != NULL);
970 }
971 return &t->gent;
972 }
973
974 static const struct part_type_desc *
975 gpt_create_custom_part_type(const char *custom, const char **err_msg)
976 {
977 uuid_t id;
978 uint32_t err;
979
980 uuid_from_string(custom, &id, &err);
981 if (err_msg != NULL &&
982 (err == uuid_s_invalid_string_uuid || err == uuid_s_bad_version)) {
983 *err_msg = MSG_invalid_guid;
984 return NULL;
985 }
986 if (err != uuid_s_ok)
987 return NULL;
988
989 return gpt_get_uuid_part_type(&id);
990 }
991
992 static const struct part_type_desc *
993 gpt_create_unknown_part_type(void)
994 {
995 uuid_t id;
996 uint32_t err;
997
998 uuid_create(&id, &err);
999 if (err != uuid_s_ok)
1000 return NULL;
1001
1002 return gpt_get_uuid_part_type(&id);
1003 }
1004
1005 static daddr_t
1006 gpt_get_part_alignment(const struct disk_partitions *parts)
1007 {
1008
1009 assert(parts->disk_size > 0);
1010 if (parts->disk_size < 0)
1011 return 1;
1012
1013 /* Use 1MB offset/alignemnt for large (>128GB) disks */
1014 if (parts->disk_size > HUGE_DISK_SIZE)
1015 return 2048;
1016 else if (parts->disk_size > TINY_DISK_SIZE)
1017 return 64;
1018 else
1019 return 4;
1020 }
1021
1022 static bool
1023 gpt_can_add_partition(const struct disk_partitions *arg)
1024 {
1025 const struct gpt_disk_partitions *parts =
1026 (const struct gpt_disk_partitions*)arg;
1027 struct disk_part_free_space space;
1028 daddr_t align;
1029
1030 if (parts->dp.num_part >= parts->max_num_parts)
1031 return false;
1032
1033 align = gpt_get_part_alignment(arg);
1034 if (parts->dp.free_space <= align)
1035 return false;
1036
1037 if (gpt_get_free_spaces_internal(parts, &space, 1, align, align,
1038 0, -1) < 1)
1039 return false;
1040
1041 return true;
1042 }
1043
1044 static bool
1045 gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
1046 const char **err_msg)
1047 {
1048 p->gp_type = gpt_find_native_type(info->nat_type);
1049 p->gp_start = info->start;
1050 p->gp_size = info->size;
1051 if (info->last_mounted != NULL && info->last_mounted !=
1052 p->last_mounted) {
1053 free(__UNCONST(p->last_mounted));
1054 p->last_mounted = strdup(info->last_mounted);
1055 }
1056 p->fs_type = info->fs_type;
1057 p->fs_sub_type = info->fs_sub_type;
1058
1059 return true;
1060 }
1061
1062 static part_id
1063 gpt_add_part(struct disk_partitions *arg,
1064 const struct disk_part_info *info, const char **err_msg)
1065 {
1066 struct gpt_disk_partitions *parts =
1067 (struct gpt_disk_partitions*)arg;
1068 struct disk_part_free_space space;
1069 struct disk_part_info data = *info;
1070 struct gpt_part_entry *p;
1071 bool ok;
1072
1073 if (err_msg != NULL)
1074 *err_msg = NULL;
1075
1076 if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 1,
1077 info->start, -1) < 1) {
1078 if (err_msg)
1079 *err_msg = msg_string(MSG_No_free_space);
1080 return NO_PART;
1081 }
1082 if (parts->dp.num_part >= parts->max_num_parts) {
1083 if (err_msg)
1084 *err_msg = msg_string(MSG_err_too_many_partitions);
1085 return NO_PART;
1086 }
1087
1088 if (data.size > space.size)
1089 data.size = space.size;
1090
1091 p = calloc(1, sizeof(*p));
1092 if (p == NULL) {
1093 if (err_msg != NULL)
1094 *err_msg = INTERNAL_ERROR;
1095 return NO_PART;
1096 }
1097 if (!gpt_info_to_part(p, &data, err_msg)) {
1098 free(p);
1099 return NO_PART;
1100 }
1101 p->gp_flags |= GPEF_MODIFIED;
1102 ok = gpt_insert_part_into_list(parts, &parts->partitions, p, err_msg);
1103 if (ok) {
1104 parts->dp.num_part++;
1105 parts->dp.free_space -= p->gp_size;
1106 return parts->dp.num_part-1;
1107 } else {
1108 free(p);
1109 return NO_PART;
1110 }
1111 }
1112
1113 static bool
1114 gpt_delete_partition(struct disk_partitions *arg, part_id id,
1115 const char **err_msg)
1116 {
1117 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1118 struct gpt_part_entry *p, *last = NULL;
1119 part_id i;
1120 bool res;
1121
1122 if (parts->dp.num_part == 0)
1123 return false;
1124
1125 for (i = 0, p = parts->partitions;
1126 i != id && i < parts->dp.num_part && p != NULL;
1127 i++, p = p->gp_next)
1128 last = p;
1129
1130 if (p == NULL) {
1131 if (err_msg)
1132 *err_msg = INTERNAL_ERROR;
1133 return false;
1134 }
1135
1136 if (last == NULL)
1137 parts->partitions = p->gp_next;
1138 else
1139 last->gp_next = p->gp_next;
1140
1141 res = true;
1142 if (p->gp_flags & GPEF_ON_DISK) {
1143 if (!gpt_insert_part_into_list(parts, &parts->obsolete,
1144 p, err_msg))
1145 res = false;
1146 } else {
1147 free(p);
1148 }
1149
1150 if (res) {
1151 parts->dp.num_part--;
1152 parts->dp.free_space += p->gp_size;
1153 }
1154
1155 return res;
1156 }
1157
1158 static bool
1159 gpt_delete_all_partitions(struct disk_partitions *arg)
1160 {
1161 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1162
1163 while (parts->dp.num_part > 0) {
1164 if (!gpt_delete_partition(&parts->dp, 0, NULL))
1165 return false;
1166 }
1167
1168 return true;
1169 }
1170
1171 static bool
1172 gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
1173 {
1174 char *textbuf, *t, *tt;
1175 static const char expected_hdr[] = "Details for index ";
1176
1177 /* run gpt show for this partition */
1178 if (collect(T_OUTPUT, &textbuf,
1179 "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
1180 return false;
1181
1182 /*
1183 * gpt show should respond with single partition details, but will
1184 * fall back to "show -a" output if something is wrong
1185 */
1186 t = strtok(textbuf, "\n"); /* first line is special */
1187 if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
1188 free(textbuf);
1189 return false;
1190 }
1191
1192 /* parse output into "old" */
1193 while ((t = strtok(NULL, "\n")) != NULL) {
1194 tt = strsep(&t, " \t");
1195 if (strlen(tt) == 0)
1196 continue;
1197 gpt_add_info(p, tt, t, true);
1198 }
1199 free(textbuf);
1200
1201 return true;
1202 }
1203
1204 static bool
1205 gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
1206 {
1207 size_t i;
1208 char attr_str[STRSIZE];
1209
1210 if (todo == 0)
1211 return true;
1212
1213 strcpy(attr_str, "-a ");
1214 for (i = 0; todo != 0; i++) {
1215 if (!(gpt_avail_attrs[i].flag & todo))
1216 continue;
1217 todo &= ~gpt_avail_attrs[i].flag;
1218 if (attr_str[0])
1219 strlcat(attr_str, ",",
1220 sizeof(attr_str));
1221 strlcat(attr_str,
1222 gpt_avail_attrs[i].name,
1223 sizeof(attr_str));
1224 }
1225 if (run_program(RUN_SILENT,
1226 "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
1227 return false;
1228 return true;
1229 }
1230
1231 /*
1232 * Modify an existing on-disk partition.
1233 * Start and size can not be changed here, caller needs to deal
1234 * with that kind of changes upfront.
1235 */
1236 static bool
1237 gpt_modify_part(const char *disk, struct gpt_part_entry *p)
1238 {
1239 struct gpt_part_entry old;
1240 uint todo_set, todo_unset;
1241
1242 /*
1243 * Query current on-disk state
1244 */
1245 memset(&old, 0, sizeof old);
1246 if (!gpt_read_part(disk, p->gp_start, &old))
1247 return false;
1248
1249 /* Reject unsupported changes */
1250 if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
1251 return false;
1252
1253 /*
1254 * GUID should never change, but the internal copy
1255 * may not yet know it.
1256 */
1257 strcpy(p->gp_id, old.gp_id);
1258
1259 /* Check type */
1260 if (p->gp_type != old.gp_type) {
1261 if (run_program(RUN_SILENT,
1262 "gpt label -b %" PRIu64 " -T %s %s",
1263 p->gp_start, p->gp_type->tid, disk) != 0)
1264 return false;
1265 }
1266
1267 /* Check label */
1268 if (strcmp(p->gp_label, old.gp_label) != 0) {
1269 if (run_program(RUN_SILENT,
1270 "gpt label -b %" PRIu64 " -l \'%s\' %s",
1271 p->gp_start, p->gp_label, disk) != 0)
1272 return false;
1273 }
1274
1275 /* Check attributes */
1276 if (p->gp_attr != old.gp_attr) {
1277 if (p->gp_attr == 0) {
1278 if (run_program(RUN_SILENT,
1279 "gpt set -N -b %" PRIu64 " %s",
1280 p->gp_start, disk) != 0)
1281 return false;
1282 } else {
1283 todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
1284 todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
1285 if (!gpt_apply_attr(disk, "unset", p->gp_start,
1286 todo_unset))
1287 return false;
1288 if (!gpt_apply_attr(disk, "set", p->gp_start,
1289 todo_set))
1290 return false;
1291 }
1292 }
1293
1294 return true;
1295 }
1296
1297 /*
1298 * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
1299 * map FS_* to wedge strings
1300 */
1301 static const char *
1302 bsdlabel_fstype_to_str(uint8_t fstype)
1303 {
1304 const char *str;
1305
1306 /*
1307 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
1308 * a suitable case branch will convert the type number to a string.
1309 */
1310 switch (fstype) {
1311 #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
1312 case __CONCAT(FS_,tag): str = __CONCAT(DKW_PTYPE_,tag); break;
1313 FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
1314 #undef FSTYPE_TO_STR_CASE
1315 default: str = NULL; break;
1316 }
1317
1318 return (str);
1319 }
1320
1321 static bool
1322 gpt_add_wedge(const char *disk, struct gpt_part_entry *p)
1323 {
1324 struct dkwedge_info dkw;
1325 const char *tname;
1326 char diskpath[MAXPATHLEN];
1327 int fd;
1328
1329 memset(&dkw, 0, sizeof(dkw));
1330 tname = bsdlabel_fstype_to_str(p->fs_type);
1331 if (tname)
1332 strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
1333
1334 strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
1335 dkw.dkw_offset = p->gp_start;
1336 dkw.dkw_size = p->gp_size;
1337 if (dkw.dkw_wname[0] == 0) {
1338 if (p->gp_label[0] != 0)
1339 strlcpy((char*)&dkw.dkw_wname,
1340 p->gp_label, sizeof(dkw.dkw_wname));
1341 }
1342 if (dkw.dkw_wname[0] == 0) {
1343 snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
1344 "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
1345 }
1346
1347 fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1348 if (fd < 0)
1349 return false;
1350 if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
1351 close(fd);
1352 return false;
1353 }
1354 close(fd);
1355
1356 strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
1357 p->gp_flags |= GPEF_WEDGE;
1358 return true;
1359 }
1360
1361 static void
1362 escape_spaces(char *dest, const char *src)
1363 {
1364 unsigned char c;
1365
1366 while (*src) {
1367 c = *src++;
1368 if (isspace(c) || c == '\\')
1369 *dest++ = '\\';
1370 *dest++ = c;
1371 }
1372 *dest = 0;
1373 }
1374
1375 static bool
1376 gpt_get_part_device(const struct disk_partitions *arg,
1377 part_id id, char *devname, size_t max_devname_len, int *part,
1378 enum dev_name_usage usage, bool with_path, bool life)
1379 {
1380 const struct gpt_disk_partitions *parts =
1381 (const struct gpt_disk_partitions*)arg;
1382 struct gpt_part_entry *p = parts->partitions;
1383 char tmpname[GPT_LABEL_LEN*2];
1384 part_id no;
1385
1386
1387 for (no = 0; p != NULL && no < id; no++)
1388 p = p->gp_next;
1389
1390 if (no != id || p == NULL)
1391 return false;
1392
1393 if (part)
1394 *part = -1;
1395
1396 if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
1397 usage = plain_name;
1398 if (usage == plain_name || usage == raw_dev_name)
1399 life = true;
1400 if (!(p->gp_flags & GPEF_WEDGE) && life)
1401 gpt_add_wedge(arg->disk, p);
1402
1403 switch (usage) {
1404 case logical_name:
1405 if (p->gp_label[0] != 0) {
1406 escape_spaces(tmpname, p->gp_label);
1407 snprintf(devname, max_devname_len,
1408 "NAME=%s", tmpname);
1409 } else {
1410 snprintf(devname, max_devname_len,
1411 "NAME=%s", p->gp_id);
1412 }
1413 break;
1414 case plain_name:
1415 assert(p->gp_flags & GPEF_WEDGE);
1416 if (with_path)
1417 snprintf(devname, max_devname_len, _PATH_DEV "%s",
1418 p->gp_dev_name);
1419 else
1420 strlcpy(devname, p->gp_dev_name, max_devname_len);
1421 break;
1422 case raw_dev_name:
1423 assert(p->gp_flags & GPEF_WEDGE);
1424 if (with_path)
1425 snprintf(devname, max_devname_len, _PATH_DEV "r%s",
1426 p->gp_dev_name);
1427 else
1428 snprintf(devname, max_devname_len, "r%s",
1429 p->gp_dev_name);
1430 break;
1431 default:
1432 return false;
1433 }
1434
1435 return true;
1436 }
1437
1438 static bool
1439 gpt_write_to_disk(struct disk_partitions *arg)
1440 {
1441 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1442 struct gpt_part_entry *p, *n;
1443 char label_arg[sizeof(p->gp_label) + 10];
1444 char diskpath[MAXPATHLEN];
1445 int fd, bits = 0;
1446 bool root_is_new = false, efi_is_new = false;
1447 part_id root_id = NO_PART, efi_id = NO_PART, pno;
1448
1449 /*
1450 * Remove all wedges on this disk - they may become invalid and we
1451 * have no easy way to associate them with the partitioning data.
1452 * Instead we will explicitly request creation of wedges on demand
1453 * later.
1454 */
1455 fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1456 if (fd < 0)
1457 return false;
1458 if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
1459 return false;
1460 close(fd);
1461
1462 /*
1463 * Collect first root and efi partition (if available), clear
1464 * "have wedge" flags.
1465 */
1466 for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
1467 p->gp_flags &= ~GPEF_WEDGE;
1468 if (root_id == NO_PART && p->gp_type != NULL) {
1469 if (p->gp_type->gent.generic_ptype == PT_root &&
1470 (p->gp_flags & GPEF_TARGET)) {
1471 root_id = pno;
1472 root_is_new = !(p->gp_flags & GPEF_ON_DISK);
1473 } else if (efi_id == NO_PART &&
1474 p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
1475 efi_id = pno;
1476 efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
1477 }
1478 }
1479 }
1480
1481 /*
1482 * If no GPT on disk yet, create it.
1483 */
1484 if (!parts->has_gpt) {
1485 char limit[30];
1486
1487 if (parts->max_num_parts > 0)
1488 sprintf(limit, "-p %zu", parts->max_num_parts);
1489 else
1490 limit[0] = 0;
1491 if (run_program(RUN_SILENT, "gpt create %s %s",
1492 limit, parts->dp.disk))
1493 return false;
1494 parts->has_gpt = true;
1495 }
1496
1497 /*
1498 * Delete all old partitions
1499 */
1500 for (p = parts->obsolete; p != NULL; p = n) {
1501 run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
1502 p->gp_start, arg->disk);
1503 n = p->gp_next;
1504 free(p);
1505 }
1506 parts->obsolete = NULL;
1507
1508 /*
1509 * Modify existing but changed partitions
1510 */
1511 for (p = parts->partitions; p != NULL; p = p->gp_next) {
1512 if (!(p->gp_flags & GPEF_ON_DISK))
1513 continue;
1514
1515 if (p->gp_flags & GPEF_RESIZED) {
1516 run_program(RUN_SILENT,
1517 "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
1518 p->gp_start, p->gp_size, arg->disk);
1519 p->gp_flags &= ~GPEF_RESIZED;
1520 }
1521
1522 if (!(p->gp_flags & GPEF_MODIFIED))
1523 continue;
1524
1525 if (!gpt_modify_part(parts->dp.disk, p))
1526 return false;
1527 }
1528
1529 /*
1530 * Add new partitions
1531 */
1532 for (p = parts->partitions; p != NULL; p = p->gp_next) {
1533 if (p->gp_flags & GPEF_ON_DISK)
1534 continue;
1535 if (!(p->gp_flags & GPEF_MODIFIED))
1536 continue;
1537
1538 if (p->gp_label[0] == 0)
1539 label_arg[0] = 0;
1540 else
1541 sprintf(label_arg, "-l \'%s\'", p->gp_label);
1542
1543 if (p->gp_type != NULL)
1544 run_program(RUN_SILENT,
1545 "gpt -n add -b %" PRIu64 " -s %" PRIu64
1546 "s -t %s %s %s",
1547 p->gp_start, p->gp_size, p->gp_type->tid,
1548 label_arg, arg->disk);
1549 else
1550 run_program(RUN_SILENT,
1551 "gpt -n add -b %" PRIu64 " -s %" PRIu64
1552 "s %s %s",
1553 p->gp_start, p->gp_size, label_arg, arg->disk);
1554 gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
1555 gpt_read_part(arg->disk, p->gp_start, p);
1556 p->gp_flags |= GPEF_ON_DISK;
1557 }
1558
1559 /*
1560 * Additional MD bootloader magic...
1561 */
1562 if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
1563 efi_is_new))
1564 return false;
1565
1566 return true;
1567 }
1568
1569 static part_id
1570 gpt_find_by_name(struct disk_partitions *arg, const char *name)
1571 {
1572 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1573 struct gpt_part_entry *p;
1574 part_id pno;
1575
1576 for (pno = 0, p = parts->partitions; p != NULL;
1577 p = p->gp_next, pno++) {
1578 if (strcmp(p->gp_label, name) == 0)
1579 return pno;
1580 if (strcmp(p->gp_id, name) == 0)
1581 return pno;
1582 }
1583
1584 return NO_PART;
1585 }
1586
1587 bool
1588 gpt_parts_check(void)
1589 {
1590
1591 check_available_binaries();
1592
1593 return have_gpt && have_dk;
1594 }
1595
1596 static void
1597 gpt_free(struct disk_partitions *arg)
1598 {
1599 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1600 struct gpt_part_entry *p, *n;
1601
1602 assert(parts != NULL);
1603 for (p = parts->partitions; p != NULL; p = n) {
1604 free(__UNCONST(p->last_mounted));
1605 n = p->gp_next;
1606 free(p);
1607 }
1608 free(__UNCONST(parts->dp.disk));
1609 free(parts);
1610 }
1611
1612 static bool
1613 gpt_custom_attribute_writable(const struct disk_partitions *arg,
1614 part_id ptn, size_t attr_no)
1615 {
1616 const struct gpt_disk_partitions *parts =
1617 (const struct gpt_disk_partitions*)arg;
1618 size_t i;
1619 struct gpt_part_entry *p;
1620
1621 if (attr_no >= arg->pscheme->custom_attribute_count)
1622 return false;
1623
1624 const msg label = arg->pscheme->custom_attributes[attr_no].label;
1625
1626 /* we can not edit the uuid attribute */
1627 if (label == MSG_ptn_uuid)
1628 return false;
1629
1630 /* the label is always editable */
1631 if (label == MSG_ptn_label)
1632 return true;
1633
1634 /* the GPT type is read only */
1635 if (label == MSG_ptn_gpt_type)
1636 return false;
1637
1638 /* BOOTME makes no sense on swap partitions */
1639 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1640 if (i == ptn)
1641 break;
1642
1643 if (p == NULL)
1644 return false;
1645
1646 if (p->fs_type == FS_SWAP ||
1647 (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
1648 return false;
1649
1650 return true;
1651 }
1652
1653 static const char *
1654 gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
1655 {
1656 const struct gpt_disk_partitions *parts =
1657 (const struct gpt_disk_partitions*)arg;
1658 size_t i;
1659 struct gpt_part_entry *p;
1660
1661 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1662 if (i == ptn)
1663 break;
1664
1665 if (p == NULL)
1666 return NULL;
1667
1668 if (p->gp_label[0] != 0)
1669 return p->gp_label;
1670 return p->gp_id;
1671 }
1672
1673 static bool
1674 gpt_format_custom_attribute(const struct disk_partitions *arg,
1675 part_id ptn, size_t attr_no, const struct disk_part_info *info,
1676 char *out, size_t out_space)
1677 {
1678 const struct gpt_disk_partitions *parts =
1679 (const struct gpt_disk_partitions*)arg;
1680 size_t i;
1681 struct gpt_part_entry *p, data;
1682
1683 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1684 if (i == ptn)
1685 break;
1686
1687 if (p == NULL)
1688 return false;
1689
1690 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1691 return false;
1692
1693 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1694
1695 if (info != NULL) {
1696 data = *p;
1697 gpt_info_to_part(&data, info, NULL);
1698 p = &data;
1699 }
1700
1701 if (label == MSG_ptn_label)
1702 strlcpy(out, p->gp_label, out_space);
1703 else if (label == MSG_ptn_uuid)
1704 strlcpy(out, p->gp_id, out_space);
1705 else if (label == MSG_ptn_gpt_type) {
1706 if (p->gp_type != NULL)
1707 strlcpy(out, p->gp_type->gent.description, out_space);
1708 else if (out_space > 1)
1709 out[0] = 0;
1710 } else if (label == MSG_ptn_boot)
1711 strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
1712 MSG_Yes : MSG_No), out_space);
1713 else
1714 return false;
1715
1716 return true;
1717 }
1718
1719 static bool
1720 gpt_custom_attribute_toggle(struct disk_partitions *arg,
1721 part_id ptn, size_t attr_no)
1722 {
1723 const struct gpt_disk_partitions *parts =
1724 (const struct gpt_disk_partitions*)arg;
1725 size_t i;
1726 struct gpt_part_entry *p;
1727
1728 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1729 if (i == ptn)
1730 break;
1731
1732 if (p == NULL)
1733 return false;
1734
1735 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1736 return false;
1737
1738 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1739 if (label != MSG_ptn_boot)
1740 return false;
1741
1742 if (p->gp_attr & GPT_ATTR_BOOT) {
1743 p->gp_attr &= ~GPT_ATTR_BOOT;
1744 } else {
1745 for (i = 0, p = parts->partitions; p != NULL;
1746 i++, p = p->gp_next)
1747 if (i == ptn)
1748 p->gp_attr |= GPT_ATTR_BOOT;
1749 else
1750 p->gp_attr &= ~GPT_ATTR_BOOT;
1751 }
1752 return true;
1753 }
1754
1755 static bool
1756 gpt_custom_attribute_set_str(struct disk_partitions *arg,
1757 part_id ptn, size_t attr_no, const char *new_val)
1758 {
1759 const struct gpt_disk_partitions *parts =
1760 (const struct gpt_disk_partitions*)arg;
1761 size_t i;
1762 struct gpt_part_entry *p;
1763
1764 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1765 if (i == ptn)
1766 break;
1767
1768 if (p == NULL)
1769 return false;
1770
1771 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1772 return false;
1773
1774 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1775
1776 if (label != MSG_ptn_label)
1777 return false;
1778
1779 strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
1780 return true;
1781 }
1782
1783 static bool
1784 gpt_have_boot_support(const char *disk)
1785 {
1786 #ifdef HAVE_GPT_BOOT
1787 return true;
1788 #else
1789 return false;
1790 #endif
1791 }
1792
1793 const struct disk_part_custom_attribute gpt_custom_attrs[] = {
1794 { .label = MSG_ptn_label, .type = pet_str },
1795 { .label = MSG_ptn_uuid, .type = pet_str },
1796 { .label = MSG_ptn_gpt_type, .type = pet_str },
1797 { .label = MSG_ptn_boot, .type = pet_bool },
1798 };
1799
1800 const struct disk_partitioning_scheme
1801 gpt_parts = {
1802 .name = MSG_parttype_gpt,
1803 .short_name = MSG_parttype_gpt_short,
1804 .part_flag_desc = MSG_gpt_flag_desc,
1805 .custom_attribute_count = __arraycount(gpt_custom_attrs),
1806 .custom_attributes = gpt_custom_attrs,
1807 .get_part_types_count = gpt_type_count,
1808 .get_part_type = gpt_get_ptype,
1809 .get_generic_part_type = gpt_get_generic_type,
1810 .get_fs_part_type = gpt_get_fs_part_type,
1811 .get_default_fstype = gpt_get_default_fstype,
1812 .create_custom_part_type = gpt_create_custom_part_type,
1813 .create_unknown_part_type = gpt_create_unknown_part_type,
1814 .get_part_alignment = gpt_get_part_alignment,
1815 .read_from_disk = gpt_read_from_disk,
1816 .get_cylinder_size = gpt_cyl_size,
1817 .create_new_for_disk = gpt_create_new,
1818 .have_boot_support = gpt_have_boot_support,
1819 .find_by_name = gpt_find_by_name,
1820 .can_add_partition = gpt_can_add_partition,
1821 .custom_attribute_writable = gpt_custom_attribute_writable,
1822 .format_custom_attribute = gpt_format_custom_attribute,
1823 .custom_attribute_toggle = gpt_custom_attribute_toggle,
1824 .custom_attribute_set_str = gpt_custom_attribute_set_str,
1825 .other_partition_identifier = gpt_get_label_str,
1826 .get_part_device = gpt_get_part_device,
1827 .max_free_space_at = gpt_max_free_space_at,
1828 .get_free_spaces = gpt_get_free_spaces,
1829 .adapt_foreign_part_info = generic_adapt_foreign_part_info,
1830 .get_part_info = gpt_get_part_info,
1831 .get_part_attr_str = gpt_get_part_attr_str,
1832 .set_part_info = gpt_set_part_info,
1833 .add_partition = gpt_add_part,
1834 .delete_all_partitions = gpt_delete_all_partitions,
1835 .delete_partition = gpt_delete_partition,
1836 .write_to_disk = gpt_write_to_disk,
1837 .free = gpt_free,
1838 .cleanup = gpt_cleanup,
1839 };
1840