gpt.c revision 1.6.2.10 1 /* $NetBSD: gpt.c,v 1.6.2.10 2022/02/02 04:25:36 msaitoh 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 DISKNAMESIZE /* 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 };
117
118 static size_t gpt_ptype_cnt = 0, gpt_ptype_alloc = 0;
119 static struct gpt_ptype_desc *gpt_ptype_descs = NULL;
120
121 /* "well" known types with special handling */
122 static const struct part_type_desc *gpt_native_root;
123
124 /* similar to struct gpt_ent, but matching our needs */
125 struct gpt_part_entry {
126 const struct gpt_ptype_desc *gp_type;
127 char gp_id[GUID_STR_LEN]; /* partition guid as string */
128 daddr_t gp_start, gp_size;
129 uint gp_attr; /* various attribute bits */
130 char gp_label[GPT_LABEL_LEN]; /* user defined label */
131 char gp_dev_name[GPT_DEV_LEN]; /* name of wedge */
132 const char *last_mounted; /* last mounted if known */
133 uint fs_type, fs_sub_type, /* FS_* and maybe sub type */
134 fs_opt1, fs_opt2, fs_opt3; /* transient file system options */
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 info->fs_opt1 = p->fs_opt1;
522 info->fs_opt2 = p->fs_opt2;
523 info->fs_opt3 = p->fs_opt3;
524 if (p->gp_flags & GPEF_TARGET)
525 info->flags |= PTI_INSTALL_TARGET;
526
527 return true;
528 }
529
530 static bool
531 gpt_get_part_attr_str(const struct disk_partitions *arg, part_id id,
532 char *str, size_t avail_space)
533 {
534 const struct gpt_disk_partitions *parts =
535 (const struct gpt_disk_partitions*)arg;
536 const struct gpt_part_entry *p = parts->partitions;
537 part_id no;
538 static const char *flags = NULL;
539
540 for (no = 0; p != NULL && no < id; no++)
541 p = p->gp_next;
542
543 if (no != id || p == NULL)
544 return false;
545
546 if (flags == NULL)
547 flags = msg_string(MSG_gpt_flags);
548
549 if (avail_space < 2)
550 return false;
551
552 if (p->gp_attr & GPT_ATTR_BOOT)
553 *str++ = flags[0];
554 *str = 0;
555
556 return true;
557 }
558
559 /*
560 * Find insert position and check for duplicates.
561 * If all goes well, insert the new "entry" in the "list".
562 * If there are collisions, report "no free space".
563 * We keep all lists sorted by start sector number,
564 */
565 static bool
566 gpt_insert_part_into_list(struct gpt_disk_partitions *parts,
567 struct gpt_part_entry **list,
568 struct gpt_part_entry *entry, const char **err_msg)
569 {
570 struct gpt_part_entry *p, *last;
571
572 /* find the first entry past the new one (if any) */
573 for (last = NULL, p = *list; p != NULL; last = p, p = p->gp_next) {
574 if (p->gp_start > entry->gp_start)
575 break;
576 }
577
578 /* check if last partition overlaps with new one */
579 if (last) {
580 if (last->gp_start + last->gp_size > entry->gp_start) {
581 if (err_msg)
582 *err_msg = msg_string(MSG_No_free_space);
583 return false;
584 }
585 }
586
587 if (p == NULL) {
588 entry->gp_next = NULL;
589 if (last != NULL) {
590 last->gp_next = entry;
591 }
592 } else {
593 /* check if new entry overlaps with next */
594 if (entry->gp_start + entry->gp_size > p->gp_start) {
595 if (err_msg)
596 *err_msg = msg_string(MSG_No_free_space);
597 return false;
598 }
599
600 entry->gp_next = p;
601 if (last != NULL)
602 last->gp_next = entry;
603 else
604 *list = entry;
605 }
606 if (*list == NULL)
607 *list = entry;
608
609 return true;
610 }
611
612 static bool
613 gpt_set_part_info(struct disk_partitions *arg, part_id id,
614 const struct disk_part_info *info, const char **err_msg)
615 {
616 struct gpt_disk_partitions *parts =
617 (struct gpt_disk_partitions*)arg;
618 struct gpt_part_entry *p = parts->partitions, *n;
619 part_id no;
620 daddr_t lendiff;
621 bool was_target;
622
623 for (no = 0; p != NULL && no < id; no++)
624 p = p->gp_next;
625
626 if (no != id || p == NULL)
627 return false;
628
629 /* update target mark - we can only have one */
630 was_target = (p->gp_flags & GPEF_TARGET) != 0;
631 if (info->flags & PTI_INSTALL_TARGET)
632 p->gp_flags |= GPEF_TARGET;
633 else
634 p->gp_flags &= ~GPEF_TARGET;
635 if (was_target)
636 for (n = parts->partitions; n != NULL; n = n->gp_next)
637 if (n != p)
638 n->gp_flags &= ~GPEF_TARGET;
639
640 if ((p->gp_flags & GPEF_ON_DISK)) {
641 if (info->start != p->gp_start) {
642 /* partition moved, we need to delete and re-add */
643 n = calloc(1, sizeof(*n));
644 if (n == NULL) {
645 if (err_msg)
646 *err_msg = err_outofmem;
647 return false;
648 }
649 *n = *p;
650 p->gp_flags &= ~GPEF_ON_DISK;
651 if (!gpt_insert_part_into_list(parts, &parts->obsolete,
652 n, err_msg))
653 return false;
654 } else if (info->size != p->gp_size) {
655 p->gp_flags |= GPEF_RESIZED;
656 }
657 }
658
659 p->gp_flags |= GPEF_MODIFIED;
660
661 lendiff = info->size - p->gp_size;
662 parts->dp.free_space -= lendiff;
663 return gpt_info_to_part(p, info, err_msg);
664 }
665
666 static size_t
667 gpt_get_free_spaces_internal(const struct gpt_disk_partitions *parts,
668 struct disk_part_free_space *result, size_t max_num_result,
669 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
670 {
671 size_t cnt = 0;
672 daddr_t s, e, from, size, end_of_disk;
673 struct gpt_part_entry *p;
674
675 if (align > 1)
676 start = max(roundup(start, align), align);
677 if (start < 0 || start < (daddr_t)parts->prologue)
678 start = parts->prologue;
679 if (parts->dp.disk_start != 0 && parts->dp.disk_start > start)
680 start = parts->dp.disk_start;
681 if (min_space_size < 1)
682 min_space_size = 1;
683 end_of_disk = parts->dp.disk_start + parts->dp.disk_size
684 - parts->epilogue;
685 from = start;
686 while (from < end_of_disk && cnt < max_num_result) {
687 again:
688 size = parts->dp.disk_start + parts->dp.disk_size - from;
689 start = from;
690 if (start + size > end_of_disk)
691 size = end_of_disk - start;
692 for (p = parts->partitions; p != NULL; p = p->gp_next) {
693 s = p->gp_start;
694 e = p->gp_size + s;
695 if (s == ignore)
696 continue;
697 if (e < from)
698 continue;
699 if (s <= from && e > from) {
700 if (e - 1 >= end_of_disk)
701 return cnt;
702 from = e + 1;
703 if (align > 1) {
704 from = max(roundup(from, align), align);
705 if (from >= end_of_disk) {
706 size = 0;
707 break;
708 }
709 }
710 goto again;
711 }
712 if (s > from && s - from < size) {
713 size = s - from;
714 }
715 }
716 if (size >= min_space_size) {
717 result->start = start;
718 result->size = size;
719 result++;
720 cnt++;
721 }
722 from += size + 1;
723 if (align > 1)
724 from = max(roundup(from, align), align);
725 }
726
727 return cnt;
728 }
729
730 static daddr_t
731 gpt_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
732 {
733 const struct gpt_disk_partitions *parts =
734 (const struct gpt_disk_partitions*)arg;
735 struct disk_part_free_space space;
736
737 if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 0,
738 start, start) == 1)
739 return space.size;
740
741 return 0;
742 }
743
744 static size_t
745 gpt_get_free_spaces(const struct disk_partitions *arg,
746 struct disk_part_free_space *result, size_t max_num_result,
747 daddr_t min_space_size, daddr_t align, daddr_t start,
748 daddr_t ignore)
749 {
750 const struct gpt_disk_partitions *parts =
751 (const struct gpt_disk_partitions*)arg;
752
753 return gpt_get_free_spaces_internal(parts, result,
754 max_num_result, min_space_size, align, start, ignore);
755 }
756
757 static void
758 gpt_match_ptype(const char *name, struct gpt_ptype_desc *t)
759 {
760 size_t i;
761
762 for (i = 0; i < __arraycount(gpt_fs_types); i++) {
763 if (strcmp(name, gpt_fs_types[i].name) == 0) {
764 t->gent.generic_ptype = gpt_fs_types[i].ptype;
765 t->fsflags = gpt_fs_types[i].fsflags;
766 t->default_fs_type = gpt_fs_types[i].fstype;
767
768 /* recongnize special entries */
769 if (gpt_native_root == NULL && i == 0)
770 gpt_native_root = &t->gent;
771
772 return;
773 }
774 }
775
776 t->gent.generic_ptype = PT_unknown;
777 t->fsflags = 0;
778 t->default_fs_type = FS_BSDFFS;
779 }
780
781 static void
782 gpt_internal_add_ptype(const char *uid, const char *name, const char *desc)
783 {
784 if (gpt_ptype_cnt >= gpt_ptype_alloc) {
785 gpt_ptype_alloc = gpt_ptype_alloc ? 2*gpt_ptype_alloc
786 : GPT_PTYPE_ALLOC;
787 struct gpt_ptype_desc *nptypes = realloc(gpt_ptype_descs,
788 gpt_ptype_alloc*sizeof(*gpt_ptype_descs));
789 if (nptypes == 0)
790 errx(EXIT_FAILURE, "out of memory");
791 gpt_ptype_descs = nptypes;
792 }
793
794 strlcpy(gpt_ptype_descs[gpt_ptype_cnt].tid, uid,
795 sizeof(gpt_ptype_descs[gpt_ptype_cnt].tid));
796 gpt_ptype_descs[gpt_ptype_cnt].gent.short_desc = strdup(name);
797 gpt_ptype_descs[gpt_ptype_cnt].gent.description = strdup(desc);
798 gpt_match_ptype(name, &gpt_ptype_descs[gpt_ptype_cnt]);
799 gpt_ptype_cnt++;
800 }
801
802 static void
803 gpt_init_ptypes(void)
804 {
805 if (gpt_ptype_cnt == 0)
806 gpt_uuid_query(gpt_internal_add_ptype);
807 }
808
809 static void
810 gpt_cleanup(void)
811 {
812 /* free all of gpt_ptype_descs */
813 for (size_t i = 0; i < gpt_ptype_cnt; i++) {
814 free(__UNCONST(gpt_ptype_descs[i].gent.short_desc));
815 free(__UNCONST(gpt_ptype_descs[i].gent.description));
816 }
817 free(gpt_ptype_descs);
818 gpt_ptype_descs = NULL;
819 gpt_ptype_cnt = gpt_ptype_alloc = 0;
820 }
821
822 static size_t
823 gpt_type_count(void)
824 {
825 if (gpt_ptype_cnt == 0)
826 gpt_init_ptypes();
827
828 return gpt_ptype_cnt;
829 }
830
831 static const struct part_type_desc *
832 gpt_get_ptype(size_t ndx)
833 {
834 if (gpt_ptype_cnt == 0)
835 gpt_init_ptypes();
836
837 if (ndx >= gpt_ptype_cnt)
838 return NULL;
839
840 return &gpt_ptype_descs[ndx].gent;
841 }
842
843 static const struct part_type_desc *
844 gpt_get_generic_type(enum part_type gent)
845 {
846 if (gpt_ptype_cnt == 0)
847 gpt_init_ptypes();
848
849 if (gent == PT_root)
850 return gpt_native_root;
851 if (gent == PT_unknown)
852 return NULL;
853
854 for (size_t i = 0; i < gpt_ptype_cnt; i++)
855 if (gpt_ptype_descs[i].gent.generic_ptype == gent)
856 return &gpt_ptype_descs[i].gent;
857
858 return NULL;
859 }
860
861 static const struct gpt_ptype_desc *
862 gpt_find_native_type(const struct part_type_desc *gent)
863 {
864 if (gpt_ptype_cnt == 0)
865 gpt_init_ptypes();
866
867 if (gent == NULL)
868 return NULL;
869
870 for (size_t i = 0; i < gpt_ptype_cnt; i++)
871 if (gent == &gpt_ptype_descs[i].gent)
872 return &gpt_ptype_descs[i];
873
874 gent = gpt_get_generic_type(gent->generic_ptype);
875 if (gent == NULL)
876 return NULL;
877
878 /* this can not recurse deeper than once, we would not have found a
879 * generic type a few lines above if it would. */
880 return gpt_find_native_type(gent);
881 }
882
883 static const struct gpt_ptype_desc *
884 gpt_find_guid_type(const char *uid)
885 {
886 if (gpt_ptype_cnt == 0)
887 gpt_init_ptypes();
888
889 if (uid == NULL || uid[0] == 0)
890 return NULL;
891
892 for (size_t i = 0; i < gpt_ptype_cnt; i++)
893 if (strcmp(gpt_ptype_descs[i].tid, uid) == 0)
894 return &gpt_ptype_descs[i];
895
896 return NULL;
897 }
898
899 static const struct part_type_desc *
900 gpt_find_type(const char *desc)
901 {
902 if (gpt_ptype_cnt == 0)
903 gpt_init_ptypes();
904
905 if (desc == NULL || desc[0] == 0)
906 return NULL;
907
908 for (size_t i = 0; i < gpt_ptype_cnt; i++)
909 if (strcmp(gpt_ptype_descs[i].gent.short_desc, desc) == 0)
910 return &gpt_ptype_descs[i].gent;
911
912 return NULL;
913 }
914
915 static const struct part_type_desc *
916 gpt_get_fs_part_type(enum part_type pt, unsigned fstype, unsigned fs_sub_type)
917 {
918 size_t i;
919
920 /* Try with complete match (including part_type) first */
921 for (i = 0; i < __arraycount(gpt_fs_types); i++)
922 if (fstype == gpt_fs_types[i].fstype &&
923 pt == gpt_fs_types[i].ptype)
924 return gpt_find_type(gpt_fs_types[i].name);
925
926 /* If that did not work, ignore part_type */
927 for (i = 0; i < __arraycount(gpt_fs_types); i++)
928 if (fstype == gpt_fs_types[i].fstype)
929 return gpt_find_type(gpt_fs_types[i].name);
930
931 return NULL;
932 }
933
934 static bool
935 gpt_get_default_fstype(const struct part_type_desc *nat_type,
936 unsigned *fstype, unsigned *fs_sub_type)
937 {
938 const struct gpt_ptype_desc *gtype;
939
940 gtype = gpt_find_native_type(nat_type);
941 if (gtype == NULL)
942 return false;
943
944 *fstype = gtype->default_fs_type;
945 #ifdef DEFAULT_UFS2
946 if (gtype->default_fs_type == FS_BSDFFS)
947 *fs_sub_type = 2;
948 else
949 #endif
950 *fs_sub_type = 0;
951 return true;
952 }
953
954 static const struct part_type_desc *
955 gpt_get_uuid_part_type(const uuid_t *id)
956 {
957 char str[GUID_STR_LEN], desc[GUID_STR_LEN + MENUSTRSIZE];
958 const struct gpt_ptype_desc *t;
959 char *guid = NULL;
960 uint32_t err;
961
962 uuid_to_string(id, &guid, &err);
963 strlcpy(str, err == uuid_s_ok ? guid : "-", sizeof str);
964 free(guid);
965
966 t = gpt_find_guid_type(str);
967 if (t == NULL) {
968 snprintf(desc, sizeof desc, "%s (%s)",
969 msg_string(MSG_custom_type), str);
970 gpt_internal_add_ptype(str, str, desc);
971 t = gpt_find_guid_type(str);
972 assert(t != NULL);
973 }
974 return &t->gent;
975 }
976
977 static const struct part_type_desc *
978 gpt_create_custom_part_type(const char *custom, const char **err_msg)
979 {
980 uuid_t id;
981 uint32_t err;
982
983 uuid_from_string(custom, &id, &err);
984 if (err_msg != NULL &&
985 (err == uuid_s_invalid_string_uuid || err == uuid_s_bad_version)) {
986 *err_msg = MSG_invalid_guid;
987 return NULL;
988 }
989 if (err != uuid_s_ok)
990 return NULL;
991
992 return gpt_get_uuid_part_type(&id);
993 }
994
995 static const struct part_type_desc *
996 gpt_create_unknown_part_type(void)
997 {
998 uuid_t id;
999 uint32_t err;
1000
1001 uuid_create(&id, &err);
1002 if (err != uuid_s_ok)
1003 return NULL;
1004
1005 return gpt_get_uuid_part_type(&id);
1006 }
1007
1008 static daddr_t
1009 gpt_get_part_alignment(const struct disk_partitions *parts)
1010 {
1011
1012 assert(parts->disk_size > 0);
1013 if (parts->disk_size < 0)
1014 return 1;
1015
1016 /* Use 1MB offset/alignemnt for large (>128GB) disks */
1017 if (parts->disk_size > HUGE_DISK_SIZE)
1018 return 2048;
1019 else if (parts->disk_size > TINY_DISK_SIZE)
1020 return 64;
1021 else
1022 return 4;
1023 }
1024
1025 static bool
1026 gpt_can_add_partition(const struct disk_partitions *arg)
1027 {
1028 const struct gpt_disk_partitions *parts =
1029 (const struct gpt_disk_partitions*)arg;
1030 struct disk_part_free_space space;
1031 daddr_t align;
1032
1033 if (parts->dp.num_part >= parts->max_num_parts)
1034 return false;
1035
1036 align = gpt_get_part_alignment(arg);
1037 if (parts->dp.free_space <= align)
1038 return false;
1039
1040 if (gpt_get_free_spaces_internal(parts, &space, 1, align, align,
1041 0, -1) < 1)
1042 return false;
1043
1044 return true;
1045 }
1046
1047 static bool
1048 gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
1049 const char **err_msg)
1050 {
1051 p->gp_type = gpt_find_native_type(info->nat_type);
1052 p->gp_start = info->start;
1053 p->gp_size = info->size;
1054 if (info->last_mounted != NULL && info->last_mounted !=
1055 p->last_mounted) {
1056 free(__UNCONST(p->last_mounted));
1057 p->last_mounted = strdup(info->last_mounted);
1058 }
1059 p->fs_type = info->fs_type;
1060 p->fs_sub_type = info->fs_sub_type;
1061 p->fs_opt1 = info->fs_opt1;
1062 p->fs_opt2 = info->fs_opt2;
1063 p->fs_opt3 = info->fs_opt3;
1064
1065 return true;
1066 }
1067
1068 static part_id
1069 gpt_add_part(struct disk_partitions *arg,
1070 const struct disk_part_info *info, const char **err_msg)
1071 {
1072 struct gpt_disk_partitions *parts =
1073 (struct gpt_disk_partitions*)arg;
1074 struct disk_part_free_space space;
1075 struct disk_part_info data = *info;
1076 struct gpt_part_entry *p;
1077 bool ok;
1078
1079 if (err_msg != NULL)
1080 *err_msg = NULL;
1081
1082 if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 1,
1083 info->start, -1) < 1) {
1084 if (err_msg)
1085 *err_msg = msg_string(MSG_No_free_space);
1086 return NO_PART;
1087 }
1088 if (parts->dp.num_part >= parts->max_num_parts) {
1089 if (err_msg)
1090 *err_msg = msg_string(MSG_err_too_many_partitions);
1091 return NO_PART;
1092 }
1093
1094 if (data.size > space.size)
1095 data.size = space.size;
1096
1097 p = calloc(1, sizeof(*p));
1098 if (p == NULL) {
1099 if (err_msg != NULL)
1100 *err_msg = INTERNAL_ERROR;
1101 return NO_PART;
1102 }
1103 if (!gpt_info_to_part(p, &data, err_msg)) {
1104 free(p);
1105 return NO_PART;
1106 }
1107 p->gp_flags |= GPEF_MODIFIED;
1108 ok = gpt_insert_part_into_list(parts, &parts->partitions, p, err_msg);
1109 if (ok) {
1110 parts->dp.num_part++;
1111 parts->dp.free_space -= p->gp_size;
1112 return parts->dp.num_part-1;
1113 } else {
1114 free(p);
1115 return NO_PART;
1116 }
1117 }
1118
1119 static bool
1120 gpt_delete_partition(struct disk_partitions *arg, part_id id,
1121 const char **err_msg)
1122 {
1123 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1124 struct gpt_part_entry *p, *last = NULL;
1125 part_id i;
1126 bool res;
1127
1128 if (parts->dp.num_part == 0)
1129 return false;
1130
1131 for (i = 0, p = parts->partitions;
1132 i != id && i < parts->dp.num_part && p != NULL;
1133 i++, p = p->gp_next)
1134 last = p;
1135
1136 if (p == NULL) {
1137 if (err_msg)
1138 *err_msg = INTERNAL_ERROR;
1139 return false;
1140 }
1141
1142 if (last == NULL)
1143 parts->partitions = p->gp_next;
1144 else
1145 last->gp_next = p->gp_next;
1146
1147 res = true;
1148 if (p->gp_flags & GPEF_ON_DISK) {
1149 if (!gpt_insert_part_into_list(parts, &parts->obsolete,
1150 p, err_msg))
1151 res = false;
1152 } else {
1153 free(p);
1154 }
1155
1156 if (res) {
1157 parts->dp.num_part--;
1158 parts->dp.free_space += p->gp_size;
1159 }
1160
1161 return res;
1162 }
1163
1164 static bool
1165 gpt_delete_all_partitions(struct disk_partitions *arg)
1166 {
1167 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1168
1169 while (parts->dp.num_part > 0) {
1170 if (!gpt_delete_partition(&parts->dp, 0, NULL))
1171 return false;
1172 }
1173
1174 return true;
1175 }
1176
1177 static bool
1178 gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
1179 {
1180 char *textbuf, *t, *tt;
1181 static const char expected_hdr[] = "Details for index ";
1182
1183 /* run gpt show for this partition */
1184 if (collect(T_OUTPUT, &textbuf,
1185 "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
1186 return false;
1187
1188 /*
1189 * gpt show should respond with single partition details, but will
1190 * fall back to "show -a" output if something is wrong
1191 */
1192 t = strtok(textbuf, "\n"); /* first line is special */
1193 if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
1194 free(textbuf);
1195 return false;
1196 }
1197
1198 /* parse output into "old" */
1199 while ((t = strtok(NULL, "\n")) != NULL) {
1200 tt = strsep(&t, " \t");
1201 if (strlen(tt) == 0)
1202 continue;
1203 gpt_add_info(p, tt, t, true);
1204 }
1205 free(textbuf);
1206
1207 return true;
1208 }
1209
1210 static bool
1211 gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
1212 {
1213 size_t i;
1214 char attr_str[STRSIZE];
1215
1216 if (todo == 0)
1217 return true;
1218
1219 strcpy(attr_str, "-a ");
1220 for (i = 0; todo != 0; i++) {
1221 if (!(gpt_avail_attrs[i].flag & todo))
1222 continue;
1223 todo &= ~gpt_avail_attrs[i].flag;
1224 if (attr_str[0])
1225 strlcat(attr_str, ",",
1226 sizeof(attr_str));
1227 strlcat(attr_str,
1228 gpt_avail_attrs[i].name,
1229 sizeof(attr_str));
1230 }
1231 if (run_program(RUN_SILENT,
1232 "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
1233 return false;
1234 return true;
1235 }
1236
1237 /*
1238 * Modify an existing on-disk partition.
1239 * Start and size can not be changed here, caller needs to deal
1240 * with that kind of changes upfront.
1241 */
1242 static bool
1243 gpt_modify_part(const char *disk, struct gpt_part_entry *p)
1244 {
1245 struct gpt_part_entry old;
1246 uint todo_set, todo_unset;
1247
1248 /*
1249 * Query current on-disk state
1250 */
1251 memset(&old, 0, sizeof old);
1252 if (!gpt_read_part(disk, p->gp_start, &old))
1253 return false;
1254
1255 /* Reject unsupported changes */
1256 if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
1257 return false;
1258
1259 /*
1260 * GUID should never change, but the internal copy
1261 * may not yet know it.
1262 */
1263 strcpy(p->gp_id, old.gp_id);
1264
1265 /* Check type */
1266 if (p->gp_type != old.gp_type) {
1267 if (run_program(RUN_SILENT,
1268 "gpt type -b %" PRIu64 " -T %s %s",
1269 p->gp_start, p->gp_type->tid, disk) != 0)
1270 return false;
1271 }
1272
1273 /* Check label */
1274 if (strcmp(p->gp_label, old.gp_label) != 0) {
1275 if (run_program(RUN_SILENT,
1276 "gpt label -b %" PRIu64 " -l \'%s\' %s",
1277 p->gp_start, p->gp_label, disk) != 0)
1278 return false;
1279 }
1280
1281 /* Check attributes */
1282 if (p->gp_attr != old.gp_attr) {
1283 if (p->gp_attr == 0) {
1284 if (run_program(RUN_SILENT,
1285 "gpt set -N -b %" PRIu64 " %s",
1286 p->gp_start, disk) != 0)
1287 return false;
1288 } else {
1289 todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
1290 todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
1291 if (!gpt_apply_attr(disk, "unset", p->gp_start,
1292 todo_unset))
1293 return false;
1294 if (!gpt_apply_attr(disk, "set", p->gp_start,
1295 todo_set))
1296 return false;
1297 }
1298 }
1299
1300 return true;
1301 }
1302
1303 /*
1304 * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
1305 * map FS_* to wedge strings
1306 */
1307 static const char *
1308 bsdlabel_fstype_to_str(uint8_t fstype)
1309 {
1310 const char *str;
1311
1312 /*
1313 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
1314 * a suitable case branch will convert the type number to a string.
1315 */
1316 switch (fstype) {
1317 #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
1318 case __CONCAT(FS_,tag): str = __CONCAT(DKW_PTYPE_,tag); break;
1319 FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
1320 #undef FSTYPE_TO_STR_CASE
1321 default: str = NULL; break;
1322 }
1323
1324 return (str);
1325 }
1326
1327 static bool
1328 gpt_add_wedge(const char *disk, struct gpt_part_entry *p)
1329 {
1330 struct dkwedge_info dkw;
1331 const char *tname;
1332 char diskpath[MAXPATHLEN];
1333 int fd;
1334
1335 memset(&dkw, 0, sizeof(dkw));
1336 tname = bsdlabel_fstype_to_str(p->fs_type);
1337 if (tname)
1338 strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
1339
1340 strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
1341 dkw.dkw_offset = p->gp_start;
1342 dkw.dkw_size = p->gp_size;
1343 if (dkw.dkw_wname[0] == 0) {
1344 if (p->gp_label[0] != 0)
1345 strlcpy((char*)&dkw.dkw_wname,
1346 p->gp_label, sizeof(dkw.dkw_wname));
1347 }
1348 if (dkw.dkw_wname[0] == 0) {
1349 snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
1350 "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
1351 }
1352
1353 fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1354 if (fd < 0)
1355 return false;
1356 if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
1357 close(fd);
1358 return false;
1359 }
1360 close(fd);
1361
1362 strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
1363 p->gp_flags |= GPEF_WEDGE;
1364 return true;
1365 }
1366
1367 static void
1368 escape_spaces(char *dest, const char *src)
1369 {
1370 unsigned char c;
1371
1372 while (*src) {
1373 c = *src++;
1374 if (isspace(c) || c == '\\')
1375 *dest++ = '\\';
1376 *dest++ = c;
1377 }
1378 *dest = 0;
1379 }
1380
1381 static bool
1382 gpt_get_part_device(const struct disk_partitions *arg,
1383 part_id id, char *devname, size_t max_devname_len, int *part,
1384 enum dev_name_usage usage, bool with_path, bool life)
1385 {
1386 const struct gpt_disk_partitions *parts =
1387 (const struct gpt_disk_partitions*)arg;
1388 struct gpt_part_entry *p = parts->partitions;
1389 char tmpname[GPT_LABEL_LEN*2];
1390 part_id no;
1391
1392
1393 for (no = 0; p != NULL && no < id; no++)
1394 p = p->gp_next;
1395
1396 if (no != id || p == NULL)
1397 return false;
1398
1399 if (part)
1400 *part = -1;
1401
1402 if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
1403 usage = plain_name;
1404 if (usage == plain_name || usage == raw_dev_name)
1405 life = true;
1406 if (!(p->gp_flags & GPEF_WEDGE) && life)
1407 gpt_add_wedge(arg->disk, p);
1408
1409 switch (usage) {
1410 case logical_name:
1411 if (p->gp_label[0] != 0) {
1412 escape_spaces(tmpname, p->gp_label);
1413 snprintf(devname, max_devname_len,
1414 "NAME=%s", tmpname);
1415 } else {
1416 snprintf(devname, max_devname_len,
1417 "NAME=%s", p->gp_id);
1418 }
1419 break;
1420 case plain_name:
1421 assert(p->gp_flags & GPEF_WEDGE);
1422 if (with_path)
1423 snprintf(devname, max_devname_len, _PATH_DEV "%s",
1424 p->gp_dev_name);
1425 else
1426 strlcpy(devname, p->gp_dev_name, max_devname_len);
1427 break;
1428 case raw_dev_name:
1429 assert(p->gp_flags & GPEF_WEDGE);
1430 if (with_path)
1431 snprintf(devname, max_devname_len, _PATH_DEV "r%s",
1432 p->gp_dev_name);
1433 else
1434 snprintf(devname, max_devname_len, "r%s",
1435 p->gp_dev_name);
1436 break;
1437 default:
1438 return false;
1439 }
1440
1441 return true;
1442 }
1443
1444 static bool
1445 gpt_write_to_disk(struct disk_partitions *arg)
1446 {
1447 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1448 struct gpt_part_entry *p, *n;
1449 char label_arg[sizeof(p->gp_label) + 10];
1450 char diskpath[MAXPATHLEN];
1451 int fd, bits = 0;
1452 bool root_is_new = false, efi_is_new = false;
1453 part_id root_id = NO_PART, efi_id = NO_PART, pno;
1454
1455 /*
1456 * Remove all wedges on this disk - they may become invalid and we
1457 * have no easy way to associate them with the partitioning data.
1458 * Instead we will explicitly request creation of wedges on demand
1459 * later.
1460 */
1461 fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1462 if (fd < 0)
1463 return false;
1464 if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
1465 return false;
1466 close(fd);
1467
1468 /*
1469 * Collect first root and efi partition (if available), clear
1470 * "have wedge" flags.
1471 */
1472 for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
1473 p->gp_flags &= ~GPEF_WEDGE;
1474 if (root_id == NO_PART && p->gp_type != NULL) {
1475 if (p->gp_type->gent.generic_ptype == PT_root &&
1476 (p->gp_flags & GPEF_TARGET)) {
1477 root_id = pno;
1478 root_is_new = !(p->gp_flags & GPEF_ON_DISK);
1479 } else if (efi_id == NO_PART &&
1480 p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
1481 efi_id = pno;
1482 efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
1483 }
1484 }
1485 }
1486
1487 /*
1488 * If no GPT on disk yet, create it.
1489 */
1490 if (!parts->has_gpt) {
1491 char limit[30];
1492
1493 if (parts->max_num_parts > 0)
1494 sprintf(limit, "-p %zu", parts->max_num_parts);
1495 else
1496 limit[0] = 0;
1497 if (run_program(RUN_SILENT, "gpt create %s %s",
1498 limit, parts->dp.disk))
1499 return false;
1500 parts->has_gpt = true;
1501 }
1502
1503 /*
1504 * Delete all old partitions
1505 */
1506 for (p = parts->obsolete; p != NULL; p = n) {
1507 run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
1508 p->gp_start, arg->disk);
1509 n = p->gp_next;
1510 free(p);
1511 }
1512 parts->obsolete = NULL;
1513
1514 /*
1515 * Modify existing but changed partitions
1516 */
1517 for (p = parts->partitions; p != NULL; p = p->gp_next) {
1518 if (!(p->gp_flags & GPEF_ON_DISK))
1519 continue;
1520
1521 if (p->gp_flags & GPEF_RESIZED) {
1522 run_program(RUN_SILENT,
1523 "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
1524 p->gp_start, p->gp_size, arg->disk);
1525 p->gp_flags &= ~GPEF_RESIZED;
1526 }
1527
1528 if (!(p->gp_flags & GPEF_MODIFIED))
1529 continue;
1530
1531 if (!gpt_modify_part(parts->dp.disk, p))
1532 return false;
1533 }
1534
1535 /*
1536 * Add new partitions
1537 */
1538 for (p = parts->partitions; p != NULL; p = p->gp_next) {
1539 if (p->gp_flags & GPEF_ON_DISK)
1540 continue;
1541 if (!(p->gp_flags & GPEF_MODIFIED))
1542 continue;
1543
1544 if (p->gp_label[0] == 0)
1545 label_arg[0] = 0;
1546 else
1547 sprintf(label_arg, "-l \'%s\'", p->gp_label);
1548
1549 if (p->gp_type != NULL)
1550 run_program(RUN_SILENT,
1551 "gpt -n add -b %" PRIu64 " -s %" PRIu64
1552 "s -t %s %s %s",
1553 p->gp_start, p->gp_size, p->gp_type->tid,
1554 label_arg, arg->disk);
1555 else
1556 run_program(RUN_SILENT,
1557 "gpt -n add -b %" PRIu64 " -s %" PRIu64
1558 "s %s %s",
1559 p->gp_start, p->gp_size, label_arg, arg->disk);
1560 gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
1561 gpt_read_part(arg->disk, p->gp_start, p);
1562 p->gp_flags |= GPEF_ON_DISK;
1563 }
1564
1565 /*
1566 * Additional MD bootloader magic...
1567 */
1568 if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
1569 efi_is_new))
1570 return false;
1571
1572 return true;
1573 }
1574
1575 static part_id
1576 gpt_find_by_name(struct disk_partitions *arg, const char *name)
1577 {
1578 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1579 struct gpt_part_entry *p;
1580 part_id pno;
1581
1582 for (pno = 0, p = parts->partitions; p != NULL;
1583 p = p->gp_next, pno++) {
1584 if (strcmp(p->gp_label, name) == 0)
1585 return pno;
1586 if (strcmp(p->gp_id, name) == 0)
1587 return pno;
1588 }
1589
1590 return NO_PART;
1591 }
1592
1593 bool
1594 gpt_parts_check(void)
1595 {
1596
1597 check_available_binaries();
1598
1599 return have_gpt && have_dk;
1600 }
1601
1602 static void
1603 gpt_free(struct disk_partitions *arg)
1604 {
1605 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1606 struct gpt_part_entry *p, *n;
1607
1608 assert(parts != NULL);
1609 for (p = parts->partitions; p != NULL; p = n) {
1610 if (p->gp_flags & GPEF_WEDGE)
1611 register_post_umount_delwedge(parts->dp.disk,
1612 p->gp_dev_name);
1613 free(__UNCONST(p->last_mounted));
1614 n = p->gp_next;
1615 free(p);
1616 }
1617 free(__UNCONST(parts->dp.disk));
1618 free(parts);
1619 }
1620
1621 static void
1622 gpt_destroy_part_scheme(struct disk_partitions *arg)
1623 {
1624
1625 run_program(RUN_SILENT, "gpt destroy %s", arg->disk);
1626 gpt_free(arg);
1627 }
1628
1629 static bool
1630 gpt_custom_attribute_writable(const struct disk_partitions *arg,
1631 part_id ptn, size_t attr_no)
1632 {
1633 const struct gpt_disk_partitions *parts =
1634 (const struct gpt_disk_partitions*)arg;
1635 size_t i;
1636 struct gpt_part_entry *p;
1637
1638 if (attr_no >= arg->pscheme->custom_attribute_count)
1639 return false;
1640
1641 const msg label = arg->pscheme->custom_attributes[attr_no].label;
1642
1643 /* we can not edit the uuid attribute */
1644 if (label == MSG_ptn_uuid)
1645 return false;
1646
1647 /* the label is always editable */
1648 if (label == MSG_ptn_label)
1649 return true;
1650
1651 /* the GPT type is read only */
1652 if (label == MSG_ptn_gpt_type)
1653 return false;
1654
1655 /* BOOTME makes no sense on swap partitions */
1656 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1657 if (i == ptn)
1658 break;
1659
1660 if (p == NULL)
1661 return false;
1662
1663 if (p->fs_type == FS_SWAP ||
1664 (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
1665 return false;
1666
1667 return true;
1668 }
1669
1670 static const char *
1671 gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
1672 {
1673 const struct gpt_disk_partitions *parts =
1674 (const struct gpt_disk_partitions*)arg;
1675 size_t i;
1676 struct gpt_part_entry *p;
1677
1678 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1679 if (i == ptn)
1680 break;
1681
1682 if (p == NULL)
1683 return NULL;
1684
1685 if (p->gp_label[0] != 0)
1686 return p->gp_label;
1687 return p->gp_id;
1688 }
1689
1690 static bool
1691 gpt_format_custom_attribute(const struct disk_partitions *arg,
1692 part_id ptn, size_t attr_no, const struct disk_part_info *info,
1693 char *out, size_t out_space)
1694 {
1695 const struct gpt_disk_partitions *parts =
1696 (const struct gpt_disk_partitions*)arg;
1697 size_t i;
1698 struct gpt_part_entry *p, data;
1699
1700 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1701 if (i == ptn)
1702 break;
1703
1704 if (p == NULL)
1705 return false;
1706
1707 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1708 return false;
1709
1710 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1711
1712 if (info != NULL) {
1713 data = *p;
1714 gpt_info_to_part(&data, info, NULL);
1715 p = &data;
1716 }
1717
1718 if (label == MSG_ptn_label)
1719 strlcpy(out, p->gp_label, out_space);
1720 else if (label == MSG_ptn_uuid)
1721 strlcpy(out, p->gp_id, out_space);
1722 else if (label == MSG_ptn_gpt_type) {
1723 if (p->gp_type != NULL)
1724 strlcpy(out, p->gp_type->gent.description, out_space);
1725 else if (out_space > 1)
1726 out[0] = 0;
1727 } else if (label == MSG_ptn_boot)
1728 strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
1729 MSG_Yes : MSG_No), out_space);
1730 else
1731 return false;
1732
1733 return true;
1734 }
1735
1736 static bool
1737 gpt_custom_attribute_toggle(struct disk_partitions *arg,
1738 part_id ptn, size_t attr_no)
1739 {
1740 const struct gpt_disk_partitions *parts =
1741 (const struct gpt_disk_partitions*)arg;
1742 size_t i;
1743 struct gpt_part_entry *p;
1744
1745 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1746 if (i == ptn)
1747 break;
1748
1749 if (p == NULL)
1750 return false;
1751
1752 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1753 return false;
1754
1755 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1756 if (label != MSG_ptn_boot)
1757 return false;
1758
1759 if (p->gp_attr & GPT_ATTR_BOOT) {
1760 p->gp_attr &= ~GPT_ATTR_BOOT;
1761 } else {
1762 for (i = 0, p = parts->partitions; p != NULL;
1763 i++, p = p->gp_next)
1764 if (i == ptn)
1765 p->gp_attr |= GPT_ATTR_BOOT;
1766 else
1767 p->gp_attr &= ~GPT_ATTR_BOOT;
1768 }
1769 return true;
1770 }
1771
1772 static bool
1773 gpt_custom_attribute_set_str(struct disk_partitions *arg,
1774 part_id ptn, size_t attr_no, const char *new_val)
1775 {
1776 const struct gpt_disk_partitions *parts =
1777 (const struct gpt_disk_partitions*)arg;
1778 size_t i;
1779 struct gpt_part_entry *p;
1780
1781 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1782 if (i == ptn)
1783 break;
1784
1785 if (p == NULL)
1786 return false;
1787
1788 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1789 return false;
1790
1791 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1792
1793 if (label != MSG_ptn_label)
1794 return false;
1795
1796 strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
1797 return true;
1798 }
1799
1800 static bool
1801 gpt_have_boot_support(const char *disk)
1802 {
1803 #ifdef HAVE_GPT_BOOT
1804 return true;
1805 #else
1806 return false;
1807 #endif
1808 }
1809
1810 const struct disk_part_custom_attribute gpt_custom_attrs[] = {
1811 { .label = MSG_ptn_label, .type = pet_str },
1812 { .label = MSG_ptn_uuid, .type = pet_str },
1813 { .label = MSG_ptn_gpt_type, .type = pet_str },
1814 { .label = MSG_ptn_boot, .type = pet_bool },
1815 };
1816
1817 const struct disk_partitioning_scheme
1818 gpt_parts = {
1819 .name = MSG_parttype_gpt,
1820 .short_name = MSG_parttype_gpt_short,
1821 .part_flag_desc = MSG_gpt_flag_desc,
1822 .custom_attribute_count = __arraycount(gpt_custom_attrs),
1823 .custom_attributes = gpt_custom_attrs,
1824 .get_part_types_count = gpt_type_count,
1825 .get_part_type = gpt_get_ptype,
1826 .get_generic_part_type = gpt_get_generic_type,
1827 .get_fs_part_type = gpt_get_fs_part_type,
1828 .get_default_fstype = gpt_get_default_fstype,
1829 .create_custom_part_type = gpt_create_custom_part_type,
1830 .create_unknown_part_type = gpt_create_unknown_part_type,
1831 .get_part_alignment = gpt_get_part_alignment,
1832 .read_from_disk = gpt_read_from_disk,
1833 .get_cylinder_size = gpt_cyl_size,
1834 .create_new_for_disk = gpt_create_new,
1835 .have_boot_support = gpt_have_boot_support,
1836 .find_by_name = gpt_find_by_name,
1837 .can_add_partition = gpt_can_add_partition,
1838 .custom_attribute_writable = gpt_custom_attribute_writable,
1839 .format_custom_attribute = gpt_format_custom_attribute,
1840 .custom_attribute_toggle = gpt_custom_attribute_toggle,
1841 .custom_attribute_set_str = gpt_custom_attribute_set_str,
1842 .other_partition_identifier = gpt_get_label_str,
1843 .get_part_device = gpt_get_part_device,
1844 .max_free_space_at = gpt_max_free_space_at,
1845 .get_free_spaces = gpt_get_free_spaces,
1846 .adapt_foreign_part_info = generic_adapt_foreign_part_info,
1847 .get_part_info = gpt_get_part_info,
1848 .get_part_attr_str = gpt_get_part_attr_str,
1849 .set_part_info = gpt_set_part_info,
1850 .add_partition = gpt_add_part,
1851 .delete_all_partitions = gpt_delete_all_partitions,
1852 .delete_partition = gpt_delete_partition,
1853 .write_to_disk = gpt_write_to_disk,
1854 .free = gpt_free,
1855 .destroy_part_scheme = gpt_destroy_part_scheme,
1856 .cleanup = gpt_cleanup,
1857 };
1858