xref: /netbsd-src/usr.sbin/sysinst/label.c (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /*	$NetBSD: label.c,v 1.32 2021/01/31 22:45:46 rillig Exp $	*/
2 
3 /*
4  * Copyright 1997 Jonathan Stone
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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed for the NetBSD Project by
18  *      Jonathan Stone.
19  * 4. The name of Jonathan Stone may not be used to endorse
20  *    or promote products derived from this software without specific prior
21  *    written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JONATHAN STONE ``AS IS''
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: label.c,v 1.32 2021/01/31 22:45:46 rillig Exp $");
40 #endif
41 
42 #include <sys/types.h>
43 #include <stddef.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <fcntl.h>
48 #include <util.h>
49 #include <unistd.h>
50 #include <sys/dkio.h>
51 #include <sys/param.h>
52 #include <sys/bootblock.h>
53 #include <sys/bitops.h>
54 #include <ufs/ffs/fs.h>
55 
56 #include "defs.h"
57 #include "msg_defs.h"
58 #include "menu_defs.h"
59 
60 /*
61  * local prototypes
62  */
63 static bool boringpart(const struct disk_part_info *info);
64 static bool checklabel(struct disk_partitions*, char *, char *);
65 static void show_partition_adder(menudesc *, struct partition_usage_set*);
66 
67 /*
68  * Return 1 if a partition should be ignored when checking
69  * for overlapping partitions.
70  */
71 static bool
72 boringpart(const struct disk_part_info *info)
73 {
74 
75 	if (info->size == 0)
76 		return true;
77 	if (info->flags &
78 	     (PTI_PSCHEME_INTERNAL|PTI_WHOLE_DISK|PTI_SEC_CONTAINER|
79 	     PTI_RAW_PART))
80 		return true;
81 
82 	return false;
83 }
84 
85 /*
86  * We have some partitions in our "wanted" list that we may not edit,
87  * like the RAW_PART in disklabel, some that just represent external
88  * mount entries for the final fstab or similar.
89  * We have previously sorted pset->parts and pset->infos to be in sync,
90  * but the former "array" may be shorter.
91  * Here are a few quick predicates to check for them.
92  */
93 static bool
94 real_partition(const struct partition_usage_set *pset, int index)
95 {
96 	if (index < 0 || (size_t)index >= pset->num)
97 		return false;
98 
99 	return pset->infos[index].cur_part_id != NO_PART;
100 }
101 
102 /*
103  * Check partitioning for overlapping partitions.
104  * Returns 0 if no overlapping partition found, nonzero otherwise.
105  * Sets reference arguments ovly1 and ovly2 to the indices of
106  * overlapping partitions if any are found.
107  */
108 static bool
109 checklabel(struct disk_partitions *parts,
110     char *ovl1, char *ovl2)
111 {
112 	part_id i, j;
113 	struct disk_part_info info;
114 	daddr_t istart, iend, jstart, jend;
115 	unsigned int fs_type, fs_sub_type;
116 
117 	for (i = 0; i < parts->num_part - 1; i ++ ) {
118 		if (!parts->pscheme->get_part_info(parts, i, &info))
119 			continue;
120 
121 		/* skip unused or reserved partitions */
122 		if (boringpart(&info))
123 			continue;
124 
125 		/*
126 		 * check succeeding partitions for overlap.
127 		 * O(n^2), but n is small.
128 		 */
129 		istart = info.start;
130 		iend = istart + info.size;
131 		fs_type = info.fs_type;
132 		fs_sub_type = info.fs_sub_type;
133 
134 		for (j = i+1; j < parts->num_part; j++) {
135 
136 			if (!parts->pscheme->get_part_info(parts, j, &info))
137 				continue;
138 
139 			/* skip unused or reserved partitions */
140 			if (boringpart(&info))
141 				continue;
142 
143 			jstart = info.start;
144 			jend = jstart + info.size;
145 
146 			/* overlap? */
147 			if ((istart <= jstart && jstart < iend) ||
148 			    (jstart <= istart && istart < jend)) {
149 				snprintf(ovl1, MENUSTRSIZE,
150 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
151 				    istart / sizemult, iend / sizemult,
152 				    multname,
153 				    getfslabelname(fs_type, fs_sub_type));
154 				snprintf(ovl2, MENUSTRSIZE,
155 				    "%" PRIu64 " - %" PRIu64 " %s, %s",
156 				    jstart / sizemult, jend / sizemult,
157 				    multname,
158 				    getfslabelname(info.fs_type,
159 				        info.fs_sub_type));
160 				return false;
161 			}
162 		}
163 	}
164 
165 	return true;
166 }
167 
168 int
169 checkoverlap(struct disk_partitions *parts)
170 {
171 	char desc1[MENUSTRSIZE], desc2[MENUSTRSIZE];
172 	if (!checklabel(parts, desc1, desc2)) {
173 		msg_display_subst(MSG_partitions_overlap, 2, desc1, desc2);
174 		return 1;
175 	}
176 	return 0;
177 }
178 
179 /*
180  * return (see post_edit_verify):
181  *  0 -> abort
182  *  1 -> re-edit
183  *  2 -> continue installation
184  */
185 static int
186 verify_parts(struct partition_usage_set *pset, bool install)
187 {
188 	struct part_usage_info *wanted;
189 	struct disk_partitions *parts;
190 	size_t i, num_root;
191 	daddr_t first_bsdstart, inst_start;
192 	int rv;
193 
194 	first_bsdstart = inst_start = -1;
195 	num_root = 0;
196 	parts = pset->parts;
197 	for (i = 0; i < pset->num; i++) {
198 		wanted = &pset->infos[i];
199 
200 		if (wanted->flags & PUIFLG_JUST_MOUNTPOINT)
201 			continue;
202 		if (wanted->cur_part_id == NO_PART)
203 			continue;
204 		if (!(wanted->instflags & PUIINST_MOUNT))
205 			continue;
206 		if (strcmp(wanted->mount, "/") != 0)
207 			continue;
208 		num_root++;
209 
210 		if (first_bsdstart <= 0) {
211 			first_bsdstart = wanted->cur_start;
212 		}
213 		if (inst_start < 0 &&
214 		    (wanted->cur_flags & PTI_INSTALL_TARGET)) {
215 			inst_start = wanted->cur_start;
216 		}
217 	}
218 
219 	if ((num_root == 0 && install) ||
220 	    (num_root > 1 && inst_start < 0)) {
221 		if (num_root == 0 && install)
222 			msg_display_subst(MSG_must_be_one_root, 2,
223 			    msg_string(parts->pscheme->name),
224 			    msg_string(parts->pscheme->short_name));
225 		else
226 			msg_display_subst(MSG_multbsdpart, 2,
227 			    msg_string(parts->pscheme->name),
228 			    msg_string(parts->pscheme->short_name));
229 		rv = ask_reedit(parts);
230 		if (rv != 2)
231 			return rv;
232 	}
233 
234 	/* Check for overlaps */
235 	if (checkoverlap(parts) != 0) {
236 		rv = ask_reedit(parts);
237 		if (rv != 2)
238 			return rv;
239 	}
240 
241 	/*
242 	 * post_edit_verify returns:
243 	 *  0 -> abort
244 	 *  1 -> re-edit
245 	 *  2 -> continue installation
246 	 */
247 	if (parts->pscheme->post_edit_verify)
248 		return parts->pscheme->post_edit_verify(parts, false);
249 
250 	return 2;
251 }
252 
253 static int
254 edit_fs_start(menudesc *m, void *arg)
255 {
256 	struct single_part_fs_edit *edit = arg;
257 	daddr_t start, end;
258 
259 	start = getpartoff(edit->pset->parts, edit->info.start);
260 	if (edit->info.size != 0) {
261 		/* Try to keep end in the same place */
262 		end = edit->info.start + edit->info.size;
263 		if (end < start)
264 			edit->info.size = edit->pset->parts->pscheme->
265 			    max_free_space_at(edit->pset->parts,
266 			    edit->info.start);
267 		else
268 			edit->info.size = end - start;
269 	}
270 	edit->info.start = start;
271 	return 0;
272 }
273 
274 static int
275 edit_fs_size(menudesc *m, void *arg)
276 {
277 	struct single_part_fs_edit *edit = arg;
278 	struct disk_part_info pinfo;
279 	daddr_t size;
280 
281 	/* get original partition data, in case start moved already */
282 	edit->pset->parts->pscheme->get_part_info(edit->pset->parts,
283 	    edit->id, &pinfo);
284 	/* ask for new size with old start and current values */
285 	size = getpartsize(edit->pset->parts, pinfo.start,
286 	    edit->info.start, edit->info.size);
287 	if (size < 0)
288 		return 0;
289 	if (size > edit->pset->parts->disk_size)
290 		size = edit->pset->parts->disk_size - edit->info.start;
291 	edit->info.size = size;
292 	return 0;
293 }
294 
295 static int
296 set_ffs_opt_pow2(menudesc *m, void *arg)
297 {
298 	struct single_part_fs_edit *edit = arg;
299 	size_t val = 1 << (edit->offset+m->cursel);
300 
301 	if (edit->mode == 1) {
302 		edit->info.fs_opt1 = val;
303 		edit->wanted->fs_opt1 = val;
304 	} else if (edit->mode == 2) {
305 		edit->info.fs_opt2 = val;
306 		edit->wanted->fs_opt2 = val;
307 	}
308 	return 0;
309 }
310 
311 static int
312 edit_fs_ffs_opt(menudesc *m, void *arg, msg head,
313     size_t min_val, size_t max_val)
314 {
315 	struct single_part_fs_edit *edit = arg;
316 	menu_ent opts[min(MAXPHYS/4096, 8)];
317 	char names[min(MAXPHYS/4096, 8)][20];
318 	size_t i, val;
319 	int menu;
320 
321 	edit->offset = ilog2(min_val);
322 	memset(opts, 0, sizeof opts);
323 	for (i = 0, val = min_val; val <= max_val; i++, val <<= 1) {
324 		snprintf(names[i], sizeof names[i], "%zu", val);
325 		opts[i].opt_name = names[i];
326 		opts[i].opt_action = set_ffs_opt_pow2;
327 		opts[i].opt_flags = OPT_EXIT;
328 	}
329 	menu = new_menu(head, opts, i, 40, 6, 0, 0, MC_NOEXITOPT,
330 	    NULL, NULL, NULL, NULL, NULL);
331 	if (menu < 0)
332 		return 1;
333 	process_menu(menu, arg);
334 	free_menu(menu);
335 	return 0;
336 }
337 
338 static int
339 edit_fs_ffs_block(menudesc *m, void *arg)
340 {
341 	struct single_part_fs_edit *edit = arg;
342 
343 	edit->mode = 1;		/* edit fs_opt1 */
344 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_block_size,
345 	    4096, MAXPHYS);
346 }
347 
348 static int
349 edit_fs_ffs_frag(menudesc *m, void *arg)
350 {
351 	struct single_part_fs_edit *edit = arg;
352 	size_t bsize, sec_size;
353 
354 	edit->mode = 2;		/* edit fs_opt2 */
355 	bsize = edit->info.fs_opt1;
356 	if (bsize == 0) {
357 		sec_size = edit->wanted->parts->bytes_per_sector;
358 		if (edit->wanted->size >= (daddr_t)(128L*(GIG/sec_size)))
359 			bsize = 32*1024;
360 		else if (edit->wanted->size >= (daddr_t)(1000L*(MEG/sec_size)))
361 			bsize = 16*1024;
362 		else if (edit->wanted->size >= (daddr_t)(20L*(MEG/sec_size)))
363 			bsize = 8*1024;
364 		else
365 			bsize = 4+1024;
366 	}
367 	return edit_fs_ffs_opt(m, arg, MSG_Select_file_system_fragment_size,
368 		bsize / 8, bsize);
369 }
370 
371 static int
372 edit_fs_ffs_avg_size(menudesc *m, void *arg)
373 {
374 	struct single_part_fs_edit *edit = arg;
375 	char answer[12];
376 
377 	snprintf(answer, sizeof answer, "%u", edit->info.fs_opt3);
378 	msg_prompt_win(MSG_ptn_isize_prompt, -1, 18, 0, 0,
379 		answer, answer, sizeof answer);
380 	edit->info.fs_opt3 = atol(answer);
381 	edit->wanted->fs_opt3 = edit->info.fs_opt3;
382 
383 	return 0;
384 }
385 
386 static int
387 edit_fs_preserve(menudesc *m, void *arg)
388 {
389 	struct single_part_fs_edit *edit = arg;
390 
391 	edit->wanted->instflags ^= PUIINST_NEWFS;
392 	return 0;
393 }
394 
395 static int
396 edit_install(menudesc *m, void *arg)
397 {
398 	struct single_part_fs_edit *edit = arg;
399 
400 	edit->info.flags ^= PTI_INSTALL_TARGET;
401 	return 0;
402 }
403 
404 static int
405 edit_fs_mount(menudesc *m, void *arg)
406 {
407 	struct single_part_fs_edit *edit = arg;
408 
409 	edit->wanted->instflags ^= PUIINST_MOUNT;
410 	return 0;
411 }
412 
413 static int
414 edit_fs_mountpt(menudesc *m, void *arg)
415 {
416 	struct single_part_fs_edit *edit = arg;
417 	char *p, *first, *last, buf[MOUNTLEN];
418 
419 	strlcpy(buf, edit->wanted->mount, sizeof buf);
420 	msg_prompt_win(MSG_mountpoint, -1, 18, 0, 0,
421 		buf, buf, MOUNTLEN);
422 
423 	/*
424 	 * Trim all leading and trailing whitespace
425 	 */
426 	for (first = NULL, last = NULL, p = buf; *p; p++) {
427 		if (isspace((unsigned char)*p))
428 			continue;
429 		if (first == NULL)
430 			first = p;
431 		last = p;
432 	}
433 	if (last != NULL)
434 		last[1] = 0;
435 
436 	if (first == NULL || *first == 0 || strcmp(first, "none") == 0) {
437 		edit->wanted->mount[0] = 0;
438 		edit->wanted->instflags &= ~PUIINST_MOUNT;
439 		return 0;
440 	}
441 
442 	if (*first != '/') {
443 		edit->wanted->mount[0] = '/';
444 		strlcpy(&edit->wanted->mount[1], first,
445 		    sizeof(edit->wanted->mount)-1);
446 	} else {
447 		strlcpy(edit->wanted->mount, first, sizeof edit->wanted->mount);
448 	}
449 
450 	return 0;
451 }
452 
453 static int
454 edit_restore(menudesc *m, void *arg)
455 {
456 	struct single_part_fs_edit *edit = arg;
457 
458 	edit->info = edit->old_info;
459 	*edit->wanted = edit->old_usage;
460 	return 0;
461 }
462 
463 static int
464 edit_cancel(menudesc *m, void *arg)
465 {
466 	struct single_part_fs_edit *edit = arg;
467 
468 	edit->rv = -1;
469 	return 1;
470 }
471 
472 static int
473 edit_delete_ptn(menudesc *m, void *arg)
474 {
475 	struct single_part_fs_edit *edit = arg;
476 
477 	edit->rv = -2;
478 	return 1;
479 }
480 
481 /*
482  * We have added/removed partitions, all cur_part_id values are
483  * out of sync. Re-fetch and reorder partitions accordingly.
484  */
485 static void
486 renumber_partitions(struct partition_usage_set *pset)
487 {
488 	struct part_usage_info *ninfos;
489 	struct disk_part_info info;
490 	size_t i;
491 	part_id pno;
492 
493 	ninfos = calloc(pset->parts->num_part, sizeof(*ninfos));
494 	if (ninfos == NULL) {
495 		err_msg_win(err_outofmem);
496 		return;
497 	}
498 
499 	for (pno = 0; pno < pset->parts->num_part; pno++) {
500 		if (!pset->parts->pscheme->get_part_info(pset->parts, pno,
501 		    &info))
502 			continue;
503 		for (i = 0; i < pset->parts->num_part; i++) {
504 			if (pset->infos[i].cur_start != info.start)
505 				continue;
506 			if (pset->infos[i].cur_flags != info.flags)
507 				continue;
508 			if ((info.fs_type != FS_UNUSED &&
509 			    info.fs_type == pset->infos[i].fs_type) ||
510 			    (pset->infos[i].type ==
511 			    info.nat_type->generic_ptype)) {
512 				memcpy(&ninfos[pno], &pset->infos[i],
513 				    sizeof(ninfos[pno]));
514 				ninfos[pno].cur_part_id = pno;
515 				break;
516 			}
517 		}
518 	}
519 
520 	memcpy(pset->infos, ninfos, sizeof(*pset->infos)*pset->parts->num_part);
521 	free(ninfos);
522 }
523 
524 /*
525  * Most often used file system types, we offer them in a first level menu.
526  */
527 static const uint edit_fs_common_types[] =
528     { FS_BSDFFS, FS_SWAP, FS_MSDOS, FS_BSDLFS, FS_EX2FS };
529 
530 /*
531  * Functions for uncommon file system types - we offer the full list,
532  * but put FFSv2 and FFSv1 at the front.
533  */
534 static void
535 init_fs_type_ext(menudesc *menu, void *arg)
536 {
537 	struct single_part_fs_edit *edit = arg;
538 	uint t = edit->info.fs_type;
539 	size_t i, ndx, max = menu->numopts;
540 
541 	if (t == FS_BSDFFS) {
542 		if (edit->info.fs_sub_type == 2)
543 			menu->cursel = 0;
544 		else
545 			menu->cursel = 1;
546 		return;
547 	} else if (t == FS_EX2FS && edit->info.fs_sub_type == 1) {
548 		menu->cursel = FSMAXTYPES;
549 		return;
550 	}
551 	/* skip the two FFS entries, and do not add FFS later again */
552 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
553 		if (i == FS_UNUSED)
554 			continue;
555 		if (i == FS_BSDFFS)
556 			continue;
557 		if (fstypenames[i] == NULL)
558 			continue;
559 
560 		if (i == t) {
561 			menu->cursel = ndx;
562 			break;
563 		}
564 		ndx++;
565 	}
566 }
567 
568 static int
569 set_fstype_ext(menudesc *menu, void *arg)
570 {
571 	struct single_part_fs_edit *edit = arg;
572 	size_t i, ndx, max = menu->numopts;
573 	enum part_type pt;
574 
575 	if (menu->cursel == 0 || menu->cursel == 1) {
576 		edit->info.fs_type = FS_BSDFFS;
577 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
578 		goto found_type;
579 	} else if (menu->cursel == FSMAXTYPES) {
580 		edit->info.fs_type = FS_EX2FS;
581 		edit->info.fs_sub_type = 1;
582 		goto found_type;
583 	}
584 
585 	for (ndx = 2, i = 0; i < FSMAXTYPES && ndx < max; i++) {
586 		if (i == FS_UNUSED)
587 			continue;
588 		if (i == FS_BSDFFS)
589 			continue;
590 		if (fstypenames[i] == NULL)
591 			continue;
592 
593 		if (ndx == (size_t)menu->cursel) {
594 			edit->info.fs_type = i;
595 			edit->info.fs_sub_type = 0;
596 			goto found_type;
597 		}
598 		ndx++;
599 	}
600 	return 1;
601 
602 found_type:
603 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
604 	edit->info.nat_type = edit->pset->parts->pscheme->
605 	    get_fs_part_type(pt, edit->info.fs_type, edit->info.fs_sub_type);
606 	if (edit->info.nat_type == NULL)
607 		edit->info.nat_type = edit->pset->parts->pscheme->
608 		    get_generic_part_type(PT_root);
609 	edit->wanted->type = edit->info.nat_type->generic_ptype;
610 	edit->wanted->fs_type = edit->info.fs_type;
611 	edit->wanted->fs_version = edit->info.fs_sub_type;
612 	return 1;
613 }
614 
615 /*
616  * Offer a menu with "exotic" file system types, start with FFSv2 and FFSv1,
617  * skip later FFS entry in the generic list.
618  */
619 static int
620 edit_fs_type_ext(menudesc *menu, void *arg)
621 {
622 	menu_ent *opts;
623 	int m;
624 	size_t i, ndx, cnt;
625 
626 	cnt = __arraycount(fstypenames);
627 	opts = calloc(cnt, sizeof(*opts));
628 	if (opts == NULL)
629 		return 1;
630 
631 	ndx = 0;
632 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffsv2);
633 	opts[ndx].opt_action = set_fstype_ext;
634 	ndx++;
635 	opts[ndx].opt_name = msg_string(MSG_fs_type_ffs);
636 	opts[ndx].opt_action = set_fstype_ext;
637 	ndx++;
638 	for (i = 0; i < FSMAXTYPES && ndx < cnt; i++) {
639 		if (i == FS_UNUSED)
640 			continue;
641 		if (i == FS_BSDFFS)
642 			continue;
643 		if (fstypenames[i] == NULL)
644 			continue;
645 		opts[ndx].opt_name = fstypenames[i];
646 		opts[ndx].opt_action = set_fstype_ext;
647 		ndx++;
648 	}
649 	opts[ndx].opt_name = msg_string(MSG_fs_type_ext2old);
650 	opts[ndx].opt_action = set_fstype_ext;
651 	ndx++;
652 	assert(ndx == cnt);
653 	m = new_menu(MSG_Select_the_type, opts, ndx,
654 		30, 6, 10, 0, MC_SUBMENU | MC_SCROLL,
655 		init_fs_type_ext, NULL, NULL, NULL, MSG_unchanged);
656 
657 	if (m < 0)
658 		return 1;
659 	process_menu(m, arg);
660 	free_menu(m);
661 	free(opts);
662 
663 	return 1;
664 }
665 
666 static void
667 init_fs_type(menudesc *menu, void *arg)
668 {
669 	struct single_part_fs_edit *edit = arg;
670 	size_t i;
671 
672 	/* init menu->cursel from fs type in arg */
673 	if (edit->info.fs_type == FS_BSDFFS) {
674 		if (edit->info.fs_sub_type == 2)
675 			menu->cursel = 0;
676 		else
677 			menu->cursel = 1;
678 	}
679 	for (i = 1; i < __arraycount(edit_fs_common_types); i++) {
680 		if (edit->info.fs_type == edit_fs_common_types[i]) {
681 			menu->cursel = i+1;
682 			break;
683 		}
684 	}
685 }
686 
687 static int
688 set_fstype(menudesc *menu, void *arg)
689 {
690 	struct single_part_fs_edit *edit = arg;
691 	enum part_type pt;
692 	int ndx;
693 
694 	pt = edit->info.nat_type ? edit->info.nat_type->generic_ptype : PT_root;
695 	if (menu->cursel < 2) {
696 		edit->info.fs_type = FS_BSDFFS;
697 		edit->info.fs_sub_type = menu->cursel == 0 ? 2 : 1;
698 		edit->info.nat_type = edit->pset->parts->pscheme->
699 		    get_fs_part_type(pt, FS_BSDFFS, 2);
700 		if (edit->info.nat_type == NULL)
701 			edit->info.nat_type = edit->pset->parts->
702 			    pscheme->get_generic_part_type(PT_root);
703 		edit->wanted->type = edit->info.nat_type->generic_ptype;
704 		edit->wanted->fs_type = edit->info.fs_type;
705 		edit->wanted->fs_version = edit->info.fs_sub_type;
706 		return 1;
707 	}
708 	ndx = menu->cursel-1;
709 
710 	if (ndx < 0 ||
711 	    (size_t)ndx >= __arraycount(edit_fs_common_types))
712 		return 1;
713 
714 	edit->info.fs_type = edit_fs_common_types[ndx];
715 	edit->info.fs_sub_type = 0;
716 	edit->info.nat_type = edit->pset->parts->pscheme->
717 	    get_fs_part_type(pt, edit->info.fs_type, 0);
718 	if (edit->info.nat_type == NULL)
719 		edit->info.nat_type = edit->pset->parts->
720 		    pscheme->get_generic_part_type(PT_root);
721 	edit->wanted->type = edit->info.nat_type->generic_ptype;
722 	edit->wanted->fs_type = edit->info.fs_type;
723 	edit->wanted->fs_version = edit->info.fs_sub_type;
724 	return 1;
725 }
726 
727 /*
728  * Offer a menu selecting the common file system types
729  */
730 static int
731 edit_fs_type(menudesc *menu, void *arg)
732 {
733 	struct single_part_fs_edit *edit = arg;
734 	menu_ent *opts;
735 	int m, cnt;
736 	size_t i;
737 
738 	/*
739 	 * Shortcut to full menu if we have an exotic value
740 	 */
741 	if (edit->info.fs_type == FS_EX2FS && edit->info.fs_sub_type == 1) {
742 		edit_fs_type_ext(menu, arg);
743 		return 0;
744 	}
745 	for (i = 0; i < __arraycount(edit_fs_common_types); i++)
746 		if (edit->info.fs_type == edit_fs_common_types[i])
747 			break;
748 	if (i >= __arraycount(edit_fs_common_types)) {
749 		edit_fs_type_ext(menu, arg);
750 		return 0;
751 	}
752 
753 	/*
754 	 * Starting with a common type, show short menu first
755 	 */
756 	cnt = __arraycount(edit_fs_common_types) + 2;
757 	opts = calloc(cnt, sizeof(*opts));
758 	if (opts == NULL)
759 		return 0;
760 
761 	/* special case entry 0: two FFS entries */
762 	for (i = 0; i < __arraycount(edit_fs_common_types); i++) {
763 		opts[i+1].opt_name = getfslabelname(edit_fs_common_types[i], 0);
764 		opts[i+1].opt_action = set_fstype;
765 	}
766 	/* duplicate FFS (at offset 1) into first entry */
767 	opts[0] = opts[1];
768 	opts[0].opt_name = msg_string(MSG_fs_type_ffsv2);
769 	opts[1].opt_name = msg_string(MSG_fs_type_ffs);
770 	/* add secondary sub-menu */
771 	assert(i+1 < (size_t)cnt);
772 	opts[i+1].opt_name = msg_string(MSG_other_fs_type);
773 	opts[i+1].opt_action = edit_fs_type_ext;
774 
775 	m = new_menu(MSG_Select_the_type, opts, cnt,
776 		30, 6, 0, 0, MC_SUBMENU | MC_SCROLL,
777 		init_fs_type, NULL, NULL, NULL, MSG_unchanged);
778 
779 	if (m < 0)
780 		return 0;
781 	process_menu(m, arg);
782 	free_menu(m);
783 	free(opts);
784 
785 	return 0;
786 }
787 
788 
789 static void update_edit_ptn_menu(menudesc *m, void *arg);
790 static void draw_edit_ptn_line(menudesc *m, int opt, void *arg);
791 static int edit_ptn_custom_type(menudesc *m, void *arg);
792 
793 static void
794 remember_deleted(struct partition_usage_set *pset,
795     struct disk_partitions *parts)
796 {
797 	size_t i, num;
798 	struct disk_partitions **tab;
799 
800 	/* do we have parts on record already? */
801 	for (i = 0; i < pset->num_write_back; i++)
802 		if (pset->write_back[i] == parts)
803 			return;
804 	/*
805 	 * Need to record this partition table for write back
806 	 */
807 	num = pset->num_write_back + 1;
808 	tab = realloc(pset->write_back, num*sizeof(*pset->write_back));
809 	if (!tab)
810 		return;
811 	tab[pset->num_write_back] = parts;
812 	pset->write_back = tab;
813 	pset->num_write_back = num;
814 }
815 
816 int
817 edit_ptn(menudesc *menu, void *arg)
818 {
819 	struct partition_usage_set *pset = arg;
820 	struct single_part_fs_edit edit;
821 	int fspart_menu, num_opts;
822 	const char *err;
823 	menu_ent *mopts, *popt;
824 	bool is_new_part, with_inst_opt = pset->parts->parent == NULL;
825 
826 	static const menu_ent edit_ptn_fields_head[] = {
827 		{ .opt_action=edit_fs_type },
828 		{ .opt_action=edit_fs_start },
829 		{ .opt_action=edit_fs_size },
830 		{ .opt_flags=OPT_IGNORE },
831 	};
832 
833 	static const menu_ent edit_ptn_fields_head_add[] = {
834 		{ .opt_action=edit_install },
835 	};
836 
837 	static const menu_ent edit_ptn_fields_head2[] = {
838 		{ .opt_action=edit_fs_preserve },
839 		{ .opt_action=edit_fs_mount },
840 		{ .opt_menu=MENU_mountoptions, .opt_flags=OPT_SUB },
841 		{ .opt_action=edit_fs_mountpt },
842 	};
843 
844 	static const menu_ent edit_ptn_fields_ffs[] = {
845 		{ .opt_action=edit_fs_ffs_avg_size },
846 		{ .opt_action=edit_fs_ffs_block },
847 		{ .opt_action=edit_fs_ffs_frag },
848 	};
849 
850 	static const menu_ent edit_ptn_fields_tail[] = {
851 		{ .opt_name=MSG_askunits, .opt_menu=MENU_sizechoice,
852 		  .opt_flags=OPT_SUB },
853 		{ .opt_name=MSG_restore,
854 		  .opt_action=edit_restore},
855 		{ .opt_name=MSG_Delete_partition,
856 		  .opt_action=edit_delete_ptn},
857 		{ .opt_name=MSG_cancel,
858 		  .opt_action=edit_cancel},
859 	};
860 
861 	memset(&edit, 0, sizeof edit);
862 	edit.pset = pset;
863 	edit.index = menu->cursel;
864 	edit.wanted = &pset->infos[edit.index];
865 
866 	if (menu->cursel < 0 || (size_t)menu->cursel > pset->parts->num_part)
867 		return 0;
868 	is_new_part = (size_t)menu->cursel == pset->parts->num_part;
869 
870 	num_opts = __arraycount(edit_ptn_fields_head) +
871 	    __arraycount(edit_ptn_fields_head2) +
872 	    __arraycount(edit_ptn_fields_tail);
873 	if (edit.wanted->fs_type == FS_BSDFFS ||
874 	    edit.wanted->fs_type == FS_BSDLFS)
875 		num_opts += __arraycount(edit_ptn_fields_ffs);
876 	if (with_inst_opt)
877 		num_opts += __arraycount(edit_ptn_fields_head_add);
878 	if (is_new_part)
879 		num_opts--;
880 	else
881 		num_opts += pset->parts->pscheme->custom_attribute_count;
882 
883 	mopts = calloc(num_opts, sizeof(*mopts));
884 	if (mopts == NULL) {
885 		err_msg_win(err_outofmem);
886 		return 0;
887 	}
888 	memcpy(mopts, edit_ptn_fields_head, sizeof(edit_ptn_fields_head));
889 	popt = mopts + __arraycount(edit_ptn_fields_head);
890 	if (with_inst_opt) {
891 		memcpy(popt, edit_ptn_fields_head_add,
892 		    sizeof(edit_ptn_fields_head_add));
893 		popt +=  __arraycount(edit_ptn_fields_head_add);
894 	}
895 	memcpy(popt, edit_ptn_fields_head2, sizeof(edit_ptn_fields_head2));
896 	popt +=  __arraycount(edit_ptn_fields_head2);
897 	if (edit.wanted->fs_type == FS_BSDFFS ||
898 	    edit.wanted->fs_type == FS_BSDLFS) {
899 		memcpy(popt, edit_ptn_fields_ffs, sizeof(edit_ptn_fields_ffs));
900 		popt +=  __arraycount(edit_ptn_fields_ffs);
901 	}
902 	edit.first_custom_attr = popt - mopts;
903 	if (!is_new_part) {
904 		for (size_t i = 0;
905 		    i < pset->parts->pscheme->custom_attribute_count;
906 		    i++, popt++) {
907 			popt->opt_action = edit_ptn_custom_type;
908 		}
909 	}
910 	memcpy(popt, edit_ptn_fields_tail, sizeof(edit_ptn_fields_tail));
911 	popt += __arraycount(edit_ptn_fields_tail) - 1;
912 	if (is_new_part)
913 		memcpy(popt-1, popt, sizeof(*popt));
914 
915 	if (is_new_part) {
916 		struct disk_part_free_space space;
917 		daddr_t align = pset->parts->pscheme->get_part_alignment(
918 		    pset->parts);
919 
920 		edit.id = NO_PART;
921 		if (pset->parts->pscheme->get_free_spaces(pset->parts,
922 		    &space, 1, align, align, -1, -1) == 1) {
923 			edit.info.start = space.start;
924 			edit.info.size = space.size;
925 			edit.info.fs_type = FS_BSDFFS;
926 			edit.info.fs_sub_type = 2;
927 			edit.info.nat_type = pset->parts->pscheme->
928 			    get_fs_part_type(PT_root, edit.info.fs_type,
929 			    edit.info.fs_sub_type);
930 			edit.wanted->instflags = PUIINST_NEWFS;
931 		}
932 	} else {
933 		edit.id = pset->infos[edit.index].cur_part_id;
934 		if (!pset->parts->pscheme->get_part_info(pset->parts, edit.id,
935 		    &edit.info)) {
936 			free(mopts);
937 			return 0;
938 		}
939 	}
940 
941 	edit.old_usage = *edit.wanted;
942 	edit.old_info = edit.info;
943 
944 	fspart_menu = new_menu(MSG_edfspart, mopts, num_opts,
945 		15, 2, 0, 55, MC_NOCLEAR | MC_SCROLL,
946 		update_edit_ptn_menu, draw_edit_ptn_line, NULL,
947 		NULL, MSG_OK);
948 
949 	process_menu(fspart_menu, &edit);
950 	free(mopts);
951 	free_menu(fspart_menu);
952 
953 	if (edit.rv == 0) {	/* OK, set new data */
954 		edit.info.last_mounted = edit.wanted->mount;
955 		if (is_new_part) {
956 			edit.wanted->parts = pset->parts;
957 			edit.wanted->cur_part_id = pset->parts->pscheme->
958 			    add_partition(pset->parts, &edit.info, &err);
959 			if (edit.wanted->cur_part_id == NO_PART)
960 				err_msg_win(err);
961 			else {
962 				pset->parts->pscheme->get_part_info(
963 				    pset->parts, edit.wanted->cur_part_id,
964 				    &edit.info);
965 				edit.wanted->cur_start = edit.info.start;
966 				edit.wanted->size = edit.info.size;
967 				edit.wanted->type =
968 				    edit.info.nat_type->generic_ptype;
969 				edit.wanted->fs_type = edit.info.fs_type;
970 				edit.wanted->fs_version = edit.info.fs_sub_type;
971 				/* things have changed, re-sort */
972 				renumber_partitions(pset);
973 			}
974 		} else {
975 			if (!pset->parts->pscheme->set_part_info(pset->parts,
976 			    edit.id, &edit.info, &err))
977 				err_msg_win(err);
978 		}
979 
980 		/*
981 		 * if size has changed, we may need to add or remove
982 		 * the option to add partitions
983 		 */
984 		show_partition_adder(menu, pset);
985 	} else if (edit.rv == -1) {	/* cancel edit */
986 		if (is_new_part) {
987 			memmove(pset->infos+edit.index,
988 			    pset->infos+edit.index+1,
989 			    sizeof(*pset->infos)*(pset->num-edit.index));
990 			memmove(menu->opts+edit.index,
991 			    menu->opts+edit.index+1,
992 			    sizeof(*menu->opts)*(menu->numopts-edit.index));
993 			menu->numopts--;
994 			menu->cursel = 0;
995 			pset->num--;
996 			return -1;
997 		}
998 		pset->infos[edit.index] = edit.old_usage;
999 	} else if (!is_new_part && edit.rv == -2) {	/* delete partition */
1000 		if (!pset->parts->pscheme->delete_partition(pset->parts,
1001 		    edit.id, &err)) {
1002 			err_msg_win(err);
1003 			return 0;
1004 		}
1005 		remember_deleted(pset,
1006 		    pset->infos[edit.index].parts);
1007 		pset->cur_free_space += pset->infos[edit.index].size;
1008 		memmove(pset->infos+edit.index,
1009 		    pset->infos+edit.index+1,
1010 		    sizeof(*pset->infos)*(pset->num-edit.index));
1011 		memmove(menu->opts+edit.index,
1012 		    menu->opts+edit.index+1,
1013 		    sizeof(*menu->opts)*(menu->numopts-edit.index));
1014 		menu->numopts--;
1015 		menu->cursel = 0;
1016 		if (pset->parts->num_part == 0)
1017 			menu->cursel = 1;	/* skip sentinel line */
1018 
1019 		/* things have changed, re-sort */
1020 		pset->num--;
1021 		renumber_partitions(pset);
1022 
1023 		/* we can likely add new partitions now */
1024 		show_partition_adder(menu, pset);
1025 
1026 		return -1;
1027 	}
1028 
1029 	return 0;
1030 }
1031 
1032 static void
1033 update_edit_ptn_menu(menudesc *m, void *arg)
1034 {
1035 	struct single_part_fs_edit *edit = arg;
1036 	int i;
1037 	uint t = edit->info.fs_type;
1038 	size_t attr_no;
1039 
1040 	/* Determine which of the properties can be changed */
1041 	for (i = 0; i < m->numopts; i++) {
1042 		if (m->opts[i].opt_action == NULL &&
1043 		    m->opts[i].opt_menu != MENU_mountoptions)
1044 			continue;
1045 
1046 		/* Default to disabled... */
1047 		m->opts[i].opt_flags |= OPT_IGNORE;
1048 		if ((t == FS_UNUSED || t == FS_SWAP) &&
1049 		    (m->opts[i].opt_action == edit_fs_preserve ||
1050 		     m->opts[i].opt_action == edit_fs_mount ||
1051 		     m->opts[i].opt_action == edit_fs_mountpt ||
1052 		     m->opts[i].opt_menu == MENU_mountoptions))
1053 			continue;
1054 		if (m->opts[i].opt_action == edit_install &&
1055 		    edit->info.nat_type &&
1056 		    edit->info.nat_type->generic_ptype != PT_root)
1057 			/* can only install onto PT_root partitions */
1058 			continue;
1059 		if (m->opts[i].opt_action == edit_fs_preserve &&
1060 		    t != FS_BSDFFS && t != FS_BSDLFS && t != FS_APPLEUFS &&
1061 		    t != FS_MSDOS && t != FS_EX2FS) {
1062 			/* Can not newfs this filesystem */
1063 			edit->wanted->instflags &= ~PUIINST_NEWFS;
1064 			continue;
1065 		}
1066 		if (m->opts[i].opt_action == edit_ptn_custom_type) {
1067 			attr_no = (size_t)i - edit->first_custom_attr;
1068 			if (!edit->pset->parts->pscheme->
1069 			    custom_attribute_writable(
1070 			    edit->pset->parts, edit->id, attr_no))
1071 				continue;
1072 		}
1073 		/* Ok: we want this one */
1074 		m->opts[i].opt_flags &= ~OPT_IGNORE;
1075 	}
1076 }
1077 
1078 static void
1079 draw_edit_ptn_line(menudesc *m, int opt, void *arg)
1080 {
1081 	struct single_part_fs_edit *edit = arg;
1082 	static int col_width;
1083 	static const char *ptn_type, *ptn_start, *ptn_size, *ptn_end,
1084 	     *ptn_newfs, *ptn_mount, *ptn_mount_options, *ptn_mountpt,
1085 	     *ptn_install, *ptn_bsize, *ptn_fsize, *ptn_isize;
1086 	const char *c;
1087 	char val[MENUSTRSIZE];
1088 	const char *attrname;
1089 	size_t attr_no;
1090 
1091 	if (col_width == 0) {
1092 		int l;
1093 
1094 #define	LOAD(STR)	STR = msg_string(MSG_##STR); l = strlen(STR); \
1095 			if (l > col_width) col_width = l
1096 
1097 		LOAD(ptn_type);
1098 		LOAD(ptn_start);
1099 		LOAD(ptn_size);
1100 		LOAD(ptn_end);
1101 		LOAD(ptn_install);
1102 		LOAD(ptn_newfs);
1103 		LOAD(ptn_mount);
1104 		LOAD(ptn_mount_options);
1105 		LOAD(ptn_mountpt);
1106 		LOAD(ptn_bsize);
1107 		LOAD(ptn_fsize);
1108 		LOAD(ptn_isize);
1109 #undef LOAD
1110 
1111 		for (size_t i = 0;
1112 		    i < edit->pset->parts->pscheme->custom_attribute_count;
1113 		    i++) {
1114 			attrname = msg_string(
1115 			    edit->pset->parts->pscheme->custom_attributes[i]
1116 			    .label);
1117 			l = strlen(attrname);
1118 			if (l > col_width) col_width = l;
1119 		}
1120 
1121 		col_width += 3;
1122 	}
1123 
1124 	if (m->opts[opt].opt_flags & OPT_IGNORE
1125 	    && (opt != 3 || edit->info.fs_type == FS_UNUSED)
1126 	    && m->opts[opt].opt_action != edit_ptn_custom_type) {
1127 		wprintw(m->mw, "%*s -", col_width, "");
1128 		return;
1129 	}
1130 
1131 	if (opt < 4) {
1132 		switch (opt) {
1133 		case 0:
1134 			if (edit->info.fs_type == FS_BSDFFS)
1135 				if (edit->info.fs_sub_type == 2)
1136 					c = msg_string(MSG_fs_type_ffsv2);
1137 				else
1138 					c = msg_string(MSG_fs_type_ffs);
1139 			else
1140 				c = getfslabelname(edit->info.fs_type,
1141 				    edit->info.fs_sub_type);
1142 			wprintw(m->mw, "%*s : %s", col_width, ptn_type, c);
1143 			return;
1144 		case 1:
1145 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1146 			    ptn_start, edit->info.start / sizemult, multname);
1147 			return;
1148 		case 2:
1149 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1150 			    ptn_size, edit->info.size / sizemult, multname);
1151 			return;
1152 		case 3:
1153 			wprintw(m->mw, "%*s : %" PRIu64 " %s", col_width,
1154 			    ptn_end, (edit->info.start + edit->info.size)
1155 			    / sizemult, multname);
1156 			return;
1157 		 }
1158 	}
1159 	if (m->opts[opt].opt_action == edit_install) {
1160 		wprintw(m->mw, "%*s : %s", col_width, ptn_install,
1161 			msg_string((edit->info.flags & PTI_INSTALL_TARGET)
1162 			    ? MSG_Yes : MSG_No));
1163 		return;
1164 	}
1165 	if (m->opts[opt].opt_action == edit_fs_preserve) {
1166 		wprintw(m->mw, "%*s : %s", col_width, ptn_newfs,
1167 			msg_string(edit->wanted->instflags & PUIINST_NEWFS
1168 			    ? MSG_Yes : MSG_No));
1169 		return;
1170 	}
1171 	if (m->opts[opt].opt_action == edit_fs_mount) {
1172 		wprintw(m->mw, "%*s : %s", col_width, ptn_mount,
1173 			msg_string(edit->wanted->instflags & PUIINST_MOUNT
1174 			    ? MSG_Yes : MSG_No));
1175 		return;
1176 	}
1177 	if (m->opts[opt].opt_action == edit_fs_ffs_block) {
1178 		wprintw(m->mw, "%*s : %u", col_width, ptn_bsize,
1179 			edit->wanted->fs_opt1);
1180 		return;
1181 	}
1182 	if (m->opts[opt].opt_action == edit_fs_ffs_frag) {
1183 		wprintw(m->mw, "%*s : %u", col_width, ptn_fsize,
1184 			edit->wanted->fs_opt2);
1185 		return;
1186 	}
1187 	if (m->opts[opt].opt_action == edit_fs_ffs_avg_size) {
1188 		if (edit->wanted->fs_opt3 == 0)
1189 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1190 				msg_string(MSG_ptn_isize_dflt));
1191 		else {
1192         	        char buf[24], *line;
1193 			const char *t = buf;
1194 
1195 			snprintf(buf, sizeof buf, "%u", edit->wanted->fs_opt3);
1196 			line = str_arg_subst(msg_string(MSG_ptn_isize_bytes),
1197 			    1, &t);
1198 			wprintw(m->mw, "%*s : %s", col_width, ptn_isize,
1199 				line);
1200 			free(line);
1201 		}
1202 		return;
1203 	}
1204 	if (m->opts[opt].opt_menu == MENU_mountoptions) {
1205 		wprintw(m->mw, "%*s : ", col_width, ptn_mount_options);
1206 		if (edit->wanted->mountflags & PUIMNT_ASYNC)
1207 			wprintw(m->mw, "async ");
1208 		if (edit->wanted->mountflags & PUIMNT_NOATIME)
1209 			wprintw(m->mw, "noatime ");
1210 		if (edit->wanted->mountflags & PUIMNT_NODEV)
1211 			wprintw(m->mw, "nodev ");
1212 		if (edit->wanted->mountflags & PUIMNT_NODEVMTIME)
1213 			wprintw(m->mw, "nodevmtime ");
1214 		if (edit->wanted->mountflags & PUIMNT_NOEXEC)
1215 			wprintw(m->mw, "noexec ");
1216 		if (edit->wanted->mountflags & PUIMNT_NOSUID)
1217 			wprintw(m->mw, "nosuid ");
1218 		if (edit->wanted->mountflags & PUIMNT_LOG)
1219 			wprintw(m->mw, "log ");
1220 		if (edit->wanted->mountflags & PUIMNT_NOAUTO)
1221 			wprintw(m->mw, "noauto  ");
1222 		return;
1223 	}
1224 	if (m->opts[opt].opt_action == edit_fs_mountpt) {
1225 		wprintw(m->mw, "%*s : %s", col_width, ptn_mountpt,
1226 		    edit->wanted->mount);
1227 		return;
1228 	}
1229 
1230 	attr_no = opt - edit->first_custom_attr;
1231 	edit->pset->parts->pscheme->format_custom_attribute(
1232 	    edit->pset->parts, edit->id, attr_no, &edit->info,
1233 	    val, sizeof val);
1234 	attrname = msg_string(edit->pset->parts->pscheme->
1235 	    custom_attributes[attr_no].label);
1236 	wprintw(m->mw, "%*s : %s", col_width, attrname, val);
1237 }
1238 
1239 static int
1240 edit_ptn_custom_type(menudesc *m, void *arg)
1241 {
1242 	struct single_part_fs_edit *edit = arg;
1243 	size_t attr_no = m->cursel - edit->first_custom_attr;
1244 	char line[STRSIZE];
1245 
1246 	switch (edit->pset->parts->pscheme->custom_attributes[attr_no].type) {
1247 	case pet_bool:
1248 		edit->pset->parts->pscheme->custom_attribute_toggle(
1249 		    edit->pset->parts, edit->id, attr_no);
1250 		break;
1251 	case pet_cardinal:
1252 	case pet_str:
1253 		edit->pset->parts->pscheme->format_custom_attribute(
1254 		    edit->pset->parts, edit->id, attr_no, &edit->info,
1255 		    line, sizeof(line));
1256 		msg_prompt_win(
1257 		    edit->pset->parts->pscheme->custom_attributes[attr_no].
1258 		    label, -1, 18, 0, 0, line, line, sizeof(line));
1259 		edit->pset->parts->pscheme->custom_attribute_set_str(
1260 		    edit->pset->parts, edit->id, attr_no, line);
1261 		break;
1262 	}
1263 
1264 	return 0;
1265 }
1266 
1267 
1268 /*
1269  * Some column width depend on translation, we will set these in
1270  * fmt_fspart_header and later use it when formatting single entries
1271  * in fmt_fspart_row.
1272  * The table consist of 3 "size like" columns, all fixed width, then
1273  * ptnheaders_fstype, part_header_col_flag, and finally the mount point
1274  * (which is variable width).
1275  */
1276 static int fstype_width, flags_width;
1277 static char fspart_separator[MENUSTRSIZE];
1278 static char fspart_title[2*MENUSTRSIZE];
1279 
1280 /*
1281  * Format the header of the main partition editor menu.
1282  */
1283 static void
1284 fmt_fspart_header(menudesc *menu, void *arg)
1285 {
1286 	struct partition_usage_set *pset = arg;
1287 	char total[6], free_space[6], scol[13], ecol[13], szcol[13],
1288 	    sepline[MENUSTRSIZE], *p, desc[MENUSTRSIZE];
1289 	const char *fstype, *flags;
1290 	int i;
1291 	size_t ptn;
1292 	bool with_clone, with_inst_flag = pset->parts->parent == NULL;
1293 
1294 	with_clone = false;
1295 	for (ptn = 0; ptn < pset->num && !with_clone; ptn++)
1296 		if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1297 			with_clone = true;
1298 	humanize_number(total, sizeof total,
1299 	    pset->parts->disk_size * pset->parts->bytes_per_sector,
1300 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1301 	humanize_number(free_space, sizeof free_space,
1302 	    pset->cur_free_space * pset->parts->bytes_per_sector,
1303 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
1304 
1305 	if (with_clone)
1306 		strlcpy(desc, msg_string(MSG_clone_flag_desc), sizeof desc);
1307 	else
1308 		desc[0] = 0;
1309 	if (pset->parts->pscheme->part_flag_desc)
1310 		strlcat(desc, msg_string(pset->parts->pscheme->part_flag_desc),
1311 		sizeof desc);
1312 
1313 	msg_display_subst(MSG_fspart, 7, pset->parts->disk,
1314 	    msg_string(pset->parts->pscheme->name),
1315 	    msg_string(pset->parts->pscheme->short_name),
1316 	    with_inst_flag ? msg_string(MSG_ptn_instflag_desc) : "",
1317 	    desc, total, free_space);
1318 
1319 	snprintf(scol, sizeof scol, "%s (%s)",
1320 	    msg_string(MSG_ptnheaders_start), multname);
1321 	snprintf(ecol, sizeof ecol, "%s (%s)",
1322 	    msg_string(MSG_ptnheaders_end), multname);
1323 	snprintf(szcol, sizeof szcol, "%s (%s)",
1324 	    msg_string(MSG_ptnheaders_size), multname);
1325 
1326 	fstype = msg_string(MSG_ptnheaders_fstype);
1327 	flags = msg_string(MSG_part_header_col_flag);
1328 	fstype_width = max(strlen(fstype), 8);
1329 	flags_width = strlen(flags);
1330 	for (i = 0, p = sepline; i < fstype_width; i++)
1331 		*p++ = '-';
1332 	for (i = 0, *p++ = ' '; i < flags_width; i++)
1333 		*p++ = '-';
1334 	*p = 0;
1335 
1336 	snprintf(fspart_separator, sizeof(fspart_separator),
1337 	    "------------ ------------ ------------ %s ----------------",
1338 	    sepline);
1339 
1340 	snprintf(fspart_title, sizeof(fspart_title),
1341 	    "   %12.12s %12.12s %12.12s %*s %*s %s\n"
1342 	    "   %s", scol, ecol, szcol, fstype_width, fstype,
1343 	    flags_width, flags, msg_string(MSG_ptnheaders_filesystem),
1344 	    fspart_separator);
1345 
1346 	msg_table_add("\n\n");
1347 }
1348 
1349 /*
1350  * Format one partition entry in the main partition editor.
1351  */
1352 static void
1353 fmt_fspart_row(menudesc *m, int ptn, void *arg)
1354 {
1355 	struct partition_usage_set *pset = arg;
1356 	struct disk_part_info info;
1357 	daddr_t poffset, psize, pend;
1358 	const char *desc;
1359 	static const char *Yes;
1360 	char flag_str[MENUSTRSIZE], *fp;
1361 	unsigned inst_flags;
1362 #ifndef NO_CLONES
1363 	size_t clone_cnt;
1364 #endif
1365 	bool with_inst_flag = pset->parts->parent == NULL;
1366 
1367 	if (Yes == NULL)
1368 		Yes = msg_string(MSG_Yes);
1369 
1370 #ifndef NO_CLONES
1371 	if ((pset->infos[ptn].flags & PUIFLG_CLONE_PARTS) &&
1372 	   pset->infos[ptn].cur_part_id == NO_PART) {
1373 		psize = pset->infos[ptn].size / sizemult;
1374 		if (pset->infos[ptn].clone_ndx <
1375 		    pset->infos[ptn].clone_src->num_sel)
1376 			clone_cnt = 1;
1377 		else
1378 			clone_cnt = pset->infos[ptn].clone_src->num_sel;
1379 		if (pset->infos[ptn].cur_part_id == NO_PART)
1380 			wprintw(m->mw, "                          %12" PRIu64
1381 			    " [%zu %s]", psize, clone_cnt,
1382 			    msg_string(MSG_clone_target_disp));
1383 		else {
1384 			poffset = pset->infos[ptn].cur_start / sizemult;
1385 			pend = (pset->infos[ptn].cur_start +
1386 			    pset->infos[ptn].size) / sizemult - 1;
1387 			wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1388 			    " [%zu %s]",
1389 			    poffset, pend, psize, clone_cnt,
1390 			    msg_string(MSG_clone_target_disp));
1391 		}
1392 		if (m->title == fspart_title)
1393 			m->opts[ptn].opt_flags |= OPT_IGNORE;
1394 		else
1395 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1396 		return;
1397 	}
1398 #endif
1399 
1400 	if (!real_partition(pset, ptn))
1401 		return;
1402 
1403 	if (!pset->parts->pscheme->get_part_info(pset->parts,
1404 	    pset->infos[ptn].cur_part_id, &info))
1405 		return;
1406 
1407 	/*
1408 	 * We use this function in multiple menus, but only want it
1409 	 * to play with enable/disable in a single one:
1410 	 */
1411 	if (m->title == fspart_title) {
1412 		/*
1413 		 * Enable / disable this line if it is something
1414 		 * like RAW_PART
1415 		 */
1416 		if ((info.flags &
1417 		    (PTI_WHOLE_DISK|PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
1418 		    || (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS))
1419 			m->opts[ptn].opt_flags |= OPT_IGNORE;
1420 		else
1421 			m->opts[ptn].opt_flags &= ~OPT_IGNORE;
1422 	}
1423 
1424 	poffset = info.start / sizemult;
1425 	psize = info.size / sizemult;
1426 	if (psize == 0)
1427 		pend = 0;
1428 	else
1429 		pend = (info.start + info.size) / sizemult - 1;
1430 
1431 	if (info.flags & PTI_WHOLE_DISK)
1432 		desc = msg_string(MSG_NetBSD_partition_cant_change);
1433 	else if (info.flags & PTI_RAW_PART)
1434 		desc = msg_string(MSG_Whole_disk_cant_change);
1435 	else if (info.flags & PTI_BOOT)
1436 		desc = msg_string(MSG_Boot_partition_cant_change);
1437 	else
1438 		desc = getfslabelname(info.fs_type, info.fs_sub_type);
1439 
1440 	fp = flag_str;
1441 	inst_flags = pset->infos[ptn].instflags;
1442 	if (with_inst_flag && (info.flags & PTI_INSTALL_TARGET) &&
1443 	    info.nat_type->generic_ptype == PT_root) {
1444 		static char inst_flag;
1445 
1446 		if (inst_flag == 0)
1447 			inst_flag = msg_string(MSG_install_flag)[0];
1448 		*fp++ = inst_flag;
1449 	}
1450 	if (inst_flags & PUIINST_NEWFS)
1451 		*fp++ = msg_string(MSG_newfs_flag)[0];
1452 	if (pset->infos[ptn].flags & PUIFLG_CLONE_PARTS)
1453 		*fp++ = msg_string(MSG_clone_flag)[0];
1454 	*fp = 0;
1455 	if (pset->parts->pscheme->get_part_attr_str != NULL)
1456 		pset->parts->pscheme->get_part_attr_str(pset->parts,
1457 		    pset->infos[ptn].cur_part_id, fp, sizeof(flag_str)-1);
1458 
1459 	/* if the fstype description does not fit, check if we can overrun */
1460 	if (strlen(desc) > (size_t)fstype_width &&
1461 	     flag_str[0] == 0 && (info.last_mounted == NULL ||
1462 	     info.last_mounted[0] == 0))
1463 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1464 		    " %s",
1465 		    poffset, pend, psize, desc);
1466 	else
1467 		wprintw(m->mw, "%12" PRIu64 " %12" PRIu64 " %12" PRIu64
1468 		    " %*.*s %*s %s",
1469 		    poffset, pend, psize, fstype_width, fstype_width, desc,
1470 		    -flags_width, flag_str,
1471 		    (inst_flags & PUIINST_MOUNT) && info.last_mounted &&
1472 		     info.last_mounted[0] ? info.last_mounted : "");
1473 }
1474 
1475 #ifndef NO_CLONES
1476 static int
1477 part_ext_clone(menudesc *m, void *arg)
1478 {
1479 	struct selected_partitions selected, *clone_src;
1480 	struct clone_target_menu_data data;
1481 	struct partition_usage_set *pset = arg;
1482 	struct part_usage_info *p;
1483 	struct disk_part_info sinfo, cinfo;
1484 	struct disk_partitions *csrc;
1485 	struct disk_part_free_space space;
1486 	menu_ent *men;
1487 	daddr_t clone_size, free_size, offset, align;
1488 	int num_men, i;
1489 	size_t s, clone_cnt;
1490 	part_id cid;
1491 	struct clone_data {
1492 		struct disk_part_info info;
1493 		part_id new_id;
1494 		size_t ndx;
1495 	};
1496 	struct clone_data *clones = NULL;
1497 
1498 	if (!select_partitions(&selected, pm->parts))
1499 		return 0;
1500 
1501 	clone_size = selected_parts_size(&selected);
1502 	num_men = pset->num+1;
1503 	men = calloc(num_men, sizeof *men);
1504 	if (men == NULL)
1505 		return 0;
1506 	for (i = 0; i < num_men; i++) {
1507 		men[i].opt_action = clone_target_select;
1508 		if (i == 0)
1509 			free_size = pset->infos[i].cur_start;
1510 		else if (i > 0 && (size_t)i < pset->num)
1511 			free_size = pset->infos[i].cur_start -
1512 			    pset->infos[i-1].cur_start - pset->infos[i-1].size;
1513 		else
1514 			free_size = pset->parts->free_space;
1515 		if (free_size < clone_size)
1516 			men[i].opt_flags = OPT_IGNORE;
1517 	}
1518 	men[num_men-1].opt_name = MSG_clone_target_end;
1519 
1520 	memset(&data, 0, sizeof data);
1521 	data.usage = *pset;
1522 	data.res = -1;
1523 
1524 	data.usage.menu = new_menu(MSG_clone_target_hdr,
1525 	    men, num_men, 3, 2, 0, 65, MC_SCROLL,
1526 	    NULL, fmt_fspart_row, NULL, NULL, MSG_cancel);
1527 	process_menu(data.usage.menu, &data);
1528 	free_menu(data.usage.menu);
1529 	free(men);
1530 
1531 	if (data.res < 0)
1532 		goto err;
1533 
1534 	/* create temporary infos for all clones that work out */
1535 	clone_cnt = 0;
1536 	clones = calloc(selected.num_sel, sizeof(*clones));
1537 	if (clones == NULL)
1538 		goto err;
1539 
1540 	clone_src = malloc(sizeof(selected));
1541 	if (clone_src == NULL)
1542 		goto err;
1543 	*clone_src = selected;
1544 
1545 	/* find selected offset from data.res and insert clones there */
1546 	align = pset->parts->pscheme->get_part_alignment(pset->parts);
1547 	offset = -1;
1548 	if (data.res > 0)
1549 		offset = pset->infos[data.res-1].cur_start
1550 		    + pset->infos[data.res-1].size;
1551 	else
1552 		offset = 0;
1553 	for (s = 0; s < selected.num_sel; s++) {
1554 		csrc = selected.selection[s].parts;
1555 		cid = selected.selection[s].id;
1556 		csrc->pscheme->get_part_info(csrc, cid, &sinfo);
1557 		if (!pset->parts->pscheme->adapt_foreign_part_info(
1558 		    pset->parts, &cinfo, csrc->pscheme, &sinfo))
1559 			continue;
1560 		size_t cnt = pset->parts->pscheme->get_free_spaces(
1561 		    pset->parts, &space, 1, cinfo.size-align, align,
1562 		    offset, -1);
1563 		if (cnt == 0)
1564 			continue;
1565 		cinfo.start = space.start;
1566 		cid = pset->parts->pscheme->add_partition(
1567 		    pset->parts, &cinfo, NULL);
1568 		if (cid == NO_PART)
1569 			continue;
1570 		pset->parts->pscheme->get_part_info(pset->parts, cid, &cinfo);
1571 		clones[clone_cnt].info = cinfo;
1572 		clones[clone_cnt].new_id = cid;
1573 		clones[clone_cnt].ndx = s;
1574 		clone_cnt++;
1575 		offset = roundup(cinfo.start+cinfo.size, align);
1576 	}
1577 
1578 	/* insert new clone records at offset data.res */
1579 	men = realloc(m->opts, (m->numopts+clone_cnt)*sizeof(*m->opts));
1580 	if (men == NULL)
1581 		goto err;
1582 	pset->menu_opts = men;
1583 	m->opts = men;
1584 	m->numopts += clone_cnt;
1585 
1586 	p = realloc(pset->infos, (pset->num+clone_cnt)*sizeof(*pset->infos));
1587 	if (p == NULL)
1588 		goto err;
1589 	pset->infos = p;
1590 
1591 	men += data.res;
1592 	p += data.res;
1593 	memmove(men+clone_cnt, men,
1594 	    sizeof(*men)*(m->numopts-data.res-clone_cnt));
1595 	if (pset->num > (size_t)data.res)
1596 		memmove(p+clone_cnt, p, sizeof(*p)*(pset->num-data.res));
1597 	memset(men, 0, sizeof(*men)*clone_cnt);
1598 	memset(p, 0, sizeof(*p)*clone_cnt);
1599 	for (s = 0; s < clone_cnt; s++) {
1600 		p[s].cur_part_id = clones[s].new_id;
1601 		p[s].cur_start = clones[s].info.start;
1602 		p[s].size = clones[s].info.size;
1603 		p[s].cur_flags = clones[s].info.flags;
1604 		p[s].flags = PUIFLG_CLONE_PARTS;
1605 		p[s].parts = pset->parts;
1606 		p[s].clone_src = clone_src;
1607 		p[s].clone_ndx = s;
1608 	}
1609 	free(clones);
1610 	m->cursel = ((size_t)data.res >= pset->num) ? 0 : data.res+clone_cnt;
1611 	pset->num += clone_cnt;
1612 	m->h = 0;
1613 	resize_menu_height(m);
1614 
1615 	return -1;
1616 
1617 err:
1618 	free(clones);
1619 	free_selected_partitions(&selected);
1620 	return 0;
1621 }
1622 #endif
1623 
1624 static int
1625 edit_fspart_pack(menudesc *m, void *arg)
1626 {
1627 	struct partition_usage_set *pset = arg;
1628 	char buf[STRSIZE];
1629 
1630 	if (!pset->parts->pscheme->get_disk_pack_name(pset->parts,
1631 	    buf, sizeof buf))
1632 		return 0;
1633 
1634 	msg_prompt_win(MSG_edit_disk_pack_hdr,
1635 	    -1, 18, 0, -1, buf, buf, sizeof(buf));
1636 
1637 	pset->parts->pscheme->set_disk_pack_name(pset->parts, buf);
1638 	return 0;
1639 }
1640 
1641 static int
1642 edit_fspart_add(menudesc *m, void *arg)
1643 {
1644 	struct partition_usage_set *pset = arg;
1645 	struct part_usage_info *ninfo;
1646 	menu_ent *nmenopts;
1647 	size_t cnt, off;
1648 
1649 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1650 	if (ninfo == NULL)
1651 		return 0;
1652 	pset->infos = ninfo;
1653 	off = pset->parts->num_part;
1654 	cnt = pset->num-pset->parts->num_part;
1655 	if (cnt > 0)
1656 		memmove(pset->infos+off+1,pset->infos+off,
1657 		    cnt*sizeof(*pset->infos));
1658 	memset(pset->infos+off, 0, sizeof(*pset->infos));
1659 
1660 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1661 	if (nmenopts == NULL)
1662 		return 0;
1663 	memmove(nmenopts+off+1, nmenopts+off,
1664 	    (m->numopts-off)*sizeof(*nmenopts));
1665 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1666 	nmenopts[off].opt_action = edit_ptn;
1667 	pset->menu_opts = m->opts = nmenopts;
1668 	m->numopts++;
1669 	m->cursel = off;
1670 	pset->num++;
1671 
1672 	/* now update edit menu to fit */
1673 	m->h = 0;
1674 	resize_menu_height(m);
1675 
1676 	/* and directly invoke the partition editor for the new part */
1677 	edit_ptn(m, arg);
1678 
1679 	show_partition_adder(m, pset);
1680 
1681 	return -1;
1682 }
1683 
1684 static void
1685 add_partition_adder(menudesc *m, struct partition_usage_set *pset)
1686 {
1687 	struct part_usage_info *ninfo;
1688 	menu_ent *nmenopts;
1689 	size_t off;
1690 
1691 	ninfo = realloc(pset->infos, (pset->num+1)*sizeof(*pset->infos));
1692 	if (ninfo == NULL)
1693 		return;
1694 	pset->infos = ninfo;
1695 	off = pset->parts->num_part+1;
1696 
1697 	nmenopts = realloc(m->opts, (m->numopts+1)*sizeof(*m->opts));
1698 	if (nmenopts == NULL)
1699 		return;
1700 	memmove(nmenopts+off+1, nmenopts+off,
1701 	    (m->numopts-off)*sizeof(*nmenopts));
1702 	memset(&nmenopts[off], 0, sizeof(nmenopts[off]));
1703 
1704 	nmenopts[off].opt_name = MSG_addpart;
1705 	nmenopts[off].opt_flags = OPT_SUB;
1706 	nmenopts[off].opt_action = edit_fspart_add;
1707 
1708 	m->opts = nmenopts;
1709 	m->numopts++;
1710 }
1711 
1712 static void
1713 remove_partition_adder(menudesc *m, struct partition_usage_set *pset)
1714 {
1715 	size_t off;
1716 
1717 	off = pset->parts->num_part+1;
1718 	memmove(m->opts+off, m->opts+off+1,
1719 	    (m->numopts-off-1)*sizeof(*m->opts));
1720 	m->numopts--;
1721 }
1722 
1723 /*
1724  * Called whenever the "add a partition" option may need to be removed
1725  * or added
1726  */
1727 static void
1728 show_partition_adder(menudesc *m, struct partition_usage_set *pset)
1729 {
1730 	if (m->opts == NULL)
1731 		return;
1732 
1733 	bool can_add_partition = pset->parts->pscheme->can_add_partition(
1734 	    pset->parts);
1735 	bool part_adder_present =
1736 	    (m->opts[pset->parts->num_part].opt_flags & OPT_IGNORE) &&
1737 	    (m->opts[pset->parts->num_part+1].opt_action == edit_fspart_add);
1738 
1739 	if (can_add_partition == part_adder_present)
1740 		return;
1741 
1742 	if (can_add_partition)
1743 		add_partition_adder(m, pset);
1744 	else
1745 		remove_partition_adder(m, pset);
1746 
1747 	/* now update edit menu to fit */
1748 	m->h = 0;
1749 	resize_menu_height(m);
1750 }
1751 
1752 static int
1753 edit_fspart_abort(menudesc *m, void *arg)
1754 {
1755 	struct partition_usage_set *pset = arg;
1756 
1757 	pset->ok = false;
1758 	return 1;
1759 }
1760 
1761 /*
1762  * Check a disklabel.
1763  * If there are overlapping active partitions,
1764  * Ask the user if they want to edit the partition or give up.
1765  */
1766 int
1767 edit_and_check_label(struct pm_devs *p, struct partition_usage_set *pset,
1768     bool install)
1769 {
1770 	menu_ent *op;
1771 	size_t cnt, i;
1772 	bool may_add = pset->parts->pscheme->can_add_partition(pset->parts);
1773 	bool may_edit_pack =
1774 	    pset->parts->pscheme->get_disk_pack_name != NULL &&
1775 	    pset->parts->pscheme->set_disk_pack_name != NULL;
1776 
1777 #ifdef NO_CLONES
1778 #define	C_M_ITEMS	0
1779 #else
1780 #define	C_M_ITEMS	1
1781 #endif
1782 	pset->menu_opts = calloc(pset->parts->num_part
1783 	     +3+C_M_ITEMS+may_add+may_edit_pack,
1784 	     sizeof *pset->menu_opts);
1785 	if (pset->menu_opts == NULL)
1786 		return 0;
1787 
1788 	op = pset->menu_opts;
1789 	for (i = 0; i < pset->parts->num_part; i++) {
1790 		op->opt_action = edit_ptn;
1791 		op++;
1792 	}
1793 	/* separator line between partitions and actions */
1794 	op->opt_name = fspart_separator;
1795 	op->opt_flags = OPT_IGNORE|OPT_NOSHORT;
1796 	op++;
1797 
1798 	/* followed by new partition adder */
1799 	if (may_add) {
1800 		op->opt_name = MSG_addpart;
1801 		op->opt_flags = OPT_SUB;
1802 		op->opt_action = edit_fspart_add;
1803 		op++;
1804 	}
1805 
1806 	/* and unit changer */
1807 	op->opt_name = MSG_askunits;
1808 	op->opt_menu = MENU_sizechoice;
1809 	op->opt_flags = OPT_SUB;
1810 	op->opt_action = NULL;
1811 	op++;
1812 
1813 	if (may_edit_pack) {
1814 		op->opt_name = MSG_editpack;
1815 		op->opt_flags = OPT_SUB;
1816 		op->opt_action = edit_fspart_pack;
1817 		op++;
1818 	}
1819 
1820 #ifndef NO_CLONES
1821 	/* add a clone-from-elsewhere option */
1822 	op->opt_name = MSG_clone_from_elsewhere;
1823 	op->opt_action = part_ext_clone;
1824 	op++;
1825 #endif
1826 
1827 	/* and abort option */
1828 	op->opt_name = MSG_cancel;
1829 	op->opt_flags = OPT_EXIT;
1830 	op->opt_action = edit_fspart_abort;
1831 	op++;
1832 	cnt = op - pset->menu_opts;
1833 	assert(cnt == pset->parts->num_part+3+C_M_ITEMS+may_add+may_edit_pack);
1834 
1835 	pset->menu = new_menu(fspart_title, pset->menu_opts, cnt,
1836 			0, -1, 0, 74,
1837 			MC_ALWAYS_SCROLL|MC_NOBOX|MC_DFLTEXIT|
1838 			MC_NOCLEAR|MC_CONTINUOUS,
1839 			fmt_fspart_header, fmt_fspart_row, NULL, NULL,
1840 			MSG_partition_sizes_ok);
1841 
1842 	if (pset->menu < 0) {
1843 		free(pset->menu_opts);
1844 		pset->menu_opts = NULL;
1845 		return 0;
1846 	}
1847 
1848 	p->current_cylsize = p->dlcylsize;
1849 
1850 	for (;;) {
1851 		/* first give the user the option to edit the label... */
1852 		pset->ok = true;
1853 		process_menu(pset->menu, pset);
1854 		if (!pset->ok) {
1855 			i = 0;
1856 			break;
1857 		}
1858 
1859 		/* User thinks the label is OK. */
1860 		i = verify_parts(pset, install);
1861 		if (i == 1)
1862 			continue;
1863 		break;
1864 	}
1865 	free(pset->menu_opts);
1866 	pset->menu_opts = NULL;
1867 	free_menu(pset->menu);
1868 	pset->menu = -1;
1869 
1870 	return i != 0;
1871 }
1872 
1873 /*
1874  * strip trailing / to avoid confusion in path comparisons later
1875  */
1876 void
1877 canonicalize_last_mounted(char *path)
1878 {
1879 	char *p;
1880 
1881 	if (path == NULL)
1882 		return;
1883 
1884 	if (strcmp(path, "/") == 0)
1885 		return;	/* in this case a "trailing" slash is allowed */
1886 
1887 	for (;;) {
1888 		p = strrchr(path, '/');
1889 		if (p == NULL)
1890 			return;
1891 		if (p[1] != 0)
1892 			return;
1893 		p[0] = 0;
1894 	}
1895 }
1896 
1897 /*
1898  * Try to get 'last mounted on' (or equiv) from fs superblock.
1899  */
1900 const char *
1901 get_last_mounted(int fd, daddr_t partstart, uint *fs_type, uint *fs_sub_type,
1902     uint flags)
1903 {
1904 	static char sblk[SBLOCKSIZE];		/* is this enough? */
1905 	struct fs *SB = (struct fs *)sblk;
1906 	static const off_t sblocks[] = SBLOCKSEARCH;
1907 	const off_t *sbp;
1908 	const char *mnt = NULL;
1909 	int len;
1910 
1911 	if (fd == -1)
1912 		return "";
1913 
1914 	if (fs_type)
1915 		*fs_type = 0;
1916 	if (fs_sub_type)
1917 		*fs_sub_type = 0;
1918 
1919 	/* Check UFS1/2 (and hence LFS) superblock */
1920 	for (sbp = sblocks; mnt == NULL && *sbp != -1; sbp++) {
1921 		if (pread(fd, sblk, sizeof sblk,
1922 		    (off_t)partstart * (off_t)512 + *sbp) != sizeof sblk)
1923 			continue;
1924 
1925 		/*
1926 		 * If start of partition and allowed by flags check
1927 		 * for other fs types
1928 		 */
1929 		if (*sbp == 0 && (flags & GLM_MAYBE_FAT32) &&
1930 		    sblk[0x42] == 0x29 && memcmp(sblk + 0x52, "FAT", 3) == 0) {
1931 			/* Probably a FAT filesystem, report volume name */
1932 			size_t i;
1933 			for (i = 0x51; i >= 0x47; i--) {
1934 				if (sblk[i] != ' ')
1935 					break;
1936 				sblk[i] = 0;
1937 			}
1938 			sblk[0x52] = 0;
1939 			if (fs_type)
1940 				*fs_type = FS_MSDOS;
1941 			if (fs_sub_type)
1942 				*fs_sub_type = sblk[0x53];
1943 			return sblk + 0x47;
1944 		} else if (*sbp == 0 && (flags & GLM_MAYBE_NTFS) &&
1945 		    memcmp(sblk+3, "NTFS    ", 8) == 0) {
1946 			if (fs_type)
1947 				*fs_type = FS_NTFS;
1948 			if (fs_sub_type)
1949 				*fs_sub_type = MBR_PTYPE_NTFS;
1950 			/* XXX dig for volume name attribute ? */
1951 			return "";
1952 		}
1953 
1954 		if (!(flags & GLM_LIKELY_FFS))
1955 			continue;
1956 
1957 		/* Maybe we should validate the checksum??? */
1958 		switch (SB->fs_magic) {
1959 		case FS_UFS1_MAGIC:
1960 		case FS_UFS1_MAGIC_SWAPPED:
1961 			if (!(SB->fs_old_flags & FS_FLAGS_UPDATED)) {
1962 				if (*sbp == SBLOCK_UFS1)
1963 					mnt = (const char *)SB->fs_fsmnt;
1964 			} else {
1965 				/* Check we have the main superblock */
1966 				if (SB->fs_sblockloc == *sbp)
1967 					mnt = (const char *)SB->fs_fsmnt;
1968 			}
1969 			if (fs_type)
1970 				*fs_type = FS_BSDFFS;
1971 			if (fs_sub_type)
1972 				*fs_sub_type = 1;
1973 			continue;
1974 		case FS_UFS2_MAGIC:
1975 		case FS_UFS2_MAGIC_SWAPPED:
1976 			/* Check we have the main superblock */
1977 			if (SB->fs_sblockloc == *sbp) {
1978 				mnt = (const char *)SB->fs_fsmnt;
1979 				if (fs_type)
1980 					*fs_type = FS_BSDFFS;
1981 				if (fs_sub_type)
1982 					*fs_sub_type = 2;
1983 			}
1984 			continue;
1985 		}
1986 	}
1987 
1988 	if (mnt == NULL)
1989 		return "";
1990 
1991 	/* If sysinst mounted this last then strip prefix */
1992 	len = strlen(targetroot_mnt);
1993 	if (memcmp(mnt, targetroot_mnt, len) == 0) {
1994 		if (mnt[len] == 0)
1995 			return "/";
1996 		if (mnt[len] == '/')
1997 			return mnt + len;
1998 	}
1999 	return mnt;
2000 	#undef SB
2001 }
2002 
2003 /* Ask for a partition offset, check bounds and do the needed roundups */
2004 daddr_t
2005 getpartoff(struct disk_partitions *parts, daddr_t defpartstart)
2006 {
2007 	char defstart[24], isize[24], maxpart, minspace, maxspace,
2008 	    *prompt, *label_msg, valid_parts[4], valid_spaces[4],
2009 	    space_prompt[1024], *head, *hint_part, *hint_space, *tail;
2010 	size_t num_freespace, spaces, ndx;
2011 	struct disk_part_free_space *freespace;
2012 	daddr_t i, localsizemult, ptn_alignment, min, max;
2013 	part_id partn;
2014 	struct disk_part_info info;
2015 	const char *errmsg = NULL;
2016 
2017 	min = parts->disk_start;
2018 	max = min + parts->disk_size;
2019 
2020 	/* upper bound on the number of free spaces, plus some slope */
2021 	num_freespace = parts->num_part * 2 + 5;
2022 	freespace = calloc(num_freespace, sizeof(*freespace));
2023 	if (freespace == NULL)
2024 		return -1;
2025 
2026 	ptn_alignment = parts->pscheme->get_part_alignment(parts);
2027 	spaces = parts->pscheme->get_free_spaces(parts, freespace,
2028 	    num_freespace, max(sizemult, ptn_alignment), ptn_alignment, -1,
2029 	    defpartstart);
2030 
2031 	maxpart = 'a' + parts->num_part -1;
2032 	if (parts->num_part > 1) {
2033 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpart);
2034 	} else if (parts->num_part == 1) {
2035 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpart);
2036 	} else {
2037 		strcpy(valid_parts, "---");
2038 	}
2039 	if (spaces > 1) {
2040 		minspace = maxpart + 1;
2041 		maxspace = minspace + spaces -1;
2042 		snprintf(valid_spaces, sizeof valid_spaces, "%c-%c", minspace,
2043 		    maxspace);
2044 	} else if (spaces == 1) {
2045 		maxspace = minspace = maxpart + 1;
2046 		snprintf(valid_spaces, sizeof valid_spaces, "  %c", minspace);
2047 	} else {
2048 		minspace = 0;
2049 		maxspace = 0;
2050 		strcpy(valid_spaces, "---");
2051 	}
2052 
2053 	/* Add description of start/size to user prompt */
2054 	const char *mstr = msg_string(MSG_free_space_line);
2055         space_prompt[0] = 0;
2056         for (ndx = 0; ndx < spaces; ndx++) {
2057                 char str_start[40], str_end[40], str_size[40], str_tag[4];
2058 
2059 		sprintf(str_tag, "%c: ", minspace+(int)ndx);
2060                 sprintf(str_start, "%" PRIu64, freespace[ndx].start / sizemult);
2061                 sprintf(str_end, "%" PRIu64,
2062                     (freespace[ndx].start + freespace[ndx].size) / sizemult);
2063                 sprintf(str_size, "%" PRIu64, freespace[ndx].size / sizemult);
2064                 const char *args[4] = { str_start, str_end, str_size,
2065                     multname };
2066                 char *line = str_arg_subst(mstr, 4, args);
2067 		strlcat(space_prompt, str_tag, sizeof(space_prompt));
2068                 size_t len = strlcat(space_prompt, line, sizeof(space_prompt));
2069                 free (line);
2070                 if (len >= sizeof space_prompt)
2071                         break;
2072         }
2073 
2074 	const char *args[] = { valid_parts, valid_spaces, multname };
2075 	hint_part = NULL;
2076 	hint_space = NULL;
2077 	head = str_arg_subst(msg_string(MSG_label_offset_head),
2078 	    __arraycount(args), args);
2079 	if (parts->num_part)
2080 		hint_part = str_arg_subst(msg_string(
2081 		    MSG_label_offset_part_hint), __arraycount(args), args);
2082 	if (spaces)
2083 		hint_space = str_arg_subst(msg_string(
2084 		    MSG_label_offset_space_hint), __arraycount(args), args);
2085 	tail = str_arg_subst(msg_string(MSG_label_offset_tail),
2086 	    __arraycount(args), args);
2087 
2088 	if (hint_part && hint_space)
2089 		asprintf(&label_msg, "%s\n%s\n%s\n\n%s\n%s",
2090 		    head, hint_part, hint_space, space_prompt, tail);
2091 	else if (hint_part)
2092 		asprintf(&label_msg, "%s\n%s\n\n%s",
2093 		    head, hint_part, tail);
2094 	else if (hint_space)
2095 		asprintf(&label_msg, "%s\n%s\n\n%s\n%s",
2096 		    head, hint_space, space_prompt, tail);
2097 	else
2098 		asprintf(&label_msg, "%s\n\n%s",
2099 		    head, tail);
2100 	free(head); free(hint_part); free(hint_space); free(tail);
2101 
2102 	localsizemult = sizemult;
2103 	errmsg = NULL;
2104 	for (;;) {
2105 		snprintf(defstart, sizeof defstart, "%" PRIu64,
2106 		    defpartstart/sizemult);
2107 		if (errmsg != NULL && errmsg[0] != 0)
2108 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2109 		else
2110 			prompt = label_msg;
2111 		msg_prompt_win(prompt, -1, 13, 70, -1,
2112 		    (defpartstart > 0) ? defstart : NULL, isize, sizeof isize);
2113 		if (label_msg != prompt)
2114 			free(prompt);
2115 		if (strcmp(defstart, isize) == 0) {
2116 			/* Don't do rounding if default accepted */
2117 			i = defpartstart;
2118 			break;
2119 		}
2120 		if (isize[1] == '\0' && isize[0] >= 'a' &&
2121 		    isize[0] <= maxpart) {
2122 			partn = isize[0] - 'a';
2123 			if (parts->pscheme->get_part_info(parts, partn,
2124 			    &info)) {
2125 				i = info.start + info.size;
2126 				localsizemult = 1;
2127 			} else {
2128 				errmsg = msg_string(MSG_invalid_sector_number);
2129 				continue;
2130 			}
2131 		} else if (isize[1] == '\0' && isize[0] >= minspace &&
2132 		    isize[0] <= maxspace) {
2133 			ndx = isize[0] - minspace;
2134 			i = freespace[ndx].start;
2135 			localsizemult = 1;
2136 		} else if (atoi(isize) == -1) {
2137 			i = min;
2138 			localsizemult = 1;
2139 		} else {
2140 			i = parse_disk_pos(isize, &localsizemult,
2141 			    parts->bytes_per_sector,
2142 			    parts->pscheme->get_cylinder_size(parts), NULL);
2143 			if (i < 0) {
2144 				errmsg = msg_string(MSG_invalid_sector_number);
2145 				continue;
2146 			}
2147 		}
2148 		/* round to cylinder size if localsizemult != 1 */
2149 		int cylsize = parts->pscheme->get_cylinder_size(parts);
2150 		i = NUMSEC(i, localsizemult, cylsize);
2151 		/* Adjust to start of slice if needed */
2152 		if ((i < min && (min - i) < localsizemult) ||
2153 		    (i > min && (i - min) < localsizemult)) {
2154 			i = min;
2155 		}
2156 		if (max == 0 || i <= max)
2157 			break;
2158 		errmsg = msg_string(MSG_startoutsidedisk);
2159 	}
2160 	free(label_msg);
2161 	free(freespace);
2162 
2163 	return i;
2164 }
2165 
2166 
2167 /* Ask for a partition size, check bounds and do the needed roundups */
2168 daddr_t
2169 getpartsize(struct disk_partitions *parts, daddr_t orig_start,
2170     daddr_t partstart, daddr_t dflt)
2171 {
2172 	char dsize[24], isize[24], max_size[24], maxpartc, valid_parts[4],
2173 	    *label_msg, *prompt, *head, *hint, *tail;
2174 	const char *errmsg = NULL;
2175 	daddr_t i, partend, diskend, localsizemult, max, max_r, dflt_r;
2176 	struct disk_part_info info;
2177 	part_id partn;
2178 
2179 	diskend = parts->disk_start + parts->disk_size;
2180 	max = parts->pscheme->max_free_space_at(parts, orig_start);
2181 	max += orig_start - partstart;
2182 	if (sizemult == 1)
2183 		max--;	/* with hugher scale proper rounding later will be ok */
2184 
2185 	/* We need to keep both the unrounded and rounded (_r) max and dflt */
2186 	dflt_r = (partstart + dflt) / sizemult - partstart / sizemult;
2187 	if (max == dflt)
2188 		max_r = dflt_r;
2189 	else
2190 		max_r = max / sizemult;
2191 	/* the partition may have been moved and now not fit any longer */
2192 	if (dflt > max)
2193 		dflt = max;
2194 	if (dflt_r > max_r)
2195 		dflt_r = max_r;
2196 
2197 	snprintf(max_size, sizeof max_size, "%" PRIu64, max_r);
2198 
2199 	maxpartc = 'a' + parts->num_part -1;
2200 	if (parts->num_part > 1) {
2201 		snprintf(valid_parts, sizeof valid_parts, "a-%c", maxpartc);
2202 	} else if (parts->num_part == 1) {
2203 		snprintf(valid_parts, sizeof valid_parts, "  %c", maxpartc);
2204 	} else {
2205 		strcpy(valid_parts, "---");
2206 	}
2207 
2208 	const char *args[] = { valid_parts, max_size, multname };
2209 	hint = NULL;
2210 	head = str_arg_subst(msg_string(MSG_label_size_head),
2211 	    __arraycount(args), args);
2212 	if (parts->num_part)
2213 		hint  = str_arg_subst(msg_string(MSG_label_size_part_hint),
2214 		    __arraycount(args), args);
2215 	tail = str_arg_subst(msg_string(MSG_label_size_tail),
2216 	    __arraycount(args), args);
2217 
2218 	if (hint != NULL)
2219 		asprintf(&label_msg, "%s\n%s\n\n%s", head, hint, tail);
2220 	else
2221 		asprintf(&label_msg, "%s\n\n%s", head, tail);
2222 	free(head); free(hint); free(tail);
2223 
2224 	localsizemult = sizemult;
2225 	i = -1;
2226 	for (;;) {
2227 		snprintf(dsize, sizeof dsize, "%" PRIu64, dflt_r);
2228 
2229 		if (errmsg != NULL && errmsg[0] != 0)
2230 			asprintf(&prompt, "%s\n\n%s", errmsg, label_msg);
2231 		else
2232 			prompt = label_msg;
2233 		msg_prompt_win(prompt, -1, 12, 70, -1,
2234 		    (dflt != 0) ? dsize : 0, isize, sizeof isize);
2235 		if (prompt != label_msg)
2236 			free(prompt);
2237 
2238 		if (strcmp(isize, dsize) == 0) {
2239 			free(label_msg);
2240 			return dflt;
2241 		}
2242 		if (parts->num_part && isize[1] == '\0' && isize[0] >= 'a' &&
2243 		    isize[0] <= maxpartc) {
2244 			partn = isize[0] - 'a';
2245 			if (parts->pscheme->get_part_info(parts, partn,
2246 			    &info)) {
2247 				i = info.start - partstart -1;
2248 				localsizemult = 1;
2249 				max_r = max;
2250 			}
2251 		} else if (atoi(isize) == -1) {
2252 			i = max;
2253 			localsizemult = 1;
2254 			max_r = max;
2255 		} else {
2256 			i = parse_disk_pos(isize, &localsizemult,
2257 			    parts->bytes_per_sector,
2258 			    parts->pscheme->get_cylinder_size(parts), NULL);
2259 			if (localsizemult != sizemult)
2260 				max_r = max;
2261 		}
2262 		if (i < 0) {
2263 			errmsg = msg_string(MSG_Invalid_numeric);
2264 			continue;
2265 		} else if (i > max_r) {
2266 			errmsg = msg_string(MSG_Too_large);
2267 			continue;
2268 		}
2269 		/*
2270 		 * partend is aligned to a cylinder if localsizemult
2271 		 * is not 1 sector
2272 		 */
2273 		int cylsize = parts->pscheme->get_cylinder_size(parts);
2274 		partend = NUMSEC((partstart + i*localsizemult) / localsizemult,
2275 		    localsizemult, cylsize);
2276 		/* Align to end-of-disk or end-of-slice if close enough */
2277 		if (partend > (diskend - sizemult)
2278 		    && partend < (diskend + sizemult))
2279 			partend = diskend;
2280 		if (partend > (partstart + max - sizemult)
2281 		    && partend < (partstart + max + sizemult))
2282 			partend = partstart + max;
2283 		/* sanity checks */
2284 		if (partend > diskend) {
2285 			partend = diskend;
2286 			errmsg = msg_string(MSG_endoutsidedisk);
2287 			continue;
2288 		}
2289 		free(label_msg);
2290 		if (partend < partstart)
2291 			return 0;
2292 		return (partend - partstart);
2293 	}
2294 	/* NOTREACHED */
2295 }
2296 
2297 /*
2298  * convert a string to a number of sectors, with a possible unit
2299  * 150M = 150 Megabytes
2300  * 2000c = 2000 cylinders
2301  * 150256s = 150256 sectors
2302  * Without units, use the default (sizemult).
2303  * returns the raw input value, and the unit used. Caller needs to multiply!
2304  * On invalid inputs, returns -1.
2305  */
2306 daddr_t
2307 parse_disk_pos(
2308 	const char *str,
2309 	daddr_t *localsizemult,
2310 	daddr_t bps,
2311 	daddr_t cyl_size,
2312 	bool *extend_this)
2313 {
2314 	daddr_t val;
2315 	char *cp;
2316 	bool mult_found;
2317 
2318 	if (str[0] == '\0') {
2319 		return -1;
2320 	}
2321 	val = strtoull(str, &cp, 10);
2322 	mult_found = false;
2323 	if (extend_this)
2324 		*extend_this = false;
2325 	while (*cp != 0) {
2326 		if (*cp == 'G' || *cp == 'g') {
2327 			if (mult_found)
2328 				return -1;
2329 			*localsizemult = GIG / bps;
2330 			goto next;
2331 		}
2332 		if (*cp == 'M' || *cp == 'm') {
2333 			if (mult_found)
2334 				return -1;
2335 			*localsizemult = MEG / bps;
2336 			goto next;
2337 		}
2338 		if (*cp == 'c' || *cp == 'C') {
2339 			if (mult_found)
2340 				return -1;
2341 			*localsizemult = cyl_size;
2342 			goto next;
2343 		}
2344 		if (*cp == 's' || *cp == 'S') {
2345 			if (mult_found)
2346 				return -1;
2347 			*localsizemult = 1;
2348 			goto next;
2349 		}
2350 		if (*cp == '+' && extend_this) {
2351 			*extend_this = true;
2352 			cp++;
2353 			break;
2354 		}
2355 
2356 		/* not a known unit */
2357 		return -1;
2358 
2359 next:
2360 		mult_found = true;
2361 		cp++;
2362 		continue;
2363 	}
2364 	if (*cp != 0)
2365 		return -1;
2366 
2367 	return val;
2368 }
2369